Page 1 of 1

Stop script when windows is locked

Posted: 22 Oct 2019, 06:43
by rafaelrsss
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


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

Re: Stop script when windows is locked

Posted: 22 Oct 2019, 06:50
by jNizM
See this example -> (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.

Re: Stop script when windows is locked

Posted: 22 Oct 2019, 07:08
by rafaelrsss
But still it is not clear to me how could I integrate your code into mine... maybe I need to study more about functions.

Re: Stop script when windows is locked  Topic is solved

Posted: 22 Oct 2019, 07:16
by jNizM
Basic Example:

Code: Select all

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

#SingleInstance Force

OnExit, EOF

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

SetTimer, MyTimer, 1000

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

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

SessionChange(notify := true)
	static WTS_CURRENT_SERVER      := 0

	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

    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 ==========================================================================================================================


; ===============================================================================================================================

Re: Stop script when windows is locked

Posted: 22 Oct 2019, 11:29
by rafaelrsss
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.