简体   繁体   English

Autohotkey:定义一个热键并将其动态传递给系统,如何?

[英]Autohotkey: Define a hotkey and passthrough it to system dynamically, how to?

I'm facing a somewhat hard question.我面临一个有点困难的问题。 Look at this script:看看这个脚本:

; Using Autohotkey 1.1.36.2

F2:: 
send_f2_withcond()
{
    if(WinExist("ahk_class Notepad"))
    {
        ControlSend Edit1, % A_Now "`r", % "ahk_class Notepad"
    }
    else
    {
        ; I hope F2 can do its original action. How to?
        
        Send {$F2} ; ??? No effect!
    }
}

My purpose:我的目的:

  • If there is Notepad running, F2 will send some text to Notepad.如果正在运行记事本,F2 将向记事本发送一些文本。
  • If Notepad is not running, I hope F2 can do it original action, for example, pressing F2 in an Explorer window will start renaming current highlighted file.如果记事本没有运行,我希望 F2 可以做它原来的动作,例如,在资源管理器 window 中按 F2 将开始重命名当前突出显示的文件。

Writing Send {F2} is not correct, because it will trigger my own F2:: ... action recursively.Send {F2}是不正确的,因为它会递归地触发我自己的F2:: ...动作。

The doc says adding a $ prefix will suppress recursive calling, BUT, Send {$F2} takes no effect(as if I totally omit this Send ), the current active application receives only F2's WM_KEYUP, no WM_KEYDOWN.文档说添加$前缀将抑制递归调用,但是, Send {$F2}无效(好像我完全省略了这个Send ),当前活动的应用程序仅接收 F2 的 WM_KEYUP,没有 WM_KEYDOWN。

The $ prefix is used only in hotkey definitions. $ 前缀仅用于热键定义。 It forces the keyboard hook to be used, which is designed to filter out keys sent by AutoHotkey scripts.它强制使用键盘钩子,该钩子旨在过滤掉 AutoHotkey 脚本发送的键。

$F2:: send_f2_withcond()

send_f2_withcond() {
    if WinExist("ahk_class Notepad")
        ControlSend, Edit1, % A_Now "`r", % "ahk_class Notepad"
    else   
        Send {F2}
}

You can greatly simplify this script by using #IfWinExist .您可以使用#IfWinExist大大简化此脚本。

#IfWinExist, ahk_class Notepad
F2::ControlSend Edit1, % A_Now "`r", % "ahk_class Notepad"
#IfWinExist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM