-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphotkey.lua
More file actions
230 lines (207 loc) · 5.41 KB
/
Copy pathphotkey.lua
File metadata and controls
230 lines (207 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
local photkey = {}
local keyDown = hs.eventtap.event.types.keyDown
local keyUp = hs.eventtap.event.types.keyUp
-- Only keyboard event types carry a keycode and modifier flags that
-- this module dispatches on, so only these are supported.
local supportedTypes = {
[keyDown] = true,
[keyUp] = true,
}
local knu = hs.loadSpoon("Knu")
local runtime = knu.runtime
local keyboard = knu.keyboard
-- A single eventtap is shared among all photkeys that listen for the
-- same event type. The registry maps an event type to a shared tap
-- and a table of entries keyed by keycode:
--
-- sharedTaps[type] = {
-- tap = <hs.eventtap>,
-- count = <number of enabled entries>,
-- byCode = { [code] = { entry, ... } },
-- }
--
-- where each entry is { mods = ..., modLr = ..., modState = ..., fn = ... }.
local sharedTaps = {}
-- Checks if a given entry matches the modifier flags of an event.
local matches = function (entry, flags)
if not flags:containExactly(entry.mods) then
return false
end
local modState = entry.modState
for mod, lr in pairs(entry.modLr) do
local modStateLr = 0
if modState[mod][1] then
modStateLr = modStateLr | 1
end
if modState[mod][2] then
modStateLr = modStateLr | 2
end
if modStateLr ~= lr then
return false
end
end
return true
end
local makeHandler = function (shared)
return function (e)
local entries = shared.byCode[e:getKeyCode()]
if entries == nil then
return
end
local flags = e:getFlags()
local matched = false
-- Iterate backwards so a handler may disable itself or later
-- entries for this key without disrupting the traversal.
for i = #entries, 1, -1 do
local entry = entries[i]
if matches(entry, flags) then
entry.fn(e)
matched = true
end
end
if matched then
return true
end
end
end
local getSharedTap = function (type)
local shared = sharedTaps[type]
if shared == nil then
shared = { count = 0, byCode = {} }
shared.tap = hs.eventtap.new({ type }, makeHandler(shared))
sharedTaps[type] = shared
end
return shared
end
local register = function (self)
for _, type in ipairs(self.types) do
local shared = getSharedTap(type)
local entries = shared.byCode[self.code]
if entries == nil then
entries = {}
shared.byCode[self.code] = entries
end
local found = false
for _, entry in ipairs(entries) do
if entry == self.entry then
found = true
break
end
end
if not found then
entries[#entries+1] = self.entry
shared.count = shared.count + 1
shared.tap:start()
end
end
return self
end
local unregister = function (self)
for _, type in ipairs(self.types) do
local shared = sharedTaps[type]
if shared then
local entries = shared.byCode[self.code]
if entries then
for i = #entries, 1, -1 do
if entries[i] == self.entry then
table.remove(entries, i)
shared.count = shared.count - 1
if shared.count == 0 then
shared.tap:stop()
end
end
end
if #entries == 0 then
shared.byCode[self.code] = nil
end
end
end
end
return self
end
photkey.methods = {
start = function (self)
return register(self)
end,
stop = function (self)
return unregister(self)
end,
-- Provide hotkey compatible API for convenience
enable = function (self)
return self:start()
end,
disable = function (self)
return self:stop()
end,
delete = function (self)
return runtime.unguard(self:stop())
end,
}
local meta = { __index = photkey.methods }
-- Creates a pseudo hotkey
--
-- A pseudo hotkey is implemented by using eventtap, allowing for
-- specifying pseudo modifiers listed below:
--
-- * leftshift, rightshift, leftctrl, rightctrl, leftalt, rightalt,
-- leftcmd, rightcmd and fn
--
-- The handler function is called with a key event.
--
-- The types paramter specifies the event types to capture, defaulted
-- to { hs.eventtap.event.types.keyDown }. Only keyDown and keyUp are
-- supported; any other type raises an error.
--
-- All photkeys that listen for the same event type share a single
-- eventtap, which is started only while at least one photkey for that
-- type is enabled.
photkey.new = function (pmods, key, fn, types)
types = types or { keyDown }
for _, type in ipairs(types) do
if not supportedTypes[type] then
error("unsupported event type: " .. tostring(type))
end
end
local modState = keyboard.getModifierState()
local mods = {}
local modLr = {}
for _, pmod in ipairs(pmods) do
local mod = pmod:match("left(.+)")
if mod then
modLr[mod] = (modLr[mod] or 0) | 1
else
mod = pmod:match("right(.+)")
if mod then
modLr[mod] = (modLr[mod] or 0) | 2
else
mod = pmod
end
end
if modState[mod] == nil then
error("unknown modifier: " .. pmod)
end
mods[#mods+1] = mod
end
local code
if type(key) == "number" then
code = key
else
code = hs.keycodes.map[key]
end
local entry = {
mods = mods,
modLr = modLr,
modState = modState,
fn = fn,
}
return runtime.guard(setmetatable({
types = types,
code = code,
entry = entry,
}, meta))
end
-- Shortcut for knu.photkey.new(...):start()
photkey.bind = function (...)
return photkey.new(...):start()
end
return photkey