英文:
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.
 - If Notepad is not running, I hope F2 can do its original action, for example, pressing F2 in an Explorer window will start renaming the currently highlighted file.
 
Writing Send {F2} is not correct because it will trigger my own F2:: ... action recursively.
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.
英文:
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.
 - 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.
 
Writing Send {F2} is not correct, because it will trigger my own F2:: ... action recursively.
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.
答案1
得分: 2
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()
$F2:: send_f2_withcond()
send_f2_withcond() {
if WinExist("ahk_class Notepad")
ControlSend, Edit1, % A_Now "`r", "ahk_class Notepad"
else
Send {F2}
}
send_f2_withcond() {
if WinExist("ahk_class Notepad")
ControlSend, Edit1, % A_Now "`r", "ahk_class Notepad"
else
Send {F2}
}
英文:
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.
$F2:: send_f2_withcond()
send_f2_withcond() {
	if WinExist("ahk_class Notepad")
		ControlSend, Edit1, % A_Now "`r", % "ahk_class Notepad"
	else   
		Send {F2}
}
答案2
得分: 1
你可以使用 #IfWinExist 来大大简化这个脚本。
#IfWinExist, ahk_class Notepad
F2::ControlSend Edit1, % A_Now " `r ", % "ahk_class Notepad"
#IfWinExist
英文:
You can greatly simplify this script by using #IfWinExist.
#IfWinExist, ahk_class Notepad
F2::ControlSend Edit1, % A_Now "`r", % "ahk_class Notepad"
#IfWinExist
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论