首页 > 解决方案 > Autohotkey - 在屏幕上的特定位置单击时发送键

问题描述

我正在尝试编写 AutoHotkey 代码,当在屏幕中的某个位置单击鼠标左键时发送空格键(范围如下所示)。如果在范围之外单击鼠标,则应导致默认行为。下面的代码不起作用。例如,如果我单击 ypos 小于 170 的应用程序的关闭按钮,则没有任何反应。请求帮助/输入。谢谢。

 #IfWinActive ahk_class IrfanView
        LButton::
        MouseGetPos,xpos, ypos 
        If (ypos >170 and ypos <570)
        {
        Msgbox %ypos%
        Send, {Space}
        }
        #IfWinActive
        Return

标签: autohotkey

解决方案


你几乎拥有它。

由于您正在处理鼠标左键单击,因此鼠标方面不会发生任何进一步的事情。

所以你必须像这样重新发出点击事件:

#IfWinActive ahk_class IrfanView
LButton::
MouseGetPos, xpos, ypos 
If (ypos >170 and ypos <570) {
  Msgbox %ypos%
  Send, {Space}
}
Else {
  Click  
}
Return

另请注意,我删除了您的第二个#ifWinActive

这不应该在 return 语句之前发生。

如果您只是为 IrfanView 添加热键,则根本不需要该语句。如果像我一样,你在一个大脚本中为不同的窗口/程序捕获键,那么遵循这样的模式

#IfWinActive ahk_class CabinetWClass    ;Windows Explorer
   ...keydefs here...

#IfWinActiveahk_class XLMAIN            ;Excel
   ...keydefs here...

#IfWinActive ahk_exe OUTLOOK.exe        ;Outlook
   ...keydefs here...

#IfWinActive                            ;Keys that work anywhere
   ...all other keydefs here...

推荐阅读