如何防止 hammerspoon 热键覆盖其他应用程序中的热键?

How can I prevent hammerspoon hotkeys from overriding hotkeys in other applications?

我正在寻找仅在 Google Chrome:

中可用的特定热键
hs.hotkey.bind({"cmd"}, "0", function()
  if hs.window.focusedWindow():application():name() == 'Google Chrome' then
    hs.eventtap.keyStrokes("000000000000000000")
  end
end)

此方法的问题是热键将无法在其他应用程序上使用。例如。 CMD+0 不会触发 Discord 中的 Reset Zoom 命令。

我该如何预防?

hs.hotkey API doesn't provide functionality to be able to propagate the captured keydown event. The hs.eventtap API 可以,但使用它需要监视 每个 keyDown 事件。

我会指出一个有点相关的内容 GitHub issue:

If you're wanting the key combo to do something for most applications, but not for a few specific ones, you're better off using a window filter or application watcher and enabling/disabling the hotkey(s) when the active application changes.

换句话说,对于您要实现的目标,建议您使用 hs.window.filter API 在进入应用程序时启用热键绑定,并在离开应用程序时禁用它,即类似于:

-- Create a new hotkey
local yourHotkey = hs.hotkey.new({ "cmd" }, "0", function()
    hs.eventtap.keyStrokes("000000000000000000")
end)

-- Initialize a Google Chrome window filter
local GoogleChromeWF = hs.window.filter.new("Google Chrome")

-- Subscribe to when your Google Chrome window is focused and unfocused
GoogleChromeWF
    :subscribe(hs.window.filter.windowFocused, function()
        -- Enable hotkey in Google Chrome
        yourHotkey:enable()
    end)
    :subscribe(hs.window.filter.windowUnfocused, function()
        -- Disable hotkey when focusing out of Google Chrome
        yourHotkey:disable()
    end)