Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to detect when the computer is locked and run command?


  • Please log in to reply
9 replies to this topic
BONER
  • Members
  • 2 posts
  • Last active: Jun 03 2010 02:29 AM
  • Joined: 03 Jun 2010
Hi

I want to be able to run a script or command when the computer is locked. It needs to detect when the computer is locked. (That's the short version of my question)

(Long version cont.)
Happy for the script to be constantly waiting for a specific window (tried waiting for the computer locked window but that didn't work) or poll the system once a minute (Doesn't have to be the exact second the computer is logged)

Pretty much the script needs to add a line to a text file to record when the computer is locked. I generally lock the computer using ALT+CTRL+DEL then pressing ENTER. This isn't going to change anytime soon, so using the Win+L option isn't going to work for me.

Only trouble I'm having is that I can't figure out how to detect when the PC is locked and have it action anything when the system is in locked state. Would also be great to be able to append the text file when I unlock the computer as well, but I think I know how this can be done if I can get the script to run a command when it is locked.

MAKING AN ALTERNATIVE LOCK SCREEN GUI IS NOT AN OPTION. (thought I might point that out)

Any help to detect when the computer is in locked state would be greatly appreciated.

None
  • Members
  • 3199 posts
  • Last active: Apr 13 2014 02:11 AM
  • Joined: 28 Nov 2009
!^Del::FileAppend %A_Now%`n,Log.txt
!^Del is a valid hotkey but Ahk does not block it from the system, so this is one way if you always lock with alt+ctrl+del.
Also a quick check showed the WinGetActiveTitle returned nothng when my computer was locked. Maybe you could setup a loop or SetTimer to check the name of the active window and log when it disapears and when there is one to detect again

tank
  • Moderators
  • 4242 posts
  • Last active: Yesterday, 01:57 AM
  • Joined: 21 Dec 2007
if !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1)

     msgbox desktop is locked
I should probably kick my own ass for providing this but since i am not giving it all to you and you must still implement it i spose its cryptic enuff to not be easily used by idiots

BONER
  • Members
  • 2 posts
  • Last active: Jun 03 2010 02:29 AM
  • Joined: 03 Jun 2010
Thanks.....ummmm...None!

That was perfect. I can't believe it's been that simple this whole time.
Now I just have to play around with it so it will log when I unlock my PC too.

Tank.....I'm not exactly sure what your code is....but I'll have a proper look at it later (at work ATM) though if it does what I think it does then that's perfect too!

doshea
  • Members
  • 1 posts
  • Last active: Jul 27 2010 06:37 AM
  • Joined: 13 Apr 2008
The solutions from None and tank detect different things:

None's code detects the action of the user locking the screen (well, they could be just changing their password)

tank's code detects if the screen is locked at the present time

tank's code happened to perform the kind of checking that I wanted, and I'm pleased to say it worked for me. Elsewhere in these forums I saw a suggestion that one could use
if (!WinExist("A"))
to detect if the screen is locked, but it turns out that (naturally) that code can't tell the difference between the screen being locked and there simply being no windows at all (or at least none that are active).

So thanks, tank!

Gtgrrdsdfg
  • Guests
  • Last active:
  • Joined: --

if !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1)
     msgbox desktop is locked
I should probably kick my own ass for providing this but since i am not giving it all to you and you must still implement it i spose its cryptic enuff to not be easily used by idiots


Wow, that was simple, even for an idiot like me!

HockeyZ
  • Guests
  • Last active:
  • Joined: --

if !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1)
     msgbox desktop is locked
I should probably kick my own ass for providing this but since i am not giving it all to you and you must still implement it i spose its cryptic enuff to not be easily used by idiots


this is glorious, many thanks to you.

SinTroN
  • Members
  • 26 posts
  • Last active: Mar 28 2012 02:44 AM
  • Joined: 12 Apr 2010
It's not the solution. It returns 0 even if you press Ctrl+Alt+Del and see the options window.

tank
  • Moderators
  • 4242 posts
  • Last active: Yesterday, 01:57 AM
  • Joined: 21 Dec 2007
the options isnt equal to being locked
you actually have to lock the desktop

Maestr0
  • Members
  • 650 posts
  • Last active: Mar 16 2014 09:27 PM
  • Joined: 18 Oct 2008
How about this?
old_locked_state = 0
!^Del::FileAppend, %A_Now% locked`n,autohotkey_log.txt
~#L::FileAppend, %A_Now% locked`n,autohotkey_log.txt

check_lock:
	if !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1)
		locked_state = 1
	else
		locked_state = 0

	if locked_state <> %old_locked_state%
	{
		if locked_state <> 1
			FileAppend %A_Now% unlocked`n,autohotkey_log.txt
		old_locked_state := locked_state
	}
return

Also, check here for alternatives: <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?t=5359">viewtopic.php?t=5359</a><!-- l -->