Not sure how "if WinActive" works Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
BurrowBird
Posts: 30
Joined: 20 Jul 2020, 11:25

Not sure how "if WinActive" works

15 Sep 2020, 16:54

Hello. I have:

Code: Select all

~LButton & k::
if WinActive("ahk_id 0x10542")
	KeyWait, k
	Send {Backspace}
	Sleep 200
	Send {Alt}fhu
	return
which does what I want it to do, but for some reason it works in other windows as well, while I only want it to trigger inside the window with id 0x10542.
What I hoped to do was to check if the active window has that id and to only trigger when it does.
Is this not the way?

Thank you!
gregster
Posts: 5132
Joined: 30 Sep 2013, 06:48

Re: Not sure how "if WinActive" works  Topic is solved

15 Sep 2020, 17:05

A "normal" if-statement needs a block of brackets ({ }) to make more than one line conditional (the same applies to a lot of other control flow statements). That means, in your current code only the line KeyWait, k gets executed based on the result of the If-check. The rest of the lines gets executed in any case.

Now, for hotkeys and hotstrings, the use of the #If... directives is preferable instead. They allow to create context-sensitive hotkeys. But contrary to If, they work purely postional (and don't need brackets).

So, this is how it could be done:

Code: Select all

#if WinActive("ahk_id 0x10542")	; following hotkeys will be context-sensitive
~LButton & k::
	KeyWait, k
	Send {Backspace}
	Sleep 200
	Send {Alt}fhu
	return
#If			; end of context-sensitive section
The directive #IfWinactive would be an alternative. #If with an expression is more versatile, but not really needed here (in AHK v2 alpha, it would be obligatory, though).
BurrowBird
Posts: 30
Joined: 20 Jul 2020, 11:25

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:11

Ah yes, brackets, I'm a dumb-dumb. Thank you!

What would be the advantage of using #If vs the brackets?
User avatar
JoeWinograd
Posts: 1608
Joined: 10 Feb 2014, 20:00

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:12

Hi BB,

You need to learn about blocks:
https://www.autohotkey.com/docs/commands/Block.htm

The code that you posted will execute only the one instruction after the If WinActive statement in the True path because you haven't enclosed the others in braces (to make it a block). So, that code should be this:

Code: Select all

if WinActive("ahk_id 0x10542")
{
  KeyWait, k
  Send {Backspace}
  Sleep 200
  Send {Alt}fhu
}
return
Regards, Joe
BurrowBird
Posts: 30
Joined: 20 Jul 2020, 11:25

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:19

Thank you!

I actually know how to use brackets, I just had a brainfart and instead of looking for a problem with my code, I was looking for a problem with "if WinActive" instead, because I never used it before, so I wasn't sure how/if it would work. :cry:
gregster
Posts: 5132
Joined: 30 Sep 2013, 06:48

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:25

BurrowBird wrote:
15 Sep 2020, 17:11
What would be the advantage of using #If vs the brackets?
For example, you can make multiple (following) hotkeys or hotstrings context-sensitive with a single line (in this case, it is only one, but it could be 2, 5, 123 hotkeys, or more - depending on where the closing #If is located.)

Also, you can easily create multiple variants of the same hotkey, for different contexts (you could create something similar with If, though).

Code: Select all

#IfWinActive, ahk_exe Notepad.exe
a::msgbox Only in Notepad.
b::msgbox Only in Notepad.
c::msgbox A third hotkey that only works in Notepad.

#IfWinActive, ahk_id 0x10542
a::msgbox Only in ahk_id 0x10542.
c::msgbox This is not Notepad.

#If 	; or #IfWinActive
BurrowBird
Posts: 30
Joined: 20 Jul 2020, 11:25

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:36

Aha, thank you!

Related a little bit, although not my original thread question, I now have:

Code: Select all

#if WinActive, ahk_class CabinetWClass
and several hotkeys below it. But they seem to still work when the active window is something different like Chrome, Notepad etc. They do have different ahk_class though.

Can you not use class with WinActive maybe?
gregster
Posts: 5132
Joined: 30 Sep 2013, 06:48

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:39

BurrowBird wrote:
15 Sep 2020, 17:36
Aha, thank you!

Related a little bit, although not my original thread question, I now have:

Code: Select all

#if WinActive, ahk_class CabinetWClass
and several hotkeys below it. But they seem to still work when the active window is something different like Chrome, Notepad etc. They do have different ahk_class though.

Can you not use class with WinActive maybe?
You can, but the #IfWinActive directive doesn't have a space in the middle.
Only #If expression has one. #ifWinActive on the other hand, doesn't support expressions.
BurrowBird
Posts: 30
Joined: 20 Jul 2020, 11:25

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:47

Ha! Thank you.

That makes sense. Honestly, I would probably spend days trying to figure out what went wrong.
gregster
Posts: 5132
Joined: 30 Sep 2013, 06:48

Re: Not sure how "if WinActive" works

15 Sep 2020, 17:54

Well, unfortunately AHK v1.x often "fails" silently.
You need a good eye for the correct syntax. :geek: ;)

Actually, in #if WinActive, ahk_class CabinetWClass, #If will see WinActive as a variable name - and ignore the rest? I am not sure (multi-statement ? Probably.).
So, it's technically not an error :D - because a variable with the name WinActive, which could be true, would be allowed :problem: .

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, boiler, DavidP, f1ster, Google [Bot], mmmax, Rohwedder, RubbeH, W9tt9W and 68 guests