【问题标题】:How can I prevent hammerspoon hotkeys from overriding hotkeys in other applications?如何防止hammerspoon 热键覆盖其他应用程序中的热键?
【发布时间】:2020-12-26 22:29:24
【问题描述】:

我正在寻找仅在 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 命令。

我怎样才能防止这种情况发生?

【问题讨论】:

    标签: hammerspoon


    【解决方案1】:

    hs.hotkey API 不提供能够传播捕获的 keydown 事件的功能。 hs.eventtap API 可以,但使用它会涉及观察每个 keyDown 事件。

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

    如果您希望组合键对大多数应用程序执行某些操作,而不是针对少数特定应用程序,则最好使用窗口过滤器或应用程序观察器并在活动时启用/禁用热键应用程序更改。

    换句话说,对于你想要达到的效果,建议你使用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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 2011-05-10
      • 2015-07-20
      • 1970-01-01
      • 2010-10-14
      • 2011-01-06
      • 2013-12-08
      • 2021-10-29
      相关资源
      最近更新 更多