Skip to content

Commit

Permalink
Adopt alternative impl for mapping caps lock to ctrl/esc
Browse files Browse the repository at this point in the history
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

🍻
  • Loading branch information
jasonrudolph committed Jan 2, 2017
1 parent e09a40e commit 01a7a5b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions hammerspoon/control-escape.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Credit for this implementation goes to @arbelt and @jasoncodes 🙇⚡️😻
--
-- https://gist.github.com/arbelt/b91e1f38a0880afb316dd5b5732759f1
-- https://github.com/jasoncodes/dotfiles/blob/ac9f3ac/hammerspoon/control_escape.lua

send_escape = false
last_mods = {}

control_key_handler = function()
send_escape = false
end

control_key_timer = hs.timer.delayed.new(0.15, control_key_handler)

control_handler = function(evt)
local new_mods = evt:getFlags()
if last_mods["ctrl"] == new_mods["ctrl"] then
return false
end
if not last_mods["ctrl"] then
last_mods = new_mods
send_escape = true
control_key_timer:start()
else
if send_escape then
hs.eventtap.keyStroke({}, "ESCAPE")
end
last_mods = new_mods
control_key_timer:stop()
end
return false
end

control_tap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, control_handler)
control_tap:start()

other_handler = function(evt)
send_escape = false
return false
end

other_tap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, other_handler)
other_tap:start()
2 changes: 1 addition & 1 deletion hammerspoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keyUpDown = function(modifiers, key)
hs.eventtap.event.newKeyEvent(modifiers, key, false):post()
end

require('better-caps-lock')
require('control-escape')
require('delete-words')
require('hyper')
require('super')
Expand Down
2 changes: 1 addition & 1 deletion karabiner/karabiner.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"name": "Default profile",
"selected": true,
"simple_modifications": {
"caps_lock": "f19",
"caps_lock": "left_control",
"right_option": "f17"
},
"virtual_hid_keyboard": {
Expand Down

0 comments on commit 01a7a5b

Please sign in to comment.