-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyboard.lua
384 lines (343 loc) · 11.3 KB
/
keyboard.lua
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
local keyboard = {}
local knu = hs.loadSpoon("Knu")
local runtime = knu.runtime
local utils = knu.utils
-- Some of the known Japanese input source IDs:
-- com.apple.inputmethod.Kotoeri.Japanese
-- com.apple.inputmethod.Kotoeri.Japanese.FullWidthRoman
-- com.apple.inputmethod.Kotoeri.Japanese.HalfWidthKana
-- com.apple.inputmethod.Kotoeri.Japanese.Katakana
-- com.apple.inputmethod.Kotoeri.Roman
-- com.apple.keylayout.UnicodeHexInput
-- com.google.inputmethod.Japanese.FullWidthRoman
-- com.google.inputmethod.Japanese.HalfWidthKana
-- com.google.inputmethod.Japanese.Katakana
-- com.google.inputmethod.Japanese.Roman
-- com.google.inputmethod.Japanese.base
-- com.justsystems.inputmethod.atok29.Japanese
-- com.justsystems.inputmethod.atok29.Japanese.FullWidthEisu
-- com.justsystems.inputmethod.atok29.Japanese.FullWidthEisuKotei
-- com.justsystems.inputmethod.atok29.Japanese.FullWidthRoman
-- com.justsystems.inputmethod.atok29.Japanese.HalfWidthEiji
-- com.justsystems.inputmethod.atok29.Japanese.HalfWidthEisu
-- com.justsystems.inputmethod.atok29.Japanese.HalfWidthInput
-- com.justsystems.inputmethod.atok29.Japanese.HalfWidthKana
-- com.justsystems.inputmethod.atok29.Japanese.HalfWidthKanaKotei
-- com.justsystems.inputmethod.atok29.Japanese.HiraganaKotei
-- com.justsystems.inputmethod.atok29.Japanese.Katakana
-- com.justsystems.inputmethod.atok29.Japanese.KatakanaKotei
-- com.justsystems.inputmethod.atok29.Roman
-- jp.sourceforge.inputmethod.aquaskk
-- jp.sourceforge.inputmethod.aquaskk.Ascii
-- jp.sourceforge.inputmethod.aquaskk.FullWidthRoman
-- jp.sourceforge.inputmethod.aquaskk.HalfWidthKana
-- jp.sourceforge.inputmethod.aquaskk.Hiragana
-- jp.sourceforge.inputmethod.aquaskk.Katakana
-- Check if the keyboard is currently in Japanese mode
keyboard.isJapaneseMode = function (sid)
sid = sid or hs.keycodes.currentSourceID()
return sid:find("%.Roman$") == nil and
(sid:find("%.Japanese") or sid:find("%.aquaskk%.[^A]")) ~= nil
end
-- Check if the keyboard is currently in Unicode Hex Input mode
keyboard.isUnicodeHexInputMode = function (sid)
sid = sid or hs.keycodes.currentSourceID()
return sid == "com.apple.keylayout.UnicodeHexInput"
end
local fnsOnChange = {}
local prevMethod, prevLayout, prevSourceID
hs.keycodes.inputSourceChanged(function ()
local method, layout, sourceID = hs.keycodes.currentMethod(), hs.keycodes.currentLayout(), hs.keycodes.currentSourceID()
if sourceID == prevSourceID and method == prevMethod and layout == prevLayout then
return
end
for _, fn in ipairs(fnsOnChange) do
fn()
end
prevMethod, prevLayout, prevSourceID = method, layout, sourceID
end)
-- Add a input source change handler
keyboard.onChange = function (fn)
table.insert(fnsOnChange, fn)
fn()
return fn
end
-- Remove a input source change handler
keyboard.offChange = function (fn)
for i = #fnsOnChange, 1, -1 do
if fnsOnChange[i] == fn then
table.remove(fnsOnChange, i)
return
end
end
end
-- Switch the keyboard to the specified method and layout
keyboard.switch = function (method, layout)
if method then
hs.keycodes.setMethod(method)
end
if layout then
hs.keycodes.setLayout(layout)
end
end
-- A table mapping BundleID to function to paste a string to the
-- application
keyboard.pasteFunctions = {
}
local savePB = function (delay)
local data = hs.pasteboard.readAllData()
return function ()
hs.timer.doAfter(delay or 0.5, function ()
hs.pasteboard.writeAllData(data)
end)
end
end
local function defaultPaste(str)
local restorePB = savePB()
hs.pasteboard.setContents(str)
hs.eventtap.keyStroke({"cmd"}, "v")
restorePB()
end
setmetatable(keyboard.pasteFunctions, {
__index = function (_, key)
return defaultPaste
end
})
-- Paste a string to the frontmost application, saving the original
-- content of pasteboard if possible
--
-- This function should not be called more than one time from a
-- function, because it posts an event. Use hs.timer.doAfter() if you
-- need to.
keyboard.paste = function(str)
keyboard.pasteFunctions[hs.application.frontmostApplication():bundleID()](str)
end
local function pasteOrSend(str)
if str:find("[\xf0-\xfd]") then
keyboard.paste(str)
else
hs.eventtap.keyStrokes(str)
end
end
-- A table mapping BundleID to function to paste a string to the
-- application, defaulted to hs.eventtap.keyStrokes
keyboard.sendFunctions = {
["com.apple.Terminal"] = pasteOrSend,
["com.googlecode.iterm2"] = function (str)
hs.osascript.applescript(([[
tell application "iTerm2"
tell current session of current window
write text "%s" newline NO
end tell
end tell
]]):format(str:gsub("([\"\\])", function (c) return "\\" .. c end)))
end,
["org.gnu.Emacs"] = function (str)
for _, cp in utf8.codes(str) do
if cp >= 0x10000 then
-- Use C-x 8 RET <codepoint> RET
hs.eventtap.keyStrokes(("\x188\r%x\r"):format(cp))
else
hs.eventtap.keyStrokes(hs.utf8.codepointToUTF8(cp))
end
end
end,
}
-- Send a string as keyboard input to the frontmost application
--
-- It uses special methods registered in sendFunctions, and uses
-- hs.eventtap.keyStroke() for other applications.
--
-- An optional parameter fallback, defaulted to
-- hs.eventtap.keyStrokes, specifies the function to use when
-- sendFunction does not have a method for the application.
--
-- Attributed text fields do not accept emoji sent via
-- hs.eventtap.keyStrokes(), so try paste() instead.
keyboard.send = function (str, fallback)
local bundleID = hs.application.frontmostApplication():bundleID()
local fn = keyboard.sendFunctions[bundleID] or fallback or hs.eventtap.keyStrokes
fn(str)
end
local uuid = nil
local shown = nil
keyboard.alertStyle = {
textSize = 24,
strokeWidth = 0,
strokeColor = { alpha = 0.25 },
fillColor = { alpha = 0.25 },
radius = 9
}
-- Shows a given string as input mode
keyboard.showInputMode = function (s, duration)
duration = duration or 0.5
if s == shown then
return
end
if uuid then
hs.alert.closeSpecific(uuid)
end
uuid = hs.alert.show(s, keyboard.alertStyle, hs.screen.mainScreen(), duration)
shown = s
end
-- Shows the current input mode
keyboard.showCurrentInputMode = function (sid, duration)
if type(sid) == "number" then
sid, duration = nil, sid
end
sid = sid or hs.keycodes.currentSourceID()
if keyboard.isJapaneseMode(sid) then
keyboard.showInputMode("あ⃣", duration)
elseif keyboard.isUnicodeHexInputMode(sid) then
keyboard.showInputMode("U⃣", duration)
else
keyboard.showInputMode("A⃣", duration)
end
end
local modState = {
shift = {false, false},
ctrl = {false, false},
alt = {false, false},
cmd = {false, false},
fn = false
}
local modTracer = hs.eventtap.new({hs.eventtap.event.types.flagsChanged},
function (e)
local code = e:getKeyCode()
local cmod, cidx
if code == 63 then
modState.fn = e:getFlags().fn == true
return
elseif code == 56 then
cmod, cidx = "shift", 1
elseif code == 60 then
cmod, cidx = "shift", 2
elseif code == 59 then
cmod, cidx = "ctrl", 1
elseif code == 62 then
cmod, cidx = "ctrl", 2
elseif code == 58 then
cmod, cidx = "alt", 1
elseif code == 61 then
cmod, cidx = "alt", 2
elseif code == 55 then
cmod, cidx = "cmd", 1
elseif code == 54 then
cmod, cidx = "cmd", 2
else
return
end
local lr = modState[cmod]
lr[cidx] = not lr[cidx]
local flag = (e:getFlags()[cmod] == true)
if (lr[1] or lr[2]) ~= flag then
-- Fix unsynced modifier state typically caused by other eventtaps
lr[1] = false
lr[2] = false
lr[cidx] = flag
end
end
)
-- Gets the modifier state object (table)
--
-- This always returns the same object.
keyboard.getModifierState = function ()
modTracer:start()
return modState
end
local makeCodeTable = function (keys)
local codeTable = {}
for _, key in ipairs(keys) do
local code = hs.keycodes.map[key]
if code ~= nil then
codeTable[code] = true
end
end
return codeTable
end
-- Caps-words mode is turned on when a capital letter is input holding both shift keys at the same time. In the mode, capital letters and the underscore can be input without the need for the shift key until non-alphanumeric keys are input.
--
-- capsWords([options]) -> A capsWords object that responds to :enable() and :disable()
--
-- Parameters:
--
-- - options: A table containing the following keys:
--
-- - stopKeys: An array of keys that stops the caps-words mode. Defaults to {"shift"}.
-- - shiftedKeys: An array of keys that are input with the shift modifier in the caps-words mode. Defaults to {"a".."z", "-"}.
-- - continueKeys: An array of keys that are passed through without quitting the caps-words mode. Defaults to {"_", "0".."9", "delete", "forwarddelete"}.
-- - continueCtrlKeys: An array of keys that are passed through without quitting the caps-words mode when pressed with the control key. Defaults to {"h"}.
keyboard.capsWords = function (options)
options = options or {}
local duration = options.indicatorDuration or 0.5
local capsWordsMode = false
local options = {}
local isStopCode = makeCodeTable(options.stopKeys or {
"shift"
})
local isShiftedCode = makeCodeTable(options.shiftedKeys or {
"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", "-"
})
local isContinueCode = makeCodeTable(options.continueKeys or {
"_",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"delete", "forwarddelete"
})
local isContinueCtrlCode = makeCodeTable(options.continueCtrlKeys or {
"h"
})
-- Enable the modifier tracer
keyboard.getModifierState()
local capsWordsHandler = function (e)
local code = e:getKeyCode()
local flags = e:getFlags()
if capsWordsMode then
if isStopCode[code] then
capsWordsMode = false
elseif isContinueCtrlCode[code] and flags:containExactly({"ctrl"}) then
-- ok
elseif isShiftedCode[code] then
if flags.cmd or flags.alt or flags.ctrl or flags.fn then
capsWordsMode = false
elseif not flags.shift then
return true, {
hs.eventtap.event.newKeyEvent({"shift"}, code, true),
}
end
else
capsWordsMode = isContinueCode[code]
end
if not capsWordsMode then
keyboard.showInputMode("⇪⃠", duration)
end
elseif isShiftedCode[code] and flags:containExactly({"shift"}) then
local modState = keyboard.getModifierState()
capsWordsMode = modState.shift[1] and modState.shift[2]
if capsWordsMode then
keyboard.showInputMode("⇪⃝", duration)
end
end
return false
end
local capsWordsTap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, capsWordsHandler)
return runtime.guard({
enable = function (self)
capsWordsTap:start()
end,
disable = function (self)
capsWordsTap:stop()
end,
})
end
-- Switch the Karabiner Elements profile
keyboard.switchKarabinerProfile = function (name)
hs.execute(utils.shelljoin(
"/Library/Application Support/org.pqrs/Karabiner-Elements/bin/karabiner_cli",
"--select-profile",
name
))
end
return keyboard