Keys mapping

4 views
Skip to first unread message

Alberto Simoes

unread,
Dec 17, 2025, 10:37:13 AM (6 days ago) Dec 17
to Hammerspoon
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



.dmg

unread,
Dec 17, 2025, 2:04:08 PM (6 days ago) Dec 17
to Alberto Simoes, Hammerspoon
if your goal is to only remap the keys, karabiner and kanata are much better alternatives than hammerspoon.

that is the consensus among hammerspoon users/maintainers.

--
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/hammerspoon/69f67ad1-ec0c-4869-bafa-8c514461abf6n%40googlegroups.com.


--
--dmg

---
Daniel M. German
http://turingmachine.org

Alberto Simões

unread,
Dec 18, 2025, 3:50:52 AM (5 days ago) Dec 18
to .dmg, Hammerspoon
Hi
Will try Kanata. Karabiner is restricted by out IT department.
Thanks
--
Alberto Simões

Alberto Simões

unread,
Dec 18, 2025, 4:30:29 AM (5 days ago) Dec 18
to .dmg, Hammerspoon
Ok, here goes my bug, it was simple.

 local keyCode  = e:getKeyCode()
 local mods = e:getFlags()
 [...]
 hs.eventtap.keyStroke(mods, mappedKeyCode, 0)
 
mods, as returned by getFlags, is not recoignized by keyStroke.
it would be a great addition

Meanwhile, solved with this aux method


local function modsToTable(flags)
   local mods = {}
   if flags.cmd then table.insert(mods, "cmd") end
   if flags.alt then table.insert(mods, "alt") end
   if flags.ctrl then table.insert(mods, "ctrl") end
   if flags.shift then table.insert(mods, "shift") end
   return mods
end


Hope this heads-up helps someone.



On Wed, Dec 17, 2025 at 7:04 PM .dmg <dmge...@gmail.com> wrote:


--
Alberto Simões
Reply all
Reply to author
Forward
0 new messages