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

huangapple go评论51阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月6日 12:50:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定