如果未处理,需要重新发送密钥吗?

Need to resend key if not processed?

我有我的第一个 AHK 脚本 运行,拦截功能键并将它们转换为媒体键(仅当启用 ScrollLock 时)。效果很好。

但是,如果未启用 ScrollLock,那么我希望 AHK 仅将密钥传递给 运行 应用程序。所以我添加了一个 else 子句来重新发送密钥(见下文),但是一旦我按下 F3,这会导致每秒 70 次以上的击键。好像我在循环中。

这段代码有什么问题?

F3::
  if GetKeyState("Scrolllock", "T") {
    Send {Volume_Up}
    SoundBeep 1000, 50
    SoundBeep 1100, 50
  } else {
    SendInput, {F3}
  }
return
$F3::
  if GetKeyState("Scrolllock", "T") {
    Send {Volume_Up}
    SoundBeep 1000, 50
    SoundBeep 1100, 50
  } else {
    SendInput, {F3}
  }
return

您遇到的问题是您的 Send 命令重新触发了它所属的热键,这可以通过使用 $ 修饰符来防止。

来自docs

This is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise cause it to trigger itself. The $ prefix forces the keyboard hook to be used to implement this hotkey, which as a side-effect prevents the Send command from triggering it. The $ prefix is equivalent to having specified #UseHook somewhere above the definition of this hotkey.

假设您没有更复杂的脚本,caveats of #If 可能会搞砸,我建议对任何像这样的重映射使用以下方法。

#If, GetKeyState("ScrollLock", "T")
F3::
    SendInput, {Volume_Up}
    SoundBeep, 1000, 50
    SoundBeep, 1100, 50
return
#If

这样,当滚动锁定未打开时,F3 将保持其原始功能。

(发送F3与实际使用的键不一样,一些程序可能无法识别模拟击键,特别是如果它使用直接输入,你也失去了按住F3的功能)