-
-
Save eoranged/b05481d2a2646a84a15da88a53304d92 to your computer and use it in GitHub Desktop.
This file contains 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
local logger = hs.logger.new('taps', 'info') | |
local inspect = hs.inspect.inspect | |
-- Constants | |
local ONLY_LEFT_CMD = 1048584 | |
local ONLY_RIGHT_CMD = 1048592 | |
local ONLY_LEFT_ALT = 524320 | |
local ONLY_RIGHT_ALT = 524352 | |
local last_flags = 0 | |
-- TODO: Fn+b - toggle bluetooth | |
-- TODO: Fn+w - toggle wifi | |
local function resetFlags(event) | |
--local flags, data = event:getFlags(), event:getRawEventData() | |
-- logger:d(inspect(flags), inspect(data)) | |
last_flags = 0 | |
end | |
local bindings = {} | |
local function handleKeyUp(event) | |
-- local flags, data = event:getFlags(), event:getRawEventData() | |
-- logger:d(inspect(flags), inspect(data)) | |
last_flags = 0 | |
local mods = {} | |
for key, state in pairs(event:getFlags()) do | |
if state then table.insert(mods, key) end | |
end | |
table.sort(mods) | |
table.insert(mods, tostring(event:getKeyCode())) | |
local scmd = table.concat(mods, '+') | |
local binding = bindings[scmd] | |
if binding then | |
local handler, options = binding[1], binding[2] | |
if event:getType() == hs.eventtap.event.types.keyUp then | |
handler(event) | |
end | |
return options and options.override, {} | |
end | |
end | |
local function handleFlagsChanged(event) | |
local flags = event:getRawEventData().CGEventData.flags | |
-- logger:d('flags changed: %d', tostring(flags)) | |
if flags ~= 0 or last_flags == 0 then | |
last_flags = flags | |
return | |
end | |
if last_flags == ONLY_LEFT_CMD then | |
hs.keycodes.setLayout('U.S.') | |
-- logger:d('setting layout to U.S.') | |
elseif last_flags == ONLY_RIGHT_CMD then | |
hs.keycodes.setLayout('Russian') | |
-- logger:d('setting layout to Russian') | |
elseif last_flags == ONLY_RIGHT_ALT then | |
hs.application.launchOrFocus('/Applications/Emacs.app') | |
-- logger:d('switching to emacs') | |
end | |
end | |
local flagsTap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, handleFlagsChanged) | |
local keyUpTap = hs.eventtap.new( | |
{ | |
hs.eventtap.event.types.keyUp, | |
hs.eventtap.event.types.keyDown, | |
}, | |
handleKeyUp | |
) | |
local otherEventsTap = hs.eventtap.new( | |
{ | |
hs.eventtap.event.types.leftMouseDown, | |
hs.eventtap.event.types.rightMouseDown, | |
hs.eventtap.event.types.leftMouseUp, | |
hs.eventtap.event.types.rightMouseUp, | |
}, | |
resetFlags | |
) | |
flagsTap:start() | |
otherEventsTap:start() | |
keyUpTap:start() | |
local function bind(key, handler, options) | |
bindings[key] = {handler, options} | |
end | |
-- FIXME Does not handle propagation properly | |
bind('fn+11', function(event) | |
hs.alert('fn+b') | |
end, {override=true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment