How can I lock my computer with AutoHotkey?
Asked Answered
P

3

13

I'm trying to bind "Esc" key to lock my computer with AutoHotkey.

Manually pressing Winkey + l will lock my computer, but it doesn't work in my AutoHotkey script.

esc::
   MsgBox Going to lock
   Send, #l
Return

I have tried multiple other AutoHotkey syntax (without the modifier for example) without success.

Picturesque answered 18/2, 2017 at 12:5 Comment(2)
but I'm not in locked screen screen mode ! I'm in satndard window, and I want to trigger locked scren mode via AHKPicturesque
Ah. I see! DllCall("LockWorkStation") instead of send #lSacramentalism
T
21

Per the recommendation in the comments by wOxxOm:

Esc::
{
DllCall("LockWorkStation")
}
return
Topdrawer answered 15/5, 2017 at 18:53 Comment(1)
This can be simplified to Esc::DllCall("LockWorkStation")Rome
A
1

What you are doing in that code, is pressing the windows key first and then the 'l' key. Not both at the same time. To make key combinations, you need to press the combination key down and then the key you want to combine it with. Remember to release the key afterwards. Your code would then look like:

Send {LWin down}
Send l
Send {LWin up}

or

Send {LWin down}l{LWin up}
Alcazar answered 22/3, 2017 at 19:16 Comment(5)
As much as I like this potential solution in that it doesn't require knowledge of a new function, I tried it out and it just didn't work.Thagard
@Thagard You have to Send "{Blind}" first. Docs explicitly mention protection against accidental Win+L, and Blind disables it. Also quotes are needed. Try #J::Send "{Blind}{LWin down}L{LWin up}"Jurel
@Jurel I tried it and it resulted in simply inputting text "L" when I press the #J (windows key + J) buttons. It did not result in getting to the Windows Lock screen.Thagard
@Thagard Try #J::Send "{Blind}{j up}l{LWin up}" - or RWin up depending on your keyboard. If it doesn't help, add KeyHistory, you'll need 4 lines, 1st: #J::{, 2nd: Send "{Blind}{j up}l{LWin up}", 3rd: KeyHistory, 4th: }. KeyHistory will show you sequence of keys pressed and simulated. Also check isn't Win+L disabled and does it lock workstation. Also ensure only one instance of AHK owns the keyboardJurel
@Jurel // I appreciate the response. For me, the simplicity and easier reproducibiility of "wOxxOm" / "David Metcalfe" 's response are good enough for me.Thagard
G
1

Just improving the code of my fellow camarades, you can block AND turn off the screen if you want to:

#J::
    KeyWait LWin
    KeyWait J
    DllCall("LockWorkStation")
    SendMessage 0x0112, 0xF170, 2,, "Program Manager"
    Return
Glimmering answered 3/12, 2022 at 18:32 Comment(2)
for those wondering about SendMessage parameters, the exact same snippet is in docs - "0x0112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER"Jurel
For me to make it work, I had to enclose the commands in braces {,} and quote ", " keynames in KeyWait. Thus: 1st line #J::{, 2nd line KeyWait "LWin", 3rd line KeyWait "J", and add a new line } at the endJurel

© 2022 - 2024 — McMap. All rights reserved.