Skip to content

Instantly share code, notes, and snippets.

@dragod812
Last active August 8, 2022 02:05
Show Gist options
  • Save dragod812/fee8f5d4e3a24cf99ccf5e34d7f57d53 to your computer and use it in GitHub Desktop.
Save dragod812/fee8f5d4e3a24cf99ccf5e34d7f57d53 to your computer and use it in GitHub Desktop.
Hyper Key configuration for MacOS
-- HyperKey Setup
-- install carbiner and hammerspoon
-- map the capslock key to F18, map the escape key to capslock
-- paste this document in hammerspoon config in ~/.hammerspoon/init.lua
-- A variable for the Hyper Mode
local k = hs.hotkey.modal.new({}, "F17")
-- VimMode Support
-- Install vim-mode -> Run the following commands in a terminal
-- mkdir -p ~/.hammerspoon/Spoons
-- git clone https://github.com/dbalatero/VimMode.spoon ~/.hammerspoon/Spoons/VimMode.spoon
-- press f and j together to enter into vim mode anywhere
-- press i to go into insert mode.
-- press v to go into visual mode.
local VimMode = hs.loadSpoon("VimMode")
local vim = VimMode:new()
vim:disableForApp("Code"):disableForApp("iTerm"):disableForApp("zoom.us"):disableForApp("GoLand"):enterWithSequence(
"df"
)
-- vim:useFallbackMode('Google Chrome')
-- HyperKey Key map
local key_map = {
["-"] = {{"ctrl"}, "left"},
["="] = {{"ctrl"}, "right"},
-- { {'cmd', 'alt', 'shift', 'ctrl'}, 'key' }
["space"] = {{"cmd", "alt", "shift", "ctrl"}, "space"},
["tab"] = {{"cmd", "alt", "shift", "ctrl"}, "`"},
["return"] = {{"cmd", "alt", "shift", "ctrl"}, "return"},
["c"] = {{"cmd", "alt", "shift", "ctrl"}, "c"},
["f"] = {{"cmd", "alt", "shift", "ctrl"}, "f"},
["g"] = {{"cmd", "alt", "shift", "ctrl"}, "g"},
["n"] = {{"cmd", "alt", "shift", "ctrl"}, "n"},
["m"] = {{"cmd", "alt", "shift", "ctrl"}, "m"},
["t"] = {{"cmd", "alt", "shift", "ctrl"}, "t"},
["v"] = {{"cmd", "alt", "shift", "ctrl"}, "v"},
["z"] = {{"cmd", "alt", "shift", "ctrl"}, "z"},
["x"] = {{"cmd", "alt", "shift", "ctrl"}, "x"},
-- { {'cmd', 'shift'}, 'key' }
["a"] = {{"cmd", "shift"}, "a"},
["["] = {{"cmd", "shift"}, "["},
["]"] = {{"cmd", "shift"}, "]"},
-- { {'cmd'}, 'key' }
["s"] = {{"cmd"}, "`"},
-- Direction keys
["h"] = {{}, "left", {{"alt"}, {"shift"}, {"cmd"}, {"alt", "shift"}, {"cmd", "shift"}}},
["j"] = {{}, "down", {{"alt"}, {"shift"}, {"cmd"}, {"alt", "shift"}, {"cmd", "shift"}}},
["k"] = {{}, "up", {{"alt"}, {"shift"}, {"cmd"}, {"alt", "shift"}, {"cmd", "shift"}}},
["l"] = {{}, "right", {{"alt"}, {"shift"}, {"cmd"}, {"alt", "shift"}, {"cmd", "shift"}}},
-- custom
["delete"] = {{"alt"}, "delete"},
["0"] = {{"cmd"}, "left", {"shift"}},
["4"] = {{"cmd"}, "right", {"shift"}},
["b"] = {{"alt"}, "left"},
["e"] = {{"alt"}, "right"},
["w"] = {{"ctrl"}, "w"},
["o"] = {{}, "end", nil, {{}, "return"}},
-- { {'ctrl' + 'cmd' }, 'key'}
["q"] = {{"ctrl", "cmd"}, "q"},
-- { {'ctrl'}, 'key'}
["d"] = {{"ctrl"}, "d"},
["u"] = {{"ctrl"}, "u"},
[","] = {{"cmd", "shift"}, "["},
["."] = {{"cmd", "shift"}, "]"}
}
-- sends a key event with all modifiers
-- bool -> string -> void -> side effect
local hyper = function(isdown)
return function(key, mods)
return function()
k.triggered = true
if mods == nil then
mods = key_map[key][1]
end
local event = require("hs.eventtap").event
event.newKeyEvent(mods, key_map[key][2], isdown):post()
-- hs.eventtap.event.newKeyEvent( mods, key_map[key][2], isdown):post()
if key_map[key][4] ~= nil then
event.newKeyEvent(mods, key_map[key][2], isdown):post()
-- hs.eventtap.event.newKeyEvent( key_map[key][4][1], key_map[key][4][2], isdown):post()
end
end
end
end
local hyperDown = hyper(true)
local hyperUp = hyper(false)
-- actually bind a key
local hyperBind = function(key)
k:bind({}, key, msg, hyperDown(key, nil), hyperUp(key, nil), nil)
-- bind the direction key with all the modifers that you want
if key_map[key][3] ~= nil then
local modifier_types = key_map[key][3]
for i, mods in ipairs(modifier_types) do
k:bind(mods, key, msg, hyperDown(key, mods), hyperUp(key, mods), nil)
end
end
end
-- bind all the keys in the key_map table
for key, value in pairs(key_map) do
hyperBind(key)
end
-- Enter Hyper Mode when F18 (Hyper/Capslock) is pressed
local pressedF18 = function()
k.triggered = false
k:enter()
end
-- Leave Hyper Mode when F18 (Hyper/Capslock) is pressed,
-- send ESCAPE if no other keys are pressed.
local releasedF18 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({}, "ESCAPE")
end
end
local shiftEscape = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({"shift"}, "ESCAPE")
end
end
-- Bind the Hyper key
local f18 = hs.hotkey.bind({}, "F18", pressedF18, releasedF18)
-- bind ; to ctrl+key when pressed with another key
-- map ; to F20
local l = hs.hotkey.modal.new({}, "F19")
alphabet = {
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
}
for i, ch in ipairs(alphabet) do
l:bind(
{},
ch,
nil,
function()
l.triggered = true
hs.eventtap.keyStroke({"ctrl"}, ch)
end
)
end
local pressedF20 = function()
l.triggered = false
l:enter()
end
local releasedF20 = function()
l:exit()
if not l.triggered then
hs.eventtap.keyStroke({}, ";")
end
end
local releasedShiftF20 = function()
l:exit()
if not l.triggered then
hs.eventtap.keyStroke({"shift"}, ";")
end
end
local releasedCmdF20 = function()
l:exit()
if not l.triggered then
hs.eventtap.keyStroke({"cmd"}, ";")
end
end
hs.hotkey.bind({}, "F20", pressedF20, releasedF20)
hs.hotkey.bind({"shift"}, "F20", pressedF20, releasedShiftF20)
hs.hotkey.bind({"cmd"}, "F20", pressedF20, releasedCmdF20)
-- bind ' to alt+key when pressed with another key
-- map ' to F16 in karabiner
local m = hs.hotkey.modal.new({}, "F15")
for i, ch in ipairs(alphabet) do
m:bind(
{},
ch,
nil,
function()
m.triggered = true
hs.eventtap.keyStroke({"alt"}, ch)
end
)
end
local pressedF16 = function()
m.triggered = false
m:enter()
end
local releasedF16 = function()
m:exit()
if not m.triggered then
hs.eventtap.keyStroke({}, "'")
end
end
local releasedShiftF16 = function()
m:exit()
if not m.triggered then
hs.eventtap.keyStroke({"shift"}, "'")
end
end
hs.hotkey.bind({}, "F16", pressedF16, releasedF16)
hs.hotkey.bind({"shift"}, "F16", pressedF16, releasedShiftF16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment