Locking computer & unlocking with a Hotkey (not having to type password)

Post your working scripts, libraries and tools
User avatar
Joe Glines
Posts: 629
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Locking computer & unlocking with a Hotkey (not having to type password)

17 Jan 2016, 21:53

Working in a corporate office I need to make sure I lock my computer screen if-when I step away. Most people use the Windows+L to do this however my script kills the screen and allows me to use a Hotkey combination to unlock it (thus not having to type my crazy password).

I use this script at least a dozen times a day. It's also a great way to save battery life on your laptop (if you're taking a short break).

I have a hotkey to launch the below script in my main AutoHotKey.ahk file (thus it is always available)

Code: Select all

#SingleInstance force
#Persistent
#Noenv	
SendMode Input

Sleep 500  ; Give user a chance to release keys (in case their release would wake up the monitor again).

           ; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
SendMessage, 0x112, 0xF170, 2,, Program Manager   ; use 2 to Turn Monitor Off

SetTimer, sleepy, 250 ;set every 1/4 of a second to ensure it is sleeping
return

;***********subroutine to ensure sleeping******************* 
Sleepy:
SendMessage, 0x112, 0xF170, 2,, Program Manager ; Use 2 to turn the monitor off.
return

;***********kill script******************* 
Capslock & u::
;~ SendMessage, 0x112, 0xF170, 1,, Program Manager ; Use 1 to turn the monitor on.
ExitApp
return

;***********create hotstrings for keys so they will do nothing******************* 
;key list found at: http://www.autohotkey.com/docs/KeyList.htm
Tab::
Escape::
Backspace::

Delete::
Insert::
Home::
End::
PgUp::
PgDn::
ScrollLock::
NumLock::

F1::
F2::
F3::
F4::
F5::
F6::
F7::
F8::
F9::
F10::
F11::
F12::
F13::
F14::
F15::
F16::
F17::
F18::
F19::
F20::
F21::
F22::
F23::
F24::

AppsKey::
Up::
Down::
Left::
Right::

LWin::
RWin::

LControl::
RControl::
LShift::
RShift::
LAlt::
RAlt::
Enter::
Space::

PrintScreen::
CtrlBreak::
;~ Pause:: ;I like being able to pause my music even when screen off
;~ Break::

Help::
Sleep::

Browser_Back::
Browser_Forward::
Browser_Refresh::
Browser_Stop::
Browser_Search::
Browser_Favorites::
Browser_Home::
;~ Volume_Mute:: ;I like being able to turn up/down/mute music
;~ Volume_Down::
;~ Volume_Up::
Media_Next::
Media_Prev::
Media_Stop::
Media_Play_Pause::
Launch_Mail::
Launch_Media::
Launch_App1::
Launch_App2::

Numpad0::
NumpadIns::
Numpad1::
NumpadEnd::
Numpad2::
NumpadDown::
Numpad3::
NumpadPgDn::
Numpad4::
NumpadLeft::
Numpad5::
NumpadClear::
Numpad6::
NumpadRight::
Numpad7::
NumpadHome::
Numpad8::
NumpadUp::
Numpad9::
NumpadPgUp::
NumpadDot::
NumpadDel::
NumpadDiv::
NumpadMult::
Numpadsub::
NumpadEnter::
NumpadAdd::

LButton::
RButton::
MButton::

a::
b::
c::
d::
e::
f::
g::
h::
i::
j::
k::
l::
m::
n::
o::
p::
q::
r::
s::
t::
u::
v::
w::
x::
y::
z::

0::
1::
2::
3::
4::
5::
6::
7::
8::
9::

Return 
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: Locking computer & unlocking with a Hotkey (not having to type password)

18 Jan 2016, 06:56

You could improve security quite a lot by preventing intruders from random guessing:
...

Code: Select all

6::
7::
8::
9::i=0
and then

Code: Select all

Capslock & u::
if i++
    {
        String =
        InputBox String, Computer is locked!, Password:, HIDE, 250, 150,,,,60
        If String <> CorrectPassWord  ; or whatever you choose
            Return
    }
ExitApp
Any wrong guess from the snooper will make the script prompt for a password, although it won't be visible!

I stole the idea from myself:
https://autohotkey.com/boards/viewtopic.php?t=3642
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"
User avatar
Joe Glines
Posts: 629
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Locking computer & unlocking with a Hotkey (not having to type password)

18 Jan 2016, 07:57

Thanks Bon but, for me, the point was to not have to type a password at all. Having said that, I might give some thought to having two, squential, hotkeys. Or at least one hotkey followed by a specific key. Thanks for the idea!
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: Locking computer & unlocking with a Hotkey (not having to type password)

18 Jan 2016, 10:57

I think you missed the point: as long as the first keypress is correct, you don't have to type the password at all. And that's the beauty of it!
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"
User avatar
Joe Glines
Posts: 629
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Locking computer & unlocking with a Hotkey (not having to type password)

18 Jan 2016, 19:34

Sorry Bon, I didn't even look at your code. I still have zero interest in typing a password. I'd rather give no response what so ever than to have a password promt come up
ozzii
Posts: 322
Joined: 30 Oct 2013, 06:04

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 06:38

Your code working just on a qwerty keyboard.
I have an azerty and I have for example é instead of 2.
So my key will wake up the monitor.
TheBrohman
Posts: 13
Joined: 07 Dec 2015, 12:31

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 07:55

Just a quick question. I am new to AHK scripting. I mean, I am able to read and code them, but I am not quite sure if I figured out how you script works. Is it so you press CapsLock and U at the same time, to unlock it?
User avatar
Joe Glines
Posts: 629
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 08:35

Yes, that is correct. I recommend you change it to something that allows you to do the key-combo with one hand instead of two. I didn't want to put my actual one in case someone happens to work where I do and read it...

When you're testing this, be sure you don't have any unsaved docs open. There's a chance you will lock your screen and can't unlock it. It is a bummer to have to reboot and lose data!
iPhilip
Posts: 407
Joined: 02 Oct 2013, 12:21

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 14:21

The following code should work with any keyboard:

Code: Select all

Esc::
SetTimer, Timer, % (t:=!t) ? "On" : "Off"
Gosub Timer
ThisHotkeyVK := GetKeyVK(A_ThisHotkey)
Loop, 255
   if (A_Index <> ThisHotkeyVK)
      Hotkey, % Format("vk{:x}", A_Index), Label, % t ? "On" : "Off"
Return

Label:
Return

Timer:
SendMessage, 0x112, 0xF170, t?2:1,, Program Manager   ; turn monitor on (1) or off (2)
Return
The hotkey can be anything you like, even a combination of keys. :)

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
evilC
Posts: 4726
Joined: 27 Feb 2014, 12:30

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 15:14

iPhillip:
I use similar code in a hotkey bind routine (Loop 255 and Format) and it does not seem to get a list of all keys.

For example, note that the Arrow keys are absent from this list:

Code: Select all

Loop, 255 {
   key := GetKeyName(Format("vk{:x}", A_Index))
   if (key != "")
	str .= key "`r`n"
}
clipboard := str
Spoiler
The arrow keys are the same VK as some of the Numpad keys, but the SC is different.
If anyone knows a way to get all keys using a similar technique, I would love to hear it.

BTW, if you do use this technique, just declare all the hotkeys at the start of the script, not when you lock/unlock the keyboard.
Put Suspend On BEFORE you declare all the hotkeys, then you can just use Suspend On/Off to turn them on or off.
It's much quicker that way as it doesnt have to do the loop each time.
You can use Suspend, Permit to make the lock hotkey immune to suspend.

eg:

Code: Select all

Suspend On
Loop, 255 {
	key := GetKeyName(Format("vk{:x}", A_Index))
	if (key != "")
		Hotkey, % key, Label, On
}
; end of auto-execute

[...]
; Block the keyboard
Suspend Off

; Re-enable the keyboard
Suspend On

[...]

LabelCalledByLockHotkey:
	Suspend, Permit ; Make the hotkey that calls this label immune to Suspend
iPhilip
Posts: 407
Joined: 02 Oct 2013, 12:21

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 16:30

Hi evilC,

Thank you for your comments. I am not sure I understand why you say that
evilC wrote:The arrow keys are the same VK as some of the Numpad keys
Microsoft claims otherwise here. For example,

Code: Select all

VK_LEFT		0x25	LEFT ARROW key
VK_NUMPAD4	0x64	Numeric keypad 4 key
Nevertheless, you are correct in that AutoHotkey maps the two codes differently as demonstrated in the following code:

Code: Select all

MsgBox % GetKeyName("vk25")   ; NumpadLeft
MsgBox % GetKeyName("vk64")   ; Numpad4
so that the arrow keys don't have a virtual key code. :shock: In any case, the code I posted works for me in that the arrow keys and the Numpad keys don't wake up the monitor.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
evilC
Posts: 4726
Joined: 27 Feb 2014, 12:30

Re: Locking computer & unlocking with a Hotkey (not having to type password)

19 Jan 2016, 17:36

The arrow keys DO have a virtual key code, but it seems that VK alone does not describe a key.

A combination of VK and SC describe a key.

Code: Select all

str .= "Left VK: " GetKeyVK("Left") ", Left SC: " GetKeySC("Left") "`r`n"
str .= "NumpadLeft VK: " GetKeyVK("NumpadLeft") ", NumpadLeft SC: " GetKeySC("NumpadLeft")
Clipboard := str
Result:
Left VK: 37, Left SC: 331
NumpadLeft VK: 37, NumpadLeft SC: 75

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bing [Bot] and 35 guests