Stop script when windows is locked Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rafaelrsss
Posts: 13
Joined: 09 Nov 2016, 07:11

Stop script when windows is locked

22 Oct 2019, 06:43

Hi all.

I am trying to implement a script that register at every second the active window name/process and save to a txt file counting the total time in which the window was active. So far it works. However, I wanted the script to be smart enough to stop when the user lock the screen... any ideas about how to do it?

Any help is appreciated. The code so far is this:

Code: Select all

path=some_random_path


Loop {
    ActiveBegin := A_TickCount
    WinGetActiveTitle, ActiveTitle
	WinGet, ActiveProcess, ProcessName, A
    ; Wait for some other window to be activated.
    WinWaitNotActive, % "ahk_id " . WinActive("A")
    ; Calculate how long it was active for.
    ActiveTime := A_TickCount - ActiveBegin
    ; Log the title and duration of the active window.
    FileAppend, %A_YYYY%-%A_MM%-%A_DD% %A_Hour%:%A_Min%:%A_Sec%$%ActiveTitle%$%ActiveProcess%$%ActiveTime%`n, %path%\log_%A_UserName%.txt
	sleep, 1000
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Stop script when windows is locked

22 Oct 2019, 06:50

See this example -> https://github.com/jNizM/AHK_Scripts/blob/master/src/messages/WM_WTSSESSION_CHANGE.ahk (with WTS_SESSION_LOCK status code)

Instand with loop + sleep 1000 I would use SetTimer. With the Session Check you can disable the SetTimer if you lock your pc and start with unlock again.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
rafaelrsss
Posts: 13
Joined: 09 Nov 2016, 07:11

Re: Stop script when windows is locked

22 Oct 2019, 07:08

Thanks!
But still it is not clear to me how could I integrate your code into mine... maybe I need to study more about functions.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Stop script when windows is locked  Topic is solved

22 Oct 2019, 07:16

Basic Example:

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force

OnExit, EOF


; SCRIPT ========================================================================================================================

SessionChange(true)
SetTimer, MyTimer, 1000
return

MyTimer:
	; Input your code here (but without loop and sleep)
	MsgBox % "It's me!"
return


; FUNCTIONS =====================================================================================================================

SessionChange(notify := true)
{
	static WTS_CURRENT_SERVER      := 0
	static NOTIFY_FOR_ALL_SESSIONS := 1

	if (notify) {
		if !(DllCall("wtsapi32.dll\WTSRegisterSessionNotificationEx", "ptr", WTS_CURRENT_SERVER, "ptr", A_ScriptHwnd, "uint", NOTIFY_FOR_ALL_SESSIONS))
			throw Exception("WTSRegisterSessionNotificationEx", -1)
		OnMessage(0x02B1, "WM_WTSSESSION_CHANGE")
	} else {
		OnMessage(0x02B1, "")
		if !(DllCall("wtsapi32.dll\WTSUnRegisterSessionNotificationEx", "ptr", WTS_CURRENT_SERVER, "ptr", A_ScriptHwnd))
			throw Exception("WTSUnRegisterSessionNotificationEx", -1)
	}
	return true
}

WM_WTSSESSION_CHANGE(wParam, lParam)
{
    static WTS_SESSION_LOCK   := 0x7
	static WTS_SESSION_UNLOCK := 0x8

	if (wParam = WTS_SESSION_LOCK)			; Session is "Locked"
		SetTimer, MyTimer, Off				; -> Set Timer Off
	if (wParam = WTS_SESSION_UNLOCK)		; Session is "UnLocked"
		SetTimer, MyTimer, 1000				; -> Start Timer
}


; EXIT ==========================================================================================================================

EOF:
SessionChange(false)
ExitApp


; ===============================================================================================================================
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
rafaelrsss
Posts: 13
Joined: 09 Nov 2016, 07:11

Re: Stop script when windows is locked

22 Oct 2019, 11:29

It worked like a charm.
A million thanks.

Now I need to understand better the function part but I think there is no mistery. Is that I am not much into ahk functions... I need to study a little bit.

Anyway, thanks.
:thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, RussF and 380 guests