Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Difference between IfWinActive and #IfWinActive


  • Please log in to reply
7 replies to this topic
sam.reckoner
  • Members
  • 51 posts
  • Last active: Apr 06 2012 02:20 AM
  • Joined: 11 Oct 2007
Can somebody please explain the difference between IfWinActive and #IfWinActive? I have a script where a define a number of hotkeys that are dependent on the context of the particular active window. The trick is that I need to use different SetTitleMatchMode's for each case since the window titles are tricky. Thus, as far as I understand documentation, I should use #IfWinActive, but I don't know how SetTitleMatchMode plays into this and I don't understand the difference between IfWinActive and #IfWinActive. For instance, the following code seems to work but I don't know why!

#IfWinActive, Console2
{
   ^9:: SendPlay, (){Left}
   ^[:: SendPlay, []{Left}
   ^u:: SendPlay, ^+{Home}
   ^k:: SendPlay, ^+{End}
}
#IfWinActive

SetTitleMatchMode 3
IfWinActive, Find and
{    
   :R:*h::*Household
   Return
}


In particular, why shouldn't I use #IfWinActive, Find and in the last block, even though I know that doesn't work.

thanks in advance. Any help appreciated.

rtcvb32
  • Members
  • 542 posts
  • Last active: Apr 04 2012 03:59 AM
  • Joined: 17 Feb 2008
#IfWinActive lets all following hotkeys only be active for different windows.

;might need titlemode 2
#ifWinActive notepad
a:: send notepad

#ifWinActive MSWord
a:: send Word!

ifWinActive is a logical check.

;hotkey always fires
a::
ifWinActive notepad
{
 ;specific lines only when active window is true
  send Notepad...
}
ifWinActive MSWord
{
  send word...
}


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
In your example, the {} after #ifwinactive aren't needed.

In the following example, F1 and F2 hotkeys are ignored if the specified window is not active.
It is as if they aren't in the script.

In the case of F4, it is always in the script, and it will always increment the variable 'b'.
This is true, even if neither window is active.

The window fred has a group of two hotkeys assigned to it.
Arranging the '#if' in this way makes it easier to add hotkeys for new programs by just adding the block for the new window.

But, if you needed the same F2 function for george, you have to add the appropriate lines with the block "#ifwinactive, george"

For the F4 example, adding hotkey for a new window requires adding a new test "ifwinactive, fred" for each hotkey that is added.

So you have to make the decision, which do you want?
1. AHk 'sees' a hotkey was pressed and then tests which window is active and runs the code in that {}
If the correct window is NOT active, the hotkey is disabled for all other windows.

2. AHk 'sees' the window and only allows the relevant hotkeys.
If the relevant window is not active, the hotkey is 'turned off' for this script, but still allows hotkeys in othe programs to work.

#ifwinactive, fred
f1:
  tooltip hi fred
return

f2:
  ; whatever you want here
return

#ifwinactive, george
f1:
  tooltip hi george
return
#ifwinactive



f4:
; f4 will always do this line, even if neither window is active
b=b+1
ifwinactive, fred
{
  tooltip hi fred
}
ifwinactive, george
{
  tooltip hi george
}
return


Ohnitiel
  • Members
  • 755 posts
  • Last active: Sep 08 2016 06:26 PM
  • Joined: 25 Aug 2011
I think of "#If" as a simplified way of doing this:

F1::
IfWinActive MyGui
     Send Hello
else
     return

"#If" is also more limited since you can only use for hotkeys or hotstrings, and it cannot be used inside threads.

sam.reckoner
  • Members
  • 51 posts
  • Last active: Apr 06 2012 02:20 AM
  • Joined: 11 Oct 2007

In your example, the {} after #ifwinactive aren't needed.

...


Thank you for your excellent explanation. This should be in the main documentation. One more thing, using either #Ifwinactive or Ifwinactive, does settitlematchmode affect these from that line forward in the script? In other words, Is the following correct?

SetTitleMatchMode 2
#Ifwinactive ; uses SetTitleMatchMode 2
.. some code ..
IfWinActive ; uses SetTitleMatchMode 2

SetTitleMatchMode 3
#Ifwinactive ; uses SetTitleMatchMode 3
.. some code ..
IfWinActive ; uses SetTitleMatchMode 3

thanks!

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

In your example, the {} after #ifwinactive aren't needed.
...

Thank you for your excellent explanation. This should be in the main documentation.

<!-- m -->http://www.autohotke...mands/Block.htm<!-- m -->

A pair of braces denotes a block. Blocks are typically used with functions, Else, Loop, While-loop, and IF-commands.



:!: I have to qualify my answer to the questions about your example.
It depends if "SetTitleMatchMode 2" is in the Auto-execute Section of the script.

The Top of the Script (the Auto-execute Section)
<!-- m -->http://www.autohotke...cripts.htm#auto<!-- m -->

<!-- m -->http://www.autohotke... ... Active.htm<!-- m -->

The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode as set in the auto-execute section).

If that line is in that section, then the answers are: yes, yes, no, no

Btw, SetTitleMatchMode would affect later instances of the "IfWinActive ???" command

<!-- m -->http://www.autohotke... ... chMode.htm<!-- m -->

This command affects the behavior of all windowing commands, e.g. IfWinExist and WinActivate.


I don't not know how the use of "SetTitleMatchMode" within the body of one hotkey might affect the "IfWinActive ???" command within another hotkey.

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
SetTitleMatchMode 2

WinGetActiveTitle Title

IfWinActive % Title

	Msgbox 1

WinSetTitle %Title%, , Toto %Title%

IfWinActive %Title%

	Msgbox 2

SetTitleMatchMode 1

IfWinActive %Title%

	Msgbox 3

SetTitleMatchMode 3

IfWinActive Toto %Title%

	Msgbox 4


sam.reckoner
  • Members
  • 51 posts
  • Last active: Apr 06 2012 02:20 AM
  • Joined: 11 Oct 2007

SetTitleMatchMode 2
WinGetActiveTitle Title
...


Yes, this works great as long as you are using IfWinActive instead of #IfWinactive. I think the bottom line is that if you plan to use #IfWinactive, then you should have only one SetTitleMatchMode in your script because multiple SetTitleMatchMode won't work otherwise.

Thanks again.