Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hammerspoon remap a key to alt modifier

Tags:

hammerspoon

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)
like image 985
user3586940 Avatar asked Oct 12 '25 10:10

user3586940


1 Answers

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()
like image 168
Christophe Geers Avatar answered Oct 16 '25 04:10

Christophe Geers