3

When I'm connected to an external display, I would like to close the laptop without it going to sleep. When I'm not connected to an external display, I would like to have closing the lid put the laptop to sleep.
I know I can achieve this by manually switching the power settings, but I'd like something automatic. Any idea? Сan we track the external display connection?
Windows 10

Starcev Misha
  • 33
  • 1
  • 3

3 Answers3

0
  1. Determine (or create, if necessary) two power schemes, one with sleep button enabled, one with disabled.

  2. Using command powercfg /l determine GUID's of these schemes.

  3. Install AutoHotKey and set up launching this monitoring script after each start of Windows. Every time the monitor is connected and disconnected, AutoHotKey will run the script for you, switching the power scheme:

    OnMessage(0x219, "MsgMonitor")
    MsgMonitor(wParam, lParam, msg)
    {
        if (wParam = 7) {
            Run, powercfg /s 381b4222-f694-41f0-9685-ff5bb260df2e
        } Else {
            Run, powercfg /s 381b4222-0001-2222-3333-000000000000
        }
        MsgBox check %wParam% and %lParam% and decide to run programs with %msg%
    }
    ;wParam: 7 lParam: 0  monitor connected
    ;wParam: 32772 lParam: 8977536 should be on disconected

Important: Replace sample GUID's in the above code with GUID's you determined in step 2.

Sources:

miroxlav
  • 13,008
  • 6
  • 65
  • 103
0

@miroxlav solution did not work for me. I changed the script as follows.

  • You still have to create two power saving configs
  • The AutoHotKey script is typically executed at startup.
  • The caught event is a bit different (WM_DISPLAYCHANGE)
  • You have to identify your main monitor instance nam from powershell get-WmiObject or device manager or...
  • power config UUIDs are hard coded in script too.
    /*
       Please note that it is not sufficient to count the number of monitors because the
       main monitors goes off line when you close the lid.
       Which resets the count to... 1
       So instead, we just make our decision on the presence of a different monitor than the known
       main one (hardcoded id, SN is a poor criterion).
    */

    /*  
        Subscribe to windows event
        0x7E = WM_DISPLAYCHANGE
    */
    OnMessage(0x7E, "MsgMonitor")

    MsgMonitor(wParam, lParam, msg) {

    /* Sleep 2 sec because there is a delay before display is known to WMI */
    Sleep 2000

    /* default */
    strComputer := "."

    /* This is the one for my PC... */
    myMonitor := "DISPLAY\LGD056E\4&13419694&0&UID265988_0"

    objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\wmi")
    colItems := objWMIService.ExecQuery("Select * FROM WMIMonitorID")._NewEnum

    hasExternal := false

    While colItems[objItem]
    if objItem.instanceName != myMonitor {
        hasExternal := True
    }

    if ( hasExternal ) {
        /* this is the power config that does not set laptop to sleep on lid closing */event
        Run, powercfg /s a48ebd52-0590-400d-b032-ac7f4302c0e1
    } Else {
        /* this instead is the power config that does set laptop to sleep on lid closing event */
        Run, powercfg /s 377a8558-bff4-4f51-ab43-626b1aa5a65f
    }

}
Alain Pannetier
  • 789
  • 2
  • 10
  • 21
0

@miroxlav solution worked, however when you remove the external display, it will not return to the original power setting. Here's how I did it.

  1. Create a power plan which disables sleep when the lid is closed.
  2. Create a power plan which DOES NOT disable sleep when the lid is closed.
  3. Install AutoHotKey

Open notepad and paste the code below. Save as AHK and run.

This autohotkey script detects the monitor count and primary monitor. If the monitor count is greater than 1, then it will change the power setting. Don't forget to paste the corresponding power schemes.

If this works, you can run this script on startup by hitting Win + R, then type shell:startup and then pasting the script there.

OnMessage(0x219, "MsgMonitor")
     MsgMonitor(wParam, lParam, msg)
     {

        SysGet, MonitorCount, MonitorCount
        SysGet, MonitorPrimary, MonitorPrimary

        count := 0
        Loop, %MonitorCount%
        {
            count++
        }

        IfLessOrEqual, count, 1
            Run, powercfg /s c7046c63-d4a3-4246-910c-c994cd704433 /* no external monitor power setting */
        Else
            Run, powercfg /s 3791f438-87b9-4243-89a1-00c797e02c84 /* external monitor connected */
     }