Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turn monitor off with a keyboard shortcut with AutoHotKey
; 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
@willpiam

This comment has been minimized.

Copy link

willpiam commented Oct 22, 2017

Dude! I want you to know that your program helped me. I have no experience with auto hotkey and I'm kinda pressed for time at the moment so I don't have time to learn right now. I needed a way of doing exactly this and I needed it quick! You're code works perfectly!
Finding people like you on the internet always brightens my day. Thank you so much.

@Kax0n

This comment has been minimized.

Copy link

Kax0n commented Nov 6, 2017

Would it be possible to create a script that turns off, locks the computer, and the screen doesn't turn back on until you press a certain keybind? This would be very helpful if it is.

@deqing

This comment has been minimized.

Copy link

deqing commented Nov 23, 2017

Thanks for the script! Just a little curious why there is a ` in the Win+Shift+\ code?

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented Nov 23, 2017

` is the escape character, so `, results in a literal comma instead of moving to the next parameter.

@Techtiger255

This comment has been minimized.

Copy link

Techtiger255 commented Sep 28, 2018

Dave I know why it sometimes turns on after a second! This DLL callback is set so that it just turns off the monitor, aka when a mouse/key event happens after that time period then it's just like if you had an auto sleep function on your PC, noticed the screen turning off and didn't want this to happen, then wiggled the mouse to wake it back up. A simple solution to this is to call a blockinput function BlockInput documentation. Call this right before the monitor off function and disable it about 1.5 seconds after this function is called.

@chasenstark

This comment has been minimized.

Copy link

chasenstark commented Jan 21, 2019

I also had an issue with the screen turning back on. I used Techtiger's solution, but it only worked for me after I switched the order of the screensaver and monitor off. This is the script that worked for me:


; Win+\
#\::
    BlockInput On
    SendMessage 0x112, 0xF170, 2, , Program Manager  ; Monitor off
    SendMessage 0x112, 0xF140, 0, , Program Manager  ; Screensaver
    Sleep 2000
    BlockInput Off
    return
@eduardoelujanf

This comment has been minimized.

Copy link

eduardoelujanf commented Jul 4, 2019

Hello all, I tried the original script by davejamesmiller, and it successfully turns my display off, for about 2 sec.
I then tried chasenstark's solution to the problem, but was not successfull. It still turns back on in about 2 sec.

Am I the only one having this issue?

@Nepaltechguy2

This comment has been minimized.

Copy link

Nepaltechguy2 commented Jul 7, 2019

@eduardoelujanf no you are not, I am having this issue as well.

@JonReyH

This comment has been minimized.

Copy link

JonReyH commented Nov 13, 2019

Could you please improve your code to allow turn on/off 1st and/or 2nd monitor if two monitors are connected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.