Create a Hotkey to Enable or Disable Your Second Monitor

I’ve been doing some multi-touch testing with MultiTouchVista, a poor man’s multi-touch simulator. It lets you use multiple mice to simulate multiple touch points. Unfortunately, it doesn’t play well with multiple monitors (Whatever coordinate data exists for the touch points gets “squished” into the space of a single, primary monitor. So if you have 2 screens side by side and you “tap” at the midpoint of your desktop that falls on the edge of each screen, the actual tap will be applied to the midpoint of the first monitor.)

No problem. I’ll disable my second monitor.

Which is fine.

Until I need to do some real work.

No problem. I’ll just enable my second monitor.

Let’s see… one click, two clicks, three clicks… no, wrong drop down, grr… four clicks, five clicks…

This is ridiculous! I need a hotkey!

Toggle Your Second Monitor with AutoHotKey

This information is already out on the internet, but I couldn’t find it all in once place. So here goes.

If you use autohotkey, there’s an easy way to define hotkeys to turn any of your monitors on and off.

UPDATE: This has only worked for me with the original, non-unicode, “basic” version of AutoHotKey. If you’re using the new AutoHotKey_L, chances are this script won’t do anything for you.

First, you’ll need a couple of monitor management functions (which are separately buried in a old 2006 forum post, and provided courtesey of Lexikos). Don’t be intimidated by how long these are. Just shove them into a separate file, like MonitorManagement.ahk:

; EnumDisplayDevices(Index [, ByRef Name, ByRef StateFlags ] )
;
; Index:        One-based index of device to get info for.
; DeviceName:   [out] The name of the device.
; StateFlags:   [out] Any reasonable combination of the following flags:
;   0x00000001      DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
;   0x00000004      DISPLAY_DEVICE_PRIMARY_DEVICE
;   0x00000008      DISPLAY_DEVICE_MIRRORING_DRIVER
; DeviceKey:    [out] Path to the device's registry key relative to HKEY_LOCAL_MACHINE.
;
; Returns true if the display device exists, otherwise false.
;
/* Example 1 (requires EnableDisplayDevice()):
    SecondaryDevice =
    count = 0
    Loop {
        if ! EnumDisplayDevices(A_Index, DeviceName, StateFlags)
            break
        if !(StateFlags & 8) ; not a pseudo-device
            if (++count = 2) ; second device
                break
    }
    if DeviceName
        EnableDisplayDevice(DeviceName, -1) ; toggle
*/
/* Example 2:
    Loop {
        if ! EnumDisplayDevices(A_Index, DeviceName, StateFlags)
            break
        if (StateFlags & 4)
            text .= DeviceName " is the primary display device.`n"
        else if (StateFlags & 1)
            text .= "The desktop extends onto " DeviceName ".`n"
        if (StateFlags & 8)
            text .= DeviceName " is a pseudo-device.`n"
    }
    MsgBox %text%
*/
EnumDisplayDevices(Index, ByRef DeviceName, ByRef StateFlags="", ByRef DeviceKey="")
{
    ; DISPLAY_DEVICE DisplayDevice
    VarSetCapacity(DisplayDevice, 424)
    ; lpDisplayDevice.cb := sizeof(DISPLAY_DEVICE)
    NumPut(424, DisplayDevice, 0)
   
    VarSetCapacity(DeviceName, 32, 0)
    VarSetCapacity(DeviceKey, 128, 0)
    ; For consistency, clear StateFlags in case of failure.
    StateFlags = 0
   
    if ! DllCall("EnumDisplayDevices"
        , "UInt", 0
        , "UInt", Index-1
        , "UInt", &DisplayDevice
        , "UInt", 0)
        return false
   
    StateFlags := NumGet(DisplayDevice, 164)
    DllCall("lstrcpynA", "Str", DeviceName, "UInt", &DisplayDevice+4,   "int", 32)
    DllCall("lstrcpynA", "Str", DeviceKey,  "UInt", &DisplayDevice+296, "int", 128)
    if (SubStr(DeviceKey,1,18)="\Registry\Machine\")
        DeviceKey := SubStr(DeviceKey,19)
    return true
}

; Enables, disables or toggles a display device.
;
; DeviceName:   The name of the device, e.g. \\.\DISPLAY1
; Action:       The action to take.
;                    0   Disable
;                    1   Enable
;                   -1   Toggle (may not be reliable if NoReset=true)
; NoReset:      If true, settings will be saved to the registry, but not applied.
;
; The following can be used to apply settings saved in the registry:
;   DllCall("ChangeDisplaySettings", "uint", 0, "uint", 1)
;
; Return values:
;    DISP_CHANGE_SUCCESSFUL       0
;    DISP_CHANGE_RESTART          1
;    DISP_CHANGE_FAILED          -1
;    DISP_CHANGE_BADMODE         -2
;    DISP_CHANGE_NOTUPDATED      -3
;    DISP_CHANGE_BADFLAGS        -4
;    DISP_CHANGE_BADPARAM        -5
;
; Examples:
;   ; disable display 2
;     EnableDisplayDevice("\\.\DISPLAY2", 0)
;     Sleep, 10000
;
;   ; simultaneously enable display 2 and disable display 1
;     EnableDisplayDevice("\\.\DISPLAY2", 1, true)
;     EnableDisplayDevice("\\.\DISPLAY1", 0)
;     Sleep, 10000
;
;   ; ensure both are enabled
;     EnableDisplayDevice("\\.\DISPLAY2", 1, true)
;     EnableDisplayDevice("\\.\DISPLAY1")
;
; Note: DeviceNames may vary. Rather than hard-coding the device name,
;       EnumDisplayDevices() should be used to enumerate the devices.
;
EnableDisplayDevice(DeviceName, Action=1, NoReset=false)
{
    if (Action = -1)
    {   ; Determine if the display should be enabled or disabled.
        Loop {
            if ! EnumDisplayDevices(A_Index, this_name, this_state)
                break
            if (this_name = DeviceName) {
                Action := !(this_state & 1) ; DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
                break
            }
        }
        ; If Action is still -1, an invalid DeviceName was specified.
        ; The script will attempt to enable the display device, but
        ; ChangeDisplaySettingsEx() will most likely return error -5.
    }

    VarSetCapacity(devmode, 156, 0)
    NumPut(156, devmode, 36, "UShort")

    ; Set DEVMODE.dmFields to indicate which fields are valid.
    if (Action) ; Enable
        NumPut(0x000020, devmode, 40)   ; position={0,0}
    else        ; Disable
        NumPut(0x180020, devmode, 40)   ; width=0, height=0, position={0,0}

    ; Since CDS_NORESET is specified here, if NoReset=true, the user must
    ; manually call ChangeDisplaySettings(NULL,1) or restart the computer.
    err := DllCall("ChangeDisplaySettingsEx", "str", DeviceName
        , "uint", &devmode, "uint", 0, "uint", 0x10000001, "uint", 0)
   
    ; ChangeDisplaySettings() is called here for two reasons:
    ;   - A restart is otherwise required to enable a secondary display device.
    ;       See:
http://support.microsoft.com/kb/308216
    ;   - Disabling display devices with just ChangeDisplaySettingsEx()
    ;     tends to leave them turned on.
    if (!err && !NoReset)
        err := DllCall("ChangeDisplaySettings", "uint", 0, "uint", 1)
   
    return err
}

Now that you have that in a separate file, you can include it in your main ahk script and easily create a hotkey to toggle your second monitor (or your first or third or fourth). I’m currently using Ctrl–Backtick to toggle my second monitor. Here’s how:

#Include MonitorManagement.ahk
^`::EnableDisplayDevice("\\.\DISPLAY2", -1) ;toggle second monitor

Modify to your liking.


0 Response to "Create a Hotkey to Enable or Disable Your Second Monitor"

Post a Comment