If Window = another window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

If Window = another window

15 Feb 2018, 05:59

Is it possible to make a program that checks if the same window is active when it checked last time? And if its the same window then just FileAppend which key is pressed?
Something like this

Code: Select all

If %Window% = %Not same Window%
~b::fileappend, %Title% %time% b, %A_ScriptDir%\folder\lol.txt
if %Window% = %Same Window%
~b::fileappend, b, %A_ScriptDir%\folder\lol.txt
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 06:30

WinActive() will give you a unique ID for the active window.

Code: Select all

lastWindow := 0
Loop {
	thisWindow := WinActive()
	if (thisWindow != lastWindow){
		soundbeep
		lastWindow := thisWindow
	}
	Sleep 100
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 06:49

I will try that
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 07:09

This doesn't seem to work when i put msgbox inside it tells me that thiswindow = 0x0?

Code: Select all

#SingleInstance Force
#NoTrayIcon
lastWindow := 0
SendMode, Input
FormatTime, time, h:m:s tt
WinGetTitle, Title, A
thisWindow := WinActive()
~a::
	if (thisWindow != lastWindow)
	{
		fileappend, `na`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow
	}
	else
	{
		fileappend, %Title% %time% `na`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow
		MsgBox, %lastWindow%
	}
	return
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 07:22

If it is hitting the Msgbox, then by definition if (thisWindow != lastWindow) is false, so thiswindow is equal to lastwindow. If it is the first time through the code, then lastWindow is 0, so thisWindow must also be 0.

FYI thisWindow := WinActive() is only executed once, at the start of the script. It is NOT executed ever again, so thisWindow never changes value

ie you probably want

Code: Select all

~a::
	thisWindow := WinActive()
	if (thisWindow != lastWindow)
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 07:32

Oh thanks now i feel dumb :D
I'll try it now
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 07:36

Like this right? Because it still doesn't seem to work?

Code: Select all

#SingleInstance Force
#NoTrayIcon
lastWindow := 0
SendMode, Input
FormatTime, time, h:m:s tt
WinGetTitle, Title, A
~a::
thisWindow := WinActive()
	if (thisWindow != lastWindow)
	{
		fileappend, `na`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow
	}
	else
	{
		fileappend, %Title% %time% `na`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow
	}
	return
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 07:46

Again WinGetTitle, Title, A only runs once, at the start of the script.
By definition therefore, the active window when the script runs will likely be Explorer (if you double-clicked the AHK file in a folder) or the desktop (If you clicked a desktop shortcut) or maybe an editor such as SciTE if you ran the script from that.
It will NOT be the game window.

Same deal with time, it never changes.

I assume that is what you mean by "It doesn't work", as the "Is this the last window?" logic most definitely does work.
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 07:50

Oh wow i'll change that.
Thanks
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 07:52

Also this code is pointless:

Code: Select all

	else
	{
		fileappend, %Title% %time% `na`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow	; <-- Pointless
	}
If the else block is hit, then by definition, lastWindow is already equal to thisWindow, so you do not need to assign.
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 07:55

Personally I would write it thus:

Code: Select all

str := ""
if (thisWindow != lastWindow){
	str := Title " " time "`n"
}
fileappend, %str%a`n, %A_ScriptDir%\folder\lol.txt
That way, you avoid duplication of code - the fileappend command is only used once - if you want to change where it logs to (Or log using a different method than fileappend), you now only need to change 1 line of code instead of 2
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 08:13

Okay! So know it simply doesn't show the time or winactive in lol.txt.

Code: Select all

#SingleInstance Force
#NoTrayIcon
lastWindow := 0
SendMode, Input
~a::
FormatTime, time, h:m:s tt
WinGetTitle, Title, A
thisWindow := WinActive()
str := ""
	if (thisWindow != lastWindow) {
		str := Title " " time "`n"
	fileappend, %str%a`n, %A_ScriptDir%\folder\lol.txt
	}
	else
	{
		fileappend, %str%a`n, %A_ScriptDir%\folder\lol.txt
		lastWindow := thisWindow
		MsgBox, %thisWindow%
	}
	return
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: If Window = another window

15 Feb 2018, 08:44

As I said, an if/else block and repeating the fileAppend is pointless

Code: Select all

#SingleInstance Force
#NoTrayIcon
lastWindow := 0
SendMode, Input
~a::
	FormatTime, time, h:m:s tt
	WinGetTitle, Title, A
	thisWindow := WinActive()
	str := ""
	if (thisWindow != lastWindow) {
		str := Title " " time "`n"
		lastWindow := thisWindow
	}
	fileappend, %str%a`n, %A_ScriptDir%\folder\lol.txt
	return
Jonas353
Posts: 64
Joined: 13 Oct 2017, 06:37

Re: If Window = another window

15 Feb 2018, 09:02

It's not made to steal passwords i just made it to see how much i was typing everyday.
But it still doesn't show Time or winactive

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot] and 127 guests