3

Wondering if I might be missing something basic in AHK: Is there a way to do a simple conditional pass-through for any mouse or keyboard action, so that if the conditions fail, the action just passes through?

Something like:

LButton::
if(WinActive("ahk_class Notepad")) {
    ; Do something cool
    }
else { 
     ; Pass through
     }

What is giving me particular trouble is a case where a LButton must be passed through. The "cleanest" technique seems to fail (see the non-working script at the bottom) when the left click is the start of a dragging action. I have a workaround, but it is verbose.

What I have tried

I have a working solution, but it's rather verbose. Below, I've adapted it to a simple Notepad example for demo purposes. What the demo script does:

  • In Notepad, if you click in the text box, you get an alert and your click is disabled. However, you can click menus.
  • Everywhere else, clicking functions normally.

I have also tried an approach with the $ modifier that is not working (please see at the bottom).

Notes

  • The script uses Click Down rather than Click because otherwise drag actions are impossible. These are needed in the actual app and can be tested in Notepad by resizing the window.
  • I didn't wrap LButton in a If WinActive because 'LButton Up` needs to apply for all classes. Why? When you shift from Notepad to another app, the click down passes the Notepad test, but the click up fails as you're now in a different window.

Working Script

#NoEnv  ; Recommended for all scripts. Avoids checking empty variables to see if they are environment variables.

LButton::
if(WinActive("ahk_class Notepad")) {
    MouseGetPos, x, y, ovw, control
    if(control = "Edit1") {
      SplashTextOn 300, 100, AutoHotkey Message, You can play with the menus`,`nbut not the text box. 
      Sleep 3000 
      SplashTextOff
      }
   else { ; pass the click through
        ; The reason we Click down is that a plain Click doesn't work
        ; for actions where hold the button down in order to drag.
        Click down
       }
   }
 else { 
      Click down
      }
Return

; The LButton section holds the down state, so we need to allow it Up
LButton Up::Click up  

Non-Working Script using $

This approach is not working because the Notepad window can no longer be resized: the "pass-through" seems to click, rather than clicking down when that is the need (dragging action).

#NoEnv  ; Recommended for all scripts. Avoids checking empty variables to see if they are environment variables.
#IfWinActive ahk_class Notepad
$LButton::
MouseGetPos, x, y, ovw, control
    if(control = "Edit1") {
        SplashTextOn 300, 100, AutoHotkey Message, You can play with the menus`,`nbut not the text box. 
        Sleep 3000 
        SplashTextOff
      }
     else {  ; pass the click through
             Send {LButton}
          }
Return
#IfWinActive

1 Answer 1

3

#If... directives are used to make a hotkey only trigger under certain conditions. This of course means: If the given condition is not met, the hotkey routine will not be called and the keys will pass through (and they will pass through like native key events at that).

Your example:

; $ prevents the hotkey from triggering itself
$LButton::
  if(WinActive("ahk_class Notepad")) {
      ; Do something cool
  } else { 
      ; Pass through
      Send, LButton
  }
return

Can easily be written as:

#IfWinActive, ahk_class Notepad
LButton::
   ; Only the cool stuff goes here
return
#IfWinActive

More specific to your problem, you can also use #If in conjunction with an expression in order to determine a combination of arbritrary conditions, namely to check if the user is trying to click into the text box in a Notepad window:

#If WinActive("ahk_class Notepad") && MouseOver() = "Edit1"
LButton::
    SplashTextOn 300, 100, AutoHotkey Message, You can play with the menus`,`nbut not the text box. 
    Sleep 3000 
    SplashTextOff
Return
#If

MouseOver() {
    MouseGetPos, , , , curControl
    return curControl
}
3
  • First off, thank you very much for writing. $ is one of the things I tried, and while it works for a plain if, I wasn't able to make it work for my demo script posted here, which forbids clicking if the mouse is over a certain control. I edited the question to paste that non-working script at the bottom. It could very well be that you have the right idea and that I missed some little thing!
    – zx81
    Oct 4, 2016 at 14:52
  • @zx81 I think I got your problem now...please check out the update. If it works, I'd be happy to elaborate.
    – MCL
    Oct 4, 2016 at 18:53
  • If "Do something cool" is "ControlSend ,,^c, Notepad,,ahk_exe eval_prog". The "if" is necessary because otherwise the keys are send always to Notepad even in other programs.
    – marli
    Sep 23, 2022 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.