Hi
I am new to Hammerspoon, so sorry if this is too basic.
I am trying to map a key to another (completely).
I want the target key to behave just like the original one.
I tried something like the following code (see below).
While it works for the key without modifiers, the shift modifier seems to be lost.
I suppose that's because we are sending a char, not a key code, but I can't really make it work.
Any suggestion?
Thanks in advance.
-- Key changes
local ptSwapKeys = {
["\\"] = "~", -- send the physical key 'grave' (the key that normally has `~`)
}
local ptKeySwapper = nil
local function enablePtSwapper()
if ptKeySwapper then return end
print("Enable")
ptKeySwapper =
hs.eventtap.new({hs.eventtap.event.types.keyDown},
function(e)
local keyCode = e:getKeyCode()
local mods = e:getFlags()
local sampleChar = hs.keycodes.map[keyCode]
if sampleChar == nil then
return false
end
local mappedChar = ptSwapKeys[sampleChar]
if mappedChar == nil then
return false
end
local mappedKeyCode = hs.keycodes.map[mappedChar]
if mappedKeyCode then
hs.eventtap.keyStroke(mods, mappedKeyCode, 0)
return true
end
return false
end)
ptKeySwapper:start()
end