简单的条件传递

Simple conditional pass through

想知道我是否遗漏了 AHK 中的一些基本内容:有没有一种方法可以为任何鼠标或键盘操作执行简单的条件传递,以便在条件失败时直接传递操作?

类似于:

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

给我特别麻烦的是必须通过 LButton 的情况。当左键单击是拖动操作的开始时,"cleanest" 技术似乎失败了(请参阅底部的非工作脚本)。我有一个解决方法,但它很冗长。

我试过的

我有一个可行的解决方案,但它相当冗长。下面,为了演示目的,我将其改编为一个简单的记事本示例。演示脚本的作用:

我也尝试过使用 $ 修饰符的方法无效(请参阅底部)。

注释

工作脚本

#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  

非工作脚本使用 $

此方法无效,因为无法再调整记事本 window 的大小:"pass-through" 似乎是单击,而不是在需要时单击(拖动操作)。

#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

#If... 指令用于使热键仅在特定条件下触发。这当然意味着:如果不满足给定的条件,则不会调用热键例程并且键会通过(并且它们会像 native 键事件那样通过)。

你的例子:

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

可以简单地写成:

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

更具体到您的问题,您还可以将 #If 与表达式结合使用以确定任意条件的组合,即检查用户是否试图点击进入文本框记事本 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
}