Lock PC & turn off screen

with AutoHotkey

by Philippe Lamarre
last updated June 26, 2014
difficulty.png Easy

by Philippe Lamarre


Every time you leave your work station for a while, you might want to lock your PC to prevent others from using it. For some companies, it is a protocol every employee has to follow.

Now you might think that with the Windows+L combination you don't need a script to do it. Even though this quick combination will lock your PC faster than the Ctrl+Alt+Del one, your monitor will still be open and you will have to wait until Windows power management kicks in for the screen to go dark.

If you're trying to save battery life on your laptop PC, this script is right for you since there is no button to turn off your screen. If you are using a desktop PC, you could always just hit the power off button on the monitor, but then you'd have to power it back on again every time you sit down—this method uses Windows power management to shut down the monitor, and then you just need to move your mouse or hit the keyboard to force it to come back. You can do it all in a single keystroke with AutoHotkey and your Enterpad.



overlay-um-date.gifPrepare your overlay
Choose an unassigned key on the Enterpad to use to lock your PC.
Name it something appropriate. This example uses Lock.


Code installation

Copy/paste the following script in your AutoHotkey script template (Enterpad.ahk) at the chosen key location.

Sleep, 2000
SendMessage,0x112,0xF170,2,,Program Manager


More Info

The previous script serves you well during your work day, but at the end of it you might want to log off and turn off your screen instead of just locking your computer. Here is how you can do it :

SendMessage,0x112,0xF170,2,,Program Manager
Sleep, 2000
Shutdown, 0

In this script, we do not use a DLL. Shutdown, 0 makes an automatic reference to a DLL to log off your session. In this case, where you want to log off, you have to turn off the screen first and then log off. If you reverse both commands, your session will log off (including AutoHotkey) and it will not be able to execute the second part of the script, which is to turn off your screen.