Last active
August 30, 2019 08:11
-
-
Save heyJordanParker/4cb661578f7ebfb4918a9046a6badd14 to your computer and use it in GitHub Desktop.
CapsLock Hammerspoon Config
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
| hk = hs.hotkey.modal.new({}, "F18") | |
| -- Reload config | |
| ofun = function() | |
| hs.reload() | |
| hs.alert.show("Config loaded") | |
| hk.triggered = true | |
| end | |
| -- Turn this off while not developing | |
| -- hk:bind({}, '\\', nil, ofun) | |
| -- Helper | |
| function dump(o) | |
| if type(o) == 'table' then | |
| local s = '{ ' | |
| for k,v in pairs(o) do | |
| if type(k) ~= 'number' then k = '"'..k..'"' end | |
| s = s .. '['..k..'] = ' .. dump(v) .. ',' | |
| end | |
| return s .. '} ' | |
| else | |
| return tostring(o) | |
| end | |
| end | |
| function clone (t) -- deep-copy a table | |
| if type(t) ~= "table" then return t end | |
| local meta = getmetatable(t) | |
| local target = {} | |
| for k, v in pairs(t) do | |
| if type(v) == "table" then | |
| target[k] = clone(v) | |
| else | |
| target[k] = v | |
| end | |
| end | |
| setmetatable(target, meta) | |
| return target | |
| end | |
| function inTable(tbl, item) | |
| for key, value in pairs(tbl) do | |
| if value == item then return key end | |
| end | |
| return false | |
| end | |
| function bit(p) | |
| return 2 ^ (p - 1) -- 1-based indexing | |
| end | |
| -- Typical call: if hasbit(x, bit(3)) then ... | |
| function hasbit(x, p) | |
| return x % (p + p) >= p | |
| end | |
| -- Modifiers | |
| modifiers = {'ctrl', 'shift', 'cmd', 'alt'} | |
| local function keyEvents(key, modifiers) | |
| modifiers = modifiers or {} | |
| keyDown = function() | |
| hs.eventtap.event.newKeyEvent(modifiers, key, true):post() | |
| print('key down is ' .. key .. ' modifiers are ' .. dump(modifiers)) | |
| hk.triggered = true | |
| end | |
| keyUp = function() | |
| hs.eventtap.event.newKeyEvent(modifiers, key, false):post() | |
| end | |
| keyRepeat = function() | |
| hs.eventtap.event.newKeyEvent(modifiers, key, true):setProperty(hs.eventtap.event.properties.keyboardEventAutorepeat, 1):post() | |
| end | |
| return keyDown, keyUp, keyRepeat | |
| end | |
| function bindKey(key, targetKey, targetModifiers) | |
| for i = 0, 15 do | |
| currentModifiers = clone(targetModifiers) or {} | |
| metaModifiers = {} | |
| for mod = 1, 4 do | |
| if hasbit(i, bit(mod)) then | |
| table.insert(metaModifiers, modifiers[mod]) | |
| end | |
| end | |
| for i,v in ipairs(metaModifiers) do | |
| if not inTable(currentModifiers, v) then | |
| table.insert(currentModifiers, v) | |
| end | |
| end | |
| if key == 'd' then | |
| print('Key is ' .. key .. ' mods are ' .. tostring(dump(currentModifiers)) .. ' meta mods are ' .. tostring(dump(metaModifiers))) | |
| end | |
| hk:bind(metaModifiers, key, select(1, keyEvents(targetKey, currentModifiers))) | |
| end | |
| end | |
| -- Directions | |
| bindKey('l', 'right') | |
| bindKey('j', 'left') | |
| bindKey('i', 'up') | |
| bindKey('k', 'down') | |
| -- Quick Movement | |
| bindKey('o', 'right', {'cmd'}) | |
| bindKey('u', 'left', {'cmd'}) | |
| bindKey(';', 'right', {'alt'}) | |
| bindKey('h', 'left', {'alt'}) | |
| bindKey('delete', 'forwarddelete') | |
| bindKey('d', 'd', {'cmd'}) | |
| pressedF19 = function() | |
| hk.triggered = false | |
| hk.triggerTime = hs.timer.absoluteTime(); | |
| hk:enter() | |
| end | |
| releasedF19 = function() | |
| hk:exit() | |
| -- send ESCAPE if no other keys are pressed and the key was tapped. | |
| if not hk.triggered and ((hs.timer.absoluteTime() - hk.triggerTime) / 1e+6 | |
| < 150) then | |
| hs.eventtap.keyStroke({}, 'ESCAPE') | |
| end | |
| end | |
| -- Bind the Hyper key | |
| for i = 0, 15 do | |
| metaModifiers = {} | |
| for mod = 1, 4 do | |
| if hasbit(i, bit(mod)) then | |
| table.insert(metaModifiers, modifiers[mod]) | |
| end | |
| end | |
| hs.hotkey.bind(metaModifiers, 'F19', pressedF19, releasedF19) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment