Skip to content

Instantly share code, notes, and snippets.

@sankarara
Created March 23, 2016 14:29
Show Gist options
  • Save sankarara/5498337c33d42cb99efd to your computer and use it in GitHub Desktop.
Save sankarara/5498337c33d42cb99efd to your computer and use it in GitHub Desktop.
AutoHotKey: Turn monitor off with a keyboard shortcut
; This is part of my AutoHotkey [1] script that turns off my monitor when I
; press Win+\.
; It also turns the screensaver on at the same time, so that Windows Live
; Messenger (and any other programs) know I am away.
; I don't have a password on my screensaver, so there is a second version
; (Win+Shift+\) that locks the PC as well.
; Note: Sometimes the monitor comes back on after a second and I have to
; press it again. Not sure why. Adding "Sleep" to pause first didn't help.
; [1]: http://www.autohotkey.com/
; Win+\
#\::
SendMessage 0x112, 0xF140, 0, , Program Manager ; Start screensaver
SendMessage 0x112, 0xF170, 2, , Program Manager ; Monitor off
Return
; Win+Shift+\
#+\::
Run rundll32.exe user32.dll`,LockWorkStation ; Lock PC
Sleep 1000
SendMessage 0x112, 0xF170, 2, , Program Manager ; Monitor off
Return
@Xoniz01
Copy link

Xoniz01 commented Jul 25, 2021

Hi, I find that adding "Sleep 1000" as below fix the glitch (as the monitor comes back), probably due to bouncing effect of keyboard clicks.
Hope this help.
"
SendMessage 0x112, 0xF140, 0, , Program Manager ; Start screensaver
SendMessage 0x112, 0xF170, 2, , Program Manager ; Monitor off
Sleep 1000
"

@MartonSuveges
Copy link

Hi, i found that if you add the 1s sleep between the 2 calls works for me, like this:

SendMessage 0x112, 0xF140, 0, , Program Manager ; Start screensaver
Sleep 1000
SendMessage 0x112, 0xF170, 2, , Program Manager ; Monitor off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment