-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
465 lines (419 loc) · 17.2 KB
/
init.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
-- A helper function for printing out the contents of a table.
local inspect = require('debug/inspect')
function p(x)
print(inspect(x))
end
-- The name of my external keyboard. This is used below to make certain
-- hotkeys only apply when my external keyboard is not connected.
EXTERNAL_KEYBOARD_NAME = 'Advantage2 Keyboard'
-- The name of my external mouse. This is used below to swap the middle
-- and right mouse button events when my external mouse is connected.
EXTERNAL_MOUSE_NAME = 'Evoluent VerticalMouse C'
externalKeyboardIsConnected = false
externalMouseIsConnected = false
for _, device in pairs(hs.usb.attachedDevices()) do
-- print(device.productName)
if device.productName == EXTERNAL_KEYBOARD_NAME then
externalKeyboardIsConnected = true
elseif
device.productName == EXTERNAL_MOUSE_NAME then
externalMouseIsConnected = true
end
end
-- These constants are used in the code below to allow hotkeys to be
-- assigned using side-specific modifier keys.
ORDERED_KEY_CODES = {58, 61, 55, 54, 59, 62, 56, 60}
KEY_CODE_TO_KEY_STR = {
[58] = 'leftAlt',
[61] = 'rightAlt',
[55] = 'leftCmd',
[54] = 'rightCmd',
[59] = 'leftCtrl',
[62] = 'rightCtrl',
[56] = 'leftShift',
[60] = 'rightShift',
}
KEY_CODE_TO_MOD_TYPE = {
[58] = 'alt',
[61] = 'alt',
[55] = 'cmd',
[54] = 'cmd',
[59] = 'ctrl',
[62] = 'ctrl',
[56] = 'shift',
[60] = 'shift',
}
KEY_CODE_TO_SIBLING_KEY_CODE = {
[58] = 61,
[61] = 58,
[55] = 54,
[54] = 55,
[59] = 62,
[62] = 59,
[56] = 60,
[60] = 56,
}
-- SIDE_SPECIFIC_HOTKEYS:
-- This table is used to setup my side-specific hotkeys, the format
-- of each entry is: {fromMods, fromKey, toMods, toKey}
--
-- Note: If you are trying to map from one key to that same key
-- with a different modifier (e.g. rightCmd+a -> ctrl+a), this
-- method won't work, but you can use the workaround mentioned
-- here: https://github.com/elliotwaite/hammerspoon-config/issues/1
--
-- fromMods (string):
-- Any of the following strings, joined by plus signs ('+'). If
-- multiple are used, they must be listed in the same order as
-- they appear in this list (alphabetical by modifier name, and
-- then left before right):
-- leftAlt
-- rightAlt
-- leftCmd
-- rightCmd
-- leftCtrl
-- rightCtrl
-- leftShift
-- rightSfhit
--
-- fromKey (string):
-- Any single-character string, or the name of a keyboard key.
-- The list keyboard key names can be found here:
-- https://www.hammerspoon.org/docs/hs.keycodes.html#map
--
-- toMods (string):
-- Any of the following strings, joined by plus signs ('+').
-- Unlike `fromMods`, the order of these does not matter:
-- alt
-- cmd
-- ctrl
-- shift
-- fn
--
-- toKey (string):
-- Same format as `fromKey`.
--
SIDE_SPECIFIC_HOTKEYS = {
{'leftCmd', 'u', 'alt', 'left'},
{'leftCmd+leftShift', 'u', 'alt+shift', 'left'},
{'leftCmd', 'i', nil, 'up'},
{'leftAlt', 'i', 'cmd', 'up'},
{'leftCmd+leftShift', 'i', 'cmd+shift', 'up'},
{'leftCmd+rightShift', 'i', 'shift', 'up'},
{'leftCmd+leftShift+rightShift', 'i', 'shift', 'up'},
{'leftCmd', 'o', 'alt', 'right'},
{'leftCmd+leftShift', 'o', 'alt+shift', 'right'},
{'leftCmd', 'h', 'cmd', 'left'},
{'leftCmd+leftShift', 'h', 'cmd+shift', 'left'},
{'leftCmd', 'j', nil, 'left'},
{'leftCmd+leftShift', 'j', 'shift', 'left'},
{'leftCmd+rightShift', 'j', 'shift', 'left'},
{'leftCmd', 'k', nil, 'down'},
{'leftAlt', 'k', 'cmd', 'down'},
{'leftCmd+leftShift', 'k', 'cmd+shift', 'down'},
{'leftCmd+rightShift', 'k', 'shift', 'down'},
{'leftCmd+leftShift+rightShift', 'k', 'shift', 'down'},
{'leftCmd', 'l', nil, 'right'},
{'leftCmd+leftShift', 'l', 'shift', 'right'},
{'leftCmd+rightShift', 'l', 'shift', 'right'},
{'leftCmd', ';', 'cmd', 'right'},
{'leftCmd+leftShift', ';', 'cmd+shift', 'right'},
{'leftCmd', "'", 'cmd', 'right'},
{'leftCmd+leftShift', "'", 'cmd+shift', 'right'},
}
hotkeyGroups = {}
for _, hotkeyVals in ipairs(SIDE_SPECIFIC_HOTKEYS) do
local fromMods, fromKey, toMods, toKey = table.unpack(hotkeyVals)
local toKeyStroke = function()
hs.eventtap.keyStroke(toMods, toKey, 0)
end
local hotkey = hs.hotkey.new(fromMods, fromKey, toKeyStroke, nil, toKeyStroke)
if hotkeyGroups[fromMods] == nil then
hotkeyGroups[fromMods] = {}
end
table.insert(hotkeyGroups[fromMods], hotkey)
end
function updateEnabledHotkeys()
if curHotkeyGroup ~= nil then
for _, hotkey in ipairs(curHotkeyGroup) do
hotkey:disable()
end
end
local curModKeysStr = ''
for _, keyCode in ipairs(ORDERED_KEY_CODES) do
if modStates[keyCode] then
if curModKeysStr ~= '' then
curModKeysStr = curModKeysStr .. '+'
end
curModKeysStr = curModKeysStr .. KEY_CODE_TO_KEY_STR[keyCode]
end
end
curHotkeyGroup = hotkeyGroups[curModKeysStr]
if curHotkeyGroup ~= nil then
for _, hotkey in ipairs(curHotkeyGroup) do
hotkey:enable()
end
end
end
modStates = {}
for _, keyCode in ipairs(ORDERED_KEY_CODES) do
modStates[keyCode] = false
end
modKeyWatcher = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, function(event)
local keyCode = event:getKeyCode()
if modStates[keyCode] ~= nil then
if event:getFlags()[KEY_CODE_TO_MOD_TYPE[keyCode]] then
-- If a mod key of this type is currently pressed, we can't
-- determine if this event was a key-up or key-down event, so we
-- just toggle the `modState` value corresponding to the event's
-- key code.
modStates[keyCode] = not modStates[keyCode]
else
-- If no mod keys of this type are pressed, we know that this was
-- a key-up event, so we set the `modState` value corresponding to
-- this key code to false. We also set the `modState` value
-- corresponding to its sibling key code (e.g. the sibling of left
-- shift is right shift) to false to ensure that the state for
-- that key is correct as well. This code makes the `modState`
-- self correcting. If it ever gets in an incorrect state, which
-- could happend if some other code triggers multiple key-down
-- events for a single modifier key, the state will self correct
-- once all modifier keys of that type are released.
modStates[keyCode] = false
modStates[KEY_CODE_TO_SIBLING_KEY_CODE[keyCode]] = false
end
updateEnabledHotkeys()
end
end):start()
-- Remap [`] -> [escape] and [cmd + `] -> [`], but only when my external
-- keyboard is not connected. I use the grave accent key as my escape
-- key when using my laptop's built-in keyboard. And then when I
-- actually want to type a grave accent, I use [cmd + `].
graveAccentHotkey = hs.hotkey.new(nil, '`', function() hs.eventtap.keyStroke(nil, 'escape', 0) end)
cmdGraveAccentHotkey = hs.hotkey.new('cmd', '`', function() hs.eventtap.keyStrokes('`') end)
if not externalKeyboardIsConnected then
graveAccentHotkey:enable()
cmdGraveAccentHotkey:enable()
end
externalKeyboardWatcher = hs.usb.watcher.new(function(event)
if event.productName == EXTERNAL_KEYBOARD_NAME then
if event.eventType == 'added' then
graveAccentHotkey:disable()
cmdGraveAccentHotkey:disable()
elseif event.eventType == 'removed' then
graveAccentHotkey:enable()
cmdGraveAccentHotkey:enable()
end
end
end):start()
-- A global hotkey that activates Brave and opens a new tab. The hotkey
-- is set to [cmd + escape], but I have my caps lock key mapped to the
-- escape key using System Preferences > Keyboard > Modifier Keys, so
-- this hotkey is really for [cmd + caps lock].
hs.hotkey.bind('cmd', 'escape', function()
hs.osascript.applescriptFromFile('openNewBraveTab.applescript')
end)
-- Remapped browser hotkeys (for both Brave and Google Chrome).
browserHotkeys = {
-- Assign [cmd + 1] to toggle the developer tools.
hs.hotkey.new('cmd', '1', function() hs.eventtap.keyStroke('alt+cmd', 'i', 0) end),
-- Assign [cmd + 4] to toggle full screen mode.
hs.hotkey.new('cmd', '4', function() hs.eventtap.keyStroke('cmd+ctrl', 'f', 0) end),
}
function enableBrowserHotkeys()
for _, hotkey in ipairs(browserHotkeys) do
hotkey:enable()
end
end
function disableBrowserHotkeys()
for _, hotkey in ipairs(browserHotkeys) do
hotkey:disable()
end
end
braveWindowFilter = hs.window.filter.new({'Brave Browser', 'Google Chrome'})
braveWindowFilter:subscribe(hs.window.filter.windowFocused, enableBrowserHotkeys)
braveWindowFilter:subscribe(hs.window.filter.windowUnfocused, disableBrowserHotkeys)
if hs.window.focusedWindow() and (
hs.window.focusedWindow():application():name() == 'Brave Browser' or
hs.window.focusedWindow():application():name() == 'Google Chrome') then
-- If this script is initialized with a Brave window already in
-- focus, enable the Brave hotkeys.
enableBrowserHotkeys()
end
-- In Davinci Resolve, remap [cmd + scroll] -> [alt + scroll].
davinciResolveScrollWatcher = hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(event)
if event:getFlags():containExactly({'cmd'}) then
event:setFlags({alt = true})
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
end
end)
davinciResolveWindowFilter = hs.window.filter.new(function(win)
-- Here we use a custom function because the DaVinci Resolve window is
-- not detected for some reason when it is in full screen mode if we
-- try to use: hs.window.filter.new('DaVinci Resolve')
return win:application():name() == 'DaVinci Resolve'
end)
davinciResolveWindowFilter:subscribe(hs.window.filter.windowFocused, function()
davinciResolveScrollWatcher:start()
end)
davinciResolveWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
davinciResolveScrollWatcher:stop()
end)
-- In Sublime Text, remap [cmd + scroll] -> [scroll]. This fixes an
-- issue with Sublime Text where it ignores [cmd + scroll] events.
sublimeTextScrollWatcher = hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(event)
if event:getFlags():containExactly({'cmd'}) then
event:setFlags({cmd = false})
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
end
end)
hs.window.filter.new('Sublime Text'):subscribe(hs.window.filter.windowFocused, function()
sublimeTextScrollWatcher:start()
end)
hs.window.filter.new('Sublime Text'):subscribe(hs.window.filter.windowUnfocused, function()
sublimeTextScrollWatcher:stop()
end)
altScrollMultiple = 1000
mouseWatchers = {
-- Remap [alt + scroll up] -> [scroll to top of page] and
-- [alt + scroll down] -> [scroll to bottom of page].
hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(event)
if (
event:getFlags():containExactly({'alt'}) and
-- Any time I process or create a scroll event, I set its
-- eventSourceUserData property to 1 so that I can check for that
-- property later to make sure I don't process that scroll event
-- twice. So if eventSourceUserData is 0 (the default value), that
-- means this is a new scroll event.
event:getProperty(hs.eventtap.event.properties.eventSourceUserData) == 0
) then
event:setFlags({alt = false})
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
event:setProperty(
hs.eventtap.event.properties.scrollWheelEventDeltaAxis1,
event:getProperty(hs.eventtap.event.properties.scrollWheelEventDeltaAxis1) * altScrollMultiple
)
event:setProperty(
hs.eventtap.event.properties.scrollWheelEventFixedPtDeltaAxis1,
event:getProperty(hs.eventtap.event.properties.scrollWheelEventFixedPtDeltaAxis1) * altScrollMultiple
)
event:setProperty(
hs.eventtap.event.properties.scrollWheelEventPointDeltaAxis1,
event:getProperty(hs.eventtap.event.properties.scrollWheelEventPointDeltaAxis1) * altScrollMultiple
)
end
end),
-- I swap the middle and right mouse button events when my external
-- mouse is connected.
--
-- Note: When I convert a middle button event to a right button event,
-- it gets recaught by the right button event handler. However, this
-- doesn't happend when converting right button events to middle button
-- events (perhaps this is due to the order in which Hammerspoon
-- processes the different event types). So to prevent the right button
-- event handler from undoing the initial event conversion, I set the
-- "eventSourceUserData" property to 1 when I convert a middle button
-- event to a right button event, and then whenever I recieve a right
-- button event, I check if the "eventSourceUserData" property is 1 to
-- determine if it actually came from the middle button event handler,
-- in which case I change the "evenSourceUserData" property back to 0
-- (the default value) and let it pass through. The choice of using the
-- "eventSourceUserData" porperty was arbitrary, it was just one of the
-- event properties that didn't seem like it was being used for anything
-- else (it was always 0), so I used it to pass data between the event
-- handlers.
hs.eventtap.new({hs.eventtap.event.types.rightMouseDown}, function(event)
if event:getProperty(hs.eventtap.event.properties.eventSourceUserData) == 1 then
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 0)
return false
end
-- print('rightMouseDown')
event:setType(hs.eventtap.event.types.otherMouseDown)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 2)
end),
hs.eventtap.new({hs.eventtap.event.types.rightMouseUp}, function(event)
if event:getProperty(hs.eventtap.event.properties.eventSourceUserData) == 1 then
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 0)
return false
end
-- print('rightMouseUp')
event:setType(hs.eventtap.event.types.otherMouseUp)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 2)
end),
hs.eventtap.new({hs.eventtap.event.types.rightMouseDragged}, function(event)
if event:getProperty(hs.eventtap.event.properties.eventSourceUserData) == 1 then
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 0)
return false
end
-- print('rightMouseDragged')
event:setType(hs.eventtap.event.types.otherMouseDragged)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 2)
end),
hs.eventtap.new({hs.eventtap.event.types.otherMouseDown}, function(event)
if event:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber) == 2 then
-- print('middleMouseDown')
event:setType(hs.eventtap.event.types.rightMouseDown)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 1)
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
end
end),
hs.eventtap.new({hs.eventtap.event.types.otherMouseUp}, function(event)
if event:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber) == 2 then
-- print('middleMouseUp')
event:setType(hs.eventtap.event.types.rightMouseUp)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 1)
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
end
end),
hs.eventtap.new({hs.eventtap.event.types.otherMouseDragged}, function(event)
if event:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber) == 2 then
-- print('middleMouseDragged')
event:setType(hs.eventtap.event.types.rightMouseDragged)
event:setProperty(hs.eventtap.event.properties.mouseEventButtonNumber, 1)
event:setProperty(hs.eventtap.event.properties.eventSourceUserData, 1)
end
end),
-- I also map [mouse button 4] -> [swipe left] and [mouse button 3] -> [swipe right].
hs.eventtap.new({hs.eventtap.event.types.otherMouseDown}, function(event)
if event:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber) == 4 then
-- print('Swipe left')
require("hs.eventtap.event").newGesture("beginSwipeLeft"):post()
require("hs.eventtap.event").newGesture("endSwipeLeft"):post()
return true
elseif event:getProperty(hs.eventtap.event.properties.mouseEventButtonNumber) == 3 then
-- print('Swipe right')
require("hs.eventtap.event").newGesture("beginSwipeRight"):post()
require("hs.eventtap.event").newGesture("endSwipeRight"):post()
return true
end
end),
}
function startMouseWatchers()
for _, mouseWatcher in ipairs(mouseWatchers) do
mouseWatcher:start()
end
end
function stopMouseWatchers()
for _, mouseWatcher in ipairs(mouseWatchers) do
mouseWatcher:stop()
end
end
if externalMouseIsConnected then
startMouseWatchers()
end
externalMouseWatcher = hs.usb.watcher.new(function(event)
if event.productName == EXTERNAL_MOUSE_NAME then
if event.eventType == 'added' then
startMouseWatchers()
elseif event.eventType == 'removed' then
stopMouseWatchers()
end
end
end):start()
-- The below code automatically reloads this hammer configuration file
-- whenever a file in the ~/.hammerspoon directory is changed, and shows
-- the alert, "Config reloaded", whenever it does. I uncomment this code
-- when debugging.
-- hs.loadSpoon('ReloadConfiguration')
-- spoon.ReloadConfiguration:start()
-- hs.alert.show('Config reloaded')