hammerspoon remap a key to alt modifier

805 Views Asked by At

I to remap the ` key so that when I hold it it behave the same way as holding the alt key. For instance holding ` and pressing tab should behave as alt+tab.

I tried this, but it doesn't work. What am I doing wrong?

hs.hotkey.bind({}, "`", function() hs.eventtap.keyStroke({"alt"},"") end)
1

There are 1 best solutions below

0
On

Create an eventtap and hook into the keyDown event. Determine the keycode for the backtick (50 for my keyboard layout) and post key events to simulate that the alt (option) key was pressed. Finally return true to surpress the original key (`).

local events = hs.eventtap.event.types
keyboardTracker = hs.eventtap.new({ events.keyDown }, function (e)
  local keyCode = e:getKeyCode()
  if keyCode == 50 then
    hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt,true):post()
    hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt,true):post()
    return true
  end
end)
keyboardTracker:start()