連接外部顯示器時關閉睡眠模式

連接外部顯示器時關閉睡眠模式

當我連接到外部顯示器時,我想關閉筆記型電腦而不使其進入睡眠狀態。當我未連接到外部顯示器時,我希望合上蓋子使筆記型電腦進入睡眠狀態。
我知道我可以透過手動切換電源設定來實現此目的,但我想要自動的。任何想法?我們可以追蹤外部顯示器連接嗎?
Windows 10

答案1

  1. 確定(或根據需要建立)兩種電源方案,一種啟用睡眠按鈕,一種禁用睡眠按鈕。

  2. 使用指令powercfg /l確定這些方案的 GUID。

  3. 安裝 AutoHotKey 並設定在每次啟動 Windows 後啟動此監控腳本。每次連接並斷開顯示器時,AutoHotKey 都會為您執行腳本,切換電源使用方案:

    OnMessage(0x219,「訊息監視器」)
    MsgMonitor(wParam,lParam,msg)
    {
        如果(wParam = 7){
            運行,powercfg /s 381b4222-f694-41f0-9685-ff5bb260df2e
        } 別的 {
            運行,powercfg /s 381b4222-0001-2222-3333-000000000000
        }
        MsgBox 檢查 %wParam% 和 %lParam% 並決定執行具有 %msg% 的程序
    }
    ;wParam: 7 lParam: 0 已連線監視器
    ;wParam: 32772 lParam: 8977536 應該處於斷開狀態

重要的:將上述程式碼中的範例 GUID 替換為您在步驟中確定的 GUID2

資料來源:

答案2

@miroxlav 解決方案對我不起作用。我將腳本更改如下。

  • 您仍然需要建立兩個節能配置
  • AutoHotKey 腳本通常在啟動時執行。
  • 捕獲的事件有點不同(WM_DISPLAYCHANGE)
  • 您必須從 powershell get-WmiObject 或裝置管理員或...中識別您的主監視器實例 nam
  • 電源配置 UUID 也在腳本中硬編碼。
    /*
       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
    }

}

答案3

@miroxlav 解決方案有效,但是當您移除外部顯示器時,它不會返回原始電源設定。我是這樣做的。

  1. 建立一個電源計劃,在合上蓋子時禁用睡眠。
  2. 建立電源計劃,當蓋子關閉時不會停用睡眠。
  3. 安裝自動熱鍵

打開記事本並貼上下面的程式碼。另存為AHK並運作。

此自動熱鍵腳本偵測監視器計數和主監視器。如果監視器計數大於 1,則會變更電源設定。不要忘記貼上相應的電源方案。

如果有效,您可以按 Win + R 在啟動時執行此腳本,然後鍵入shell:startup並貼上該腳本。

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 */
     }

相關內容