I made an example which blocks CMD+F (Find) in Google Chrome and remaps CMD+N (New Window) to this functionality instead.
- Setup an event tap for the key down event
- Detect when CMD+F is pressed and return
true
to block it
- Detect when CMD+N is pressed and create key events to simulate CMD+F
The trick is to check which is the frontmost (active) application while doing so. Get the bundle ID of the app, and only if its Google Chrome block CMD+F and remap CMD+N. For all other application do nothing to allow the default action.
When CMD+N is pressed a boolean (allowOverride
) is set to true to allow the very next CMD+F keypress which is simulated. Not doing this would also block it.
You should be able to rework this example for FireFox and your shortcuts.
local allowOverride = false
local events = hs.eventtap.event.types
keyboardTracker = hs.eventtap.new({ events.keyDown }, function (e)
local keyCode = e:getKeyCode()
local flags = e:getFlags()
if flags.cmd then
local application = hs.application.frontmostApplication()
if (application:bundleID() == "com.google.Chrome") then
if keyCode == hs.keycodes.map.f then
if not allowOverride then
return true
end
allowOverride = false
elseif keyCode == hs.keycodes.map.n then
hs.eventtap.event.newKeyEvent(hs.keycodes.map.cmd, true):post()
hs.eventtap.event.newKeyEvent("f", true):post()
hs.eventtap.event.newKeyEvent("f", false):post()
hs.eventtap.event.newKeyEvent(hs.keycodes.map.cmd, false):post()
allowOverride = true
return true
end
end
end
end)
keyboardTracker:start()