Trouble getting variable to work

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
veggiebike
Posts: 4
Joined: 15 Aug 2018, 10:58

Trouble getting variable to work

17 Oct 2018, 10:02

Hi! I wrote a hotkey script based on mouseclicks for myself and coworkers which works great, but one coworker has a new monitor with a higher resolution and it requires me to change the position of one of the clicks. I wanna make my script detect the screen width and if it's 1920, do one thing, if it's another do another thing. I can't seem to get it to work. When I try the code below I get the message "Width is 1920 and it wasn't equal". What's wrong with my variable or if statement? Any help would be greatly appreciated. Here's my sample code

;Testing
^g::
Var1 :=%A_ScreenWidth%
IfEqual,Var1 ,1920
{
msgbox Congrats! Resolution matches
}else{
msgbox Width is %A_ScreenWidth% and it wasn't equal
}
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Trouble getting variable to work

17 Oct 2018, 10:18

problem is ure double-dereferencing A_ScreenWidth when you do percent signs within the context of an expression ..... := %....%. this means it looks for a variable named 1920 (which is undeclared, and hence blank) and tries to fetch its value
u can fix this by replacing the expression assignment with a traditional/legacy assignment:

Code: Select all

Var1 = %A_ScreenWidth%
IfEqual,Var1 ,1920
	msgbox Congrats! Resolution matches
else
	msgbox Width is %A_ScreenWidth% and it wasn't equal
but id recommend just learning expression syntax instead:

Code: Select all

if (A_ScreenWidth == 1920)
	msgbox Congrats! Resolution matches
else
	msgbox Width is %A_ScreenWidth% and it wasn't equal
veggiebike
Posts: 4
Joined: 15 Aug 2018, 10:58

Re: Trouble getting variable to work

17 Oct 2018, 10:28

Perfect! Thank you so much!!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 161 guests