【问题标题】:Simple conditional pass through简单的条件通过
【发布时间】:2016-10-03 10:48:05
【问题描述】:

想知道我是否遗漏了 AHK 中的一些基本内容:有没有办法对任何鼠标或键盘操作进行简单的条件传递,这样如果条件失败,操作就会通过?

类似:

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

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

我尝试过的

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

  • 在记事本中,如果您单击文本框,您会收到一条警报并且您的单击被禁用。不过,您可以点击菜单。
  • 其他地方,点击功能正常。

我还尝试了一种使用 $ 修饰符的方法,但它不起作用(请参阅底部)。

备注

  • 脚本使用Click Down 而不是Click,否则无法进行拖动操作。这些在实际应用中是必需的,可以通过调整窗口大小在记事本中进行测试。
  • 我没有将LButton 包装在If WinActive 中,因为'LButton Up` 需要申请所有课程。为什么?当您从记事本切换到另一个应用程序时,单击向下通过了记事本测试,但单击向上失败,因为您现在处于不同的窗口中。

工作脚本

#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  

使用$的非工作脚本

此方法不起作用,因为无法再调整记事本窗口的大小:“传递”似乎是单击,而不是在需要时单击(拖动操作)。

#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

【问题讨论】:

    标签: windows mouseevent keyboard-shortcuts autohotkey key-bindings


    【解决方案1】:

    #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 与表达式结合使用,以确定任意条件的组合,即检查用户是否试图单击记事本窗口中的文本框:

    #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
    }
    

    【讨论】:

    • 首先,非常感谢您的来信。 $ 是我尝试过的事情之一,虽然它适用于普通的 if,但我无法使其适用于我在此处发布的演示脚本,如果鼠标位于某个控件上方,则禁止单击。我编辑了问题以将该非工作脚本粘贴到底部。很可能是您的想法是正确的,而我错过了一些小事!
    • @zx81 我想我现在遇到了你的问题...请查看更新。如果可行,我很乐意详细说明。
    • 太棒了。你说对了。我错过了检查函数中位置的关键思想。提升和接受。非常感谢。 :)
    猜你喜欢
    • 2013-12-25
    • 2011-10-19
    • 2012-05-21
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2013-06-23
    相关资源
    最近更新 更多