Skip to content

Commit 01a7a5b

Browse files
committed
Adopt alternative impl for mapping caps lock to ctrl/esc
With this implementation, we need way less code than what was previously used in `better-caps-lock.lua`, and we won't need all these manual mappings anymore: https://github.com/jasonrudolph/keyboard/blob/7e12648/hammerspoon/better-caps-lock.lua#L48-L88 Shout out to @arbelt and @jasoncodes for this implementation: - https://gist.github.com/arbelt/b91e1f38a0880afb316dd5b5732759f1 - https://github.com/jasoncodes/dotfiles/blob/ac9f3ac/hammerspoon/control_escape.lua 🍻
1 parent e09a40e commit 01a7a5b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

hammerspoon/control-escape.lua

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Credit for this implementation goes to @arbelt and @jasoncodes 🙇⚡️😻
2+
--
3+
-- https://gist.github.com/arbelt/b91e1f38a0880afb316dd5b5732759f1
4+
-- https://github.com/jasoncodes/dotfiles/blob/ac9f3ac/hammerspoon/control_escape.lua
5+
6+
send_escape = false
7+
last_mods = {}
8+
9+
control_key_handler = function()
10+
send_escape = false
11+
end
12+
13+
control_key_timer = hs.timer.delayed.new(0.15, control_key_handler)
14+
15+
control_handler = function(evt)
16+
local new_mods = evt:getFlags()
17+
if last_mods["ctrl"] == new_mods["ctrl"] then
18+
return false
19+
end
20+
if not last_mods["ctrl"] then
21+
last_mods = new_mods
22+
send_escape = true
23+
control_key_timer:start()
24+
else
25+
if send_escape then
26+
hs.eventtap.keyStroke({}, "ESCAPE")
27+
end
28+
last_mods = new_mods
29+
control_key_timer:stop()
30+
end
31+
return false
32+
end
33+
34+
control_tap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, control_handler)
35+
control_tap:start()
36+
37+
other_handler = function(evt)
38+
send_escape = false
39+
return false
40+
end
41+
42+
other_tap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, other_handler)
43+
other_tap:start()

hammerspoon/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ keyUpDown = function(modifiers, key)
66
hs.eventtap.event.newKeyEvent(modifiers, key, false):post()
77
end
88

9-
require('better-caps-lock')
9+
require('control-escape')
1010
require('delete-words')
1111
require('hyper')
1212
require('super')

karabiner/karabiner.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"name": "Default profile",
2020
"selected": true,
2121
"simple_modifications": {
22-
"caps_lock": "f19",
22+
"caps_lock": "left_control",
2323
"right_option": "f17"
2424
},
2525
"virtual_hid_keyboard": {

0 commit comments

Comments
 (0)