phil294 / AHK_X11

AutoHotkey for Linux (X11-based systems)
GNU General Public License v2.0
815 stars 15 forks source link

The IfWinActive command does not work as expected #78

Closed SN-CH closed 5 months ago

SN-CH commented 5 months ago

First and most important of all thank you very much for the job you have done. Those who are used to AHK in Windows are truly missing its equivalent in Linux world.

I have a question about the IfWinActive command. Please take a look at the following script (the goal is to be able to use the mouse wheel button to go to the previous page in browsers, for example Chrome, while keeping its normal functionality untouched in other programs).

IfWinActive, ahk_class Google-chrome
{
  MButton::
    Send, !{Left}
    Return
}
Else
{
  $MButton::
    Send, MButton
    Return
}

The script works as intended in Chrome. Unfortunately, it sends Alt-Left in any other program/window as well, thus rendering the primary clipboard useless (the else-part is redundant, of course, but was added as an experiment; its inclusion did not change anything, though).

I would appreciate a comment or advice.

SN-CH commented 5 months ago

Oh, yes, I use Lubuntu 20.04 64-bit (LXQt + Openbox), if it matters in this case.

phil294 commented 5 months ago

Hi, thanks for the kind words! The script won't work because you can't use IfWinActive for hotkey definitions, instead they are for regular control flow code. MButton:: is a hotkey definition, not a line of code to run, so If etc don't work here.

Your code is roughly equivalent to:

IfWinActive, ahk_class Google-chrome
{
    Send, !{Left}
    Return
}
Else
{
    Send, MButton
    Return
}
Return

MButton::
Send, !{Left}
Return

$MButton::
Send, MButton
Return

What you'd want is the #IfWinActive directive instead: https://www.autohotkey.com/docs/v1/lib/_IfWinActive.htm However, this link is from Windows AHK docs, the directive doesn't yet exist in AHK_X11 (but still planned some day).

So for now you'll have to write your own logic.

~MButton:: ; ~ to keep the normal functionality
IfWinNotActive, ahk_class Google-chrome
  Return
Send, !{Left}
Return

not tested but this should work

SN-CH commented 5 months ago

Yes, of course, you are right about the necessity to use directives (and this is what I use in AHK in Windows), but I noticed there were not available at the moment, so I gave a try to standard command (after all your software can not be a full clone of AHK). I appreciate your explanation why it did not work.

Your code works perfectly, thank you very much!