I'm now writing my own init.lua
on Hammerspoon, and would like to remap some of my kews to others. Specifically, I would like to switch backslash key and delete key on my macOS.
However, while the simple delete and backslash works properly, once I switch my app focus to another, type in something there, and go back to the original app, the delete key (actually backslash key as I switched it) gets longer working; instead it deletes the characters in the app before it aborts the focus (i.e. the second to last focused app). But if I type in something there and then try again to delete it, suddenly the deletion works again without any problems.
I wonder why it is suddenly not working; it might be a bug, though. Anyway here is my init.lua
to switch delete and backslash.
local VK_BACKSLASH = 0x2a
local VK_DELETE = 0x33
keyEventtap = hs.eventtap.new({
hs.eventtap.event.types.keyDown
}, function(event)
local bundleId = string.lower(hs.application.frontmostApplication():bundleID())
local keyCode = event:getKeyCode()
local flags = event:getFlags()
if keyCode == VK_DELETE then
if flagsMatches(flags, {'shift'}) then
event:setKeyCode(VK_BACKSLASH)
event:setFlags({shift=true})
else
event:setKeyCode(VK_BACKSLASH)
end
elseif keyCode == VK_BACKSLASH then
event:setKeyCode(VK_DELETE)
end
end)
keyEventtap:start()
What am I missing here...?
You must add keyUp
and flagsChanged
event to monitor the event tap. So, instead of:
keyEventtap = hs.eventtap.new({
hs.eventtap.event.types.keyDown
}, function(event)
change to the following:
keyEventtap = hs.eventtap.new({
hs.eventtap.event.types.keyDown,
hs.eventtap.event.types.flagsChanged,
hs.eventtap.event.types.keyUp
}, function(event)
At least now this is working for me.