Issues with ifwinactive variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
edc
Posts: 26
Joined: 17 Jun 2021, 16:23

Issues with ifwinactive variables

Post by edc » 22 Jan 2022, 14:17

Hello,
I'm looking for help using variables with ifwinactive.

For some reason I cannot get the variables to work. ifwinactive does work if the window title is typed out: ifwinactive, Title - myusername1

Any ideas on how to get this working?

Code: Select all

user1 = Title - myusername1		;Window Title
user2 = Title - myusername2		;Window Title

xbutton2::TypeNum()

TypeNum(){          			  	;1 or 2 if/else
ifwinactive, %user1%				;ifwinactive, %user#%
	{								;open bracket
	sleep, 50						;sleeps
	send, 1							;send 1
	sleep, 50						;sleeps
	}								;close bracket
else								;else statement
ifwinactive, %user2%				;ifwinactive, %user#%
	{								;open bracket
	sleep, 50						;sleeps
	send, 1							;send 2
	sleep, 50						;sleeps
	}								;close bracket
return								;return - end hot key script
}									;close bracket
Thanks,
edc

User avatar
mikeyww
Posts: 26847
Joined: 09 Sep 2014, 18:38

Re: Issues with ifwinactive variables

Post by mikeyww » 22 Jan 2022, 14:28

Variables in functions are local by default. Since you are not passing parameters or returning any values, you can convert your function to a regular labeled subroutine without any trouble. Declaring variables as global is an alternative.

edc
Posts: 26
Joined: 17 Jun 2021, 16:23

Re: Issues with ifwinactive variables

Post by edc » 22 Jan 2022, 14:42

Hello,
I have looked up how to use global and am not having any luck. Is this how I should be using it?

Code: Select all

user1 = Title - myusername1		;Window Title
user2 = Title - myusername2		;Window Title

xbutton2::TypeNum()

TypeNum(){          			  	;1 or 2 if/else
global user1
global user2
ifwinactive, %user1%				;ifwinactive, %user#%
	{								;open bracket
	sleep, 50						;sleeps
	send, 1							;send 1
	sleep, 50						;sleeps
	}								;close bracket
else								;else statement
ifwinactive, %user2%				;ifwinactive, %user#%
	{								;open bracket
	sleep, 50						;sleeps
	send, 1							;send 2
	sleep, 50						;sleeps
	}								;close bracket
return								;return - end hot key script
}									;close bracket
Thanks,
EDC

User avatar
mikeyww
Posts: 26847
Joined: 09 Sep 2014, 18:38

Re: Issues with ifwinactive variables  Topic is solved

Post by mikeyww » 22 Jan 2022, 14:53

In general, yes. My test below worked. Run Window Spy to confirm your window titles.

Code: Select all

user1 = Funtitled - Notepad
user2 = Untitled - Notepad

XButton2::TypeNum()

TypeNum(){
 Global user1, user2
 If WinActive(user1)
 {
  Sleep, 50
  Send 1
  Sleep, 50
 }
 If WinActive(user2)
 {
  Sleep, 50
  Send 2
  Sleep, 50
 }
}
Or:

Code: Select all

user1 = Funtitled - Notepad
user2 = Untitled - Notepad

#If WinActive(user1)
XButton2::
Sleep, 50
Send 1
Sleep, 50
Return
 
#If WinActive(user2)
XButton2::
Sleep, 50
Send 2
Sleep, 50
Return
#If
Or:

Code: Select all

user1 = Funtitled - Notepad
user2 = Untitled - Notepad

XButton2::
Sleep, 50
If WinActive(user1)
 Send 1
If WinActive(user2)
 Send 2
Sleep, 50
Return
Or: [more ways]

edc
Posts: 26
Joined: 17 Jun 2021, 16:23

Re: Issues with ifwinactive variables

Post by edc » 22 Jan 2022, 15:12

Thank for the quick response! This was the fix.

Post Reply

Return to “Ask for Help (v1)”