Jump to content

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

An #If (understanding?) problem ...


Best Answer SkrillexAkaCraft , 25 October 2015 - 05:38 PM

Go to the full post »


  • Please log in to reply
9 replies to this topic
Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

>                                                                                           > Edited: Notice for other newbies (like me):

>                                                                                           > Please note that my script showed below is a nonsense!

>                                                                                           > #If cannot be used in such a way!

 

 

In this demo code an action should be started by click on a gui called Cover.
If this is coded with an #If WinActive("Cover") line, the action is started correctly, of course, by clicking on Cover.

However, if there is another gui defined in this script - Other -, the same action is started by clicking on Other!!!

For me as newbie a surpising behavior.

In this demo script clicking on Cover starts a sound and a tooltip with information on active window, clicking on Other only should display a tooltip (but ...).
 

WinGet, IdExplorer, ID, ahk_class CabinetWClass

Gui Cover: New, +HwndIdCover    
Gui Cover: +Owner%IdExplorer%
Gui Cover: -dpiscale ;-Caption 0x400000     
Gui Cover: Color, green
Gui Cover: Show, x100 y100 w400 h400, Cover
WinSet, Transparent, 220, ahk_id %IdCover%

Gui Other: Add, Text, x0 y0 w350 h50 gOther
Gui Other: Show, x700 y100 w350 h50, Other

OnMessage(0x202, "OnClickUp")    

RETURN

Other:
   WinGetTitle, Window, A
   ToolTip Other clicked & no sound!`r`nCurrently active: `r`nWindow: %Window%
return

OnClickUp() {
   global
   #If  WinActive("Cover")
          WinGetTitle, Window, A
       ToolTip Cover clicked & sound!!!`r`nCurrently active: `r`nWindow: %Window%
       SoundPlay, *48
       SetTimer, RemoveToolTip, 1500
   #If
}

RemoveToolTip:
   ToolTip
   SetTimer, RemoveToolTip, Off
return

Can somebody explain the reason for this behavior?

(If I replace the #If method by  If WinActive("Cover") { ... }, there is no more this surprising effect.)



SkrillexAkaCraft
  • Members
  • 45 posts
  • Last active: Jan 04 2018 06:02 PM
  • Joined: 04 Feb 2015

for me there are no errors nor weird effects :)

 

 

i did replace #If with If i also added one new line #NoEnv



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

Thank you, SkrillexAkaCraft, but:

 

There are no errors for you BEFORE you replaced #If with if???

 

That would mean that clicking on Other for you results in a tooltip "Other clicked & no sound", and there is no sound.

???

 



SkrillexAkaCraft
  • Members
  • 45 posts
  • Last active: Jan 04 2018 06:02 PM
  • Joined: 04 Feb 2015

ok there is sound like let us say the ding from windows itself :)

 

 

i suggest you to use this AHK Studio

 

 

 

the first line should contain #NoEnv then the code u use :)

 

 

 

i can replace the #If whitout problems and the #If also works for me so both work :/



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

SkrillexAkaCraft, my question refers only to the clicking behavior with #If WinActive("Cover") as used in my demo script (not to other solutions to design clicking behavior).

 

Are you really sure that on your computer with my (unchanged) demo script you receive the tooltip "Other clicked & no sound" when you click on Other???

 

With my demo script (with or without #NoEnv) on my computer I can see that a click on Other first (i.e. on click down) results correctly in the tooltip defined in the Other label, but then surprisingly OnClickUp starts the action defined in the #If WinActive("Cover") part (sound), although Cover is not the active window.

 

Could you please answer this specific question precisely ... I hardly can believe that such a simple script would behave differently on different PCs ...



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

There is a big difference between #If and If. I recommend reading up on them. In short, #If is a positional-directive that affects hotkeys and hotstrings - any hotkeys and hotstrings that are literally placed on lines underneath #If are affected by it. If is used within hotkeys, hotstrings, labels, and other conditions.

 

Here's a quick demoscript:

SetTitleMatchMode, 2 ; makes partial title matching work
 
a::
If WinActive("Notepad")
MsgBox you pressed a
return
 
#If WinActive("Notepad") ; or you can use #IfWinActive Notepad
b::MsgBox you pressed b
c::
If WinActive("Calculator") ; or you can use IfWinActive Calculator
MsgBox you pressed c ; notice this MsgBox never happens because the hotkey starts only when Notepad is active
; but immediately checks if Calculator is active - which it can't be if Notepad is active.
; UNLESS you save a file in Notepad as "Calculator" so now the Notepad window's full title is "Calculator - Notepad" - it will partial match on both of these
return

Are you really sure that on your computer with my (unchanged) demo script you receive the tooltip "Other clicked & no sound" when you click on Other???

 

I just ran the script. The gLabel executes when you click (and hold) on the Other GUI. The OnClickUp() function executes when the mouse is released. As #if is doing nothing in this case, yes, the sound is played and the tooltip says I clicked on Cover even if I click on Other. This is expected behavior from the script itself, but not from you due to the misunderstanding of what #if does.

 

If you change it to If anyway, the tooltip and sound still play because you lack a Block associating all of the commands with the If statement.



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

So I must understand that this part in my script is a complete nonsense ...

OnClickUp() {
   global
   #If  WinActive("Cover")
        WinGetTitle, Window, A
        ToolTip Cover clicked & sound!!!`r`nCurrently active: `r`nWindow: %Window%
        SoundPlay, *48
        SetTimer, RemoveToolTip, 1500
   #If
}

And I must understand that #If can be used only to control the subsequently defined hotkeys ...

 

OK, thank you, Exaskryz!

 

Just a small additional question:

Why then the above #If part nevertheless has the effect that the sound defined in it is produced only by clicks inside the Cover gui, but not by clicks outside the Cover gui (except clicks on the Other gui, which was so surprising for me)?



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

The sound only appears when you use OnClickUp() because only the AHK script's visible elements (GUIs) can trigger the OnClickUp(). It doesn't happen for any non-AHK window. Try removing the #If WinActive("Cover") completely and see what happens when you click on non-AHK windows.



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

... oh yes ...

You are a schatz, thank you very much, Exaskryz!



SkrillexAkaCraft
  • Members
  • 45 posts
  • Last active: Jan 04 2018 06:02 PM
  • Joined: 04 Feb 2015
✓  Best Answer

yes the tooltips work on my pc as do the sound