Created
April 14, 2017 22:40
-
-
Save zen0wu/a28ab3016956db601df0bba37b75546e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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