我正在考虑让某个热键只在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不会在不一致时触发Reset Zoom命令。
我怎样才能防止这种情况呢?
发布于 2020-09-28 12:02:08
keydown API不提供能够传播捕获的hs.hotkey事件的功能。hs.eventtap应用程序接口是这样做的,但是使用它将涉及到监视每个keyDown事件。
我将指出在某种程度上相关的GitHub issue中提到的内容
如果你想让组合键为大多数应用程序做一些事情,而不是为一些特定的应用程序做一些事情,你最好使用窗口过滤器或应用程序监视器,并在活动应用程序发生变化时启用/禁用热键。
也就是说,对于您想要达到的效果,建议您在进入应用时使用hs.window.filter接口开启热键绑定,在离开应用时关闭热键绑定,例如:
-- 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)https://stackoverflow.com/questions/63795560
复制相似问题