Skip to content

Instantly share code, notes, and snippets.

@zen0wu
Created April 14, 2017 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zen0wu/a28ab3016956db601df0bba37b75546e to your computer and use it in GitHub Desktop.
Save zen0wu/a28ab3016956db601df0bba37b75546e to your computer and use it in GitHub Desktop.
function invertTable(t)
local res = {}
for k, v in pairs(t) do
res[v] = k
end
return res
end
local capsMap = {
-- Arrow keys
i = { key = 'up' },
k = { key = 'down' },
j = { key = 'left' },
l = { key = 'right' },
-- Backtick and tilde
escape = { key = '`' },
-- Ctrl-B for tmux
b = { mods = {'ctrl'} },
}
-- Other controls
capsMap['['] = { key = 'home' }
capsMap[']'] = { key = 'end' }
capsMap[';'] = { key = 'pageup' }
capsMap["'"] = { key = 'pagedown' }
local inverseKeycodes = invertTable(hs.keycodes.map)
local et = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
local kc = event:getKeyCode()
local mapped = capsMap[inverseKeycodes[kc]]
if mapped ~= nil then
if mapped.key ~= nil then event:setKeyCode(hs.keycodes.map[mapped.key]) end
if mapped.mods ~= nil then
local mods = event:getFlags()
for _, m in pairs(mapped.mods) do
mods[m] = true
end
event:setFlags(mods)
end
return true, {event}
end
return false
end)
-- Bind F13 to trigger the AnnePro CapsLock mode
function bindsAnne(mods)
mods = mods or {}
hs.hotkey.bind(
mods,
"F13",
function () et:start() end,
function () et:stop() end
)
end
bindsAnne()
-- For ~
bindsAnne({'shift'})
-- For Cmd+`
bindsAnne({'cmd'})
-- For Cmd+Shift+Arrow
bindsAnne({'cmd', 'shift'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment