-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift-up-to-parens.lua
292 lines (230 loc) · 9.66 KB
/
shift-up-to-parens.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
-- Notes
--------------------------------------------------------------------------------
-- This is how Karabiner use to do it:
-- https://gist.github.com/carwin/4748951
--------------------------------------------------------------------------------
-- Shortcut to disable it.
local shiftUpToParensEnabled = true
local lastAlert = null
-- TODO(vjpr): We could make this check if SecureEntry is enabled instead.
hs.hotkey.bind({"cmd", "alt"}, "P", function()
shiftUpToParensEnabled = not shiftUpToParensEnabled
local str = shiftUpToParensEnabled and "Enabled" or "Disabled"
if lastAlert then
hs.alert.closeSpecific(lastAlert)
end
lastAlert = hs.alert(str .. " shift up to parens")
end)
--------------------------------------------------------------------------------
local fastKeyStroke = function(modifiers, character)
local event = require("hs.eventtap").event
event.newKeyEvent(modifiers, string.lower(character), true):post()
event.newKeyEvent(modifiers, string.lower(character), false):post()
end
-- local fastKeyStrokeDown = function(modifiers, character)
-- local event = require("hs.eventtap").event
-- event.newKeyEvent(modifiers, string.lower(character), true):post()
-- end
--------------------------------------------------------------------------------
-- TODO: Small problem with cmd+shift selections triggering brackets.
-- Need to differentiate left and right shift keys when shift modifier changed.
--------------------------------------------------------------------------------
local module = {}
local leftShiftDown = false
local rightShiftDown = false
--------------------------------------------------------------------------------
-- See https://github.com/Hammerspoon/hammerspoon/issues/1039
-- Which shift key was pressed and was it an up or down event.
-- e - `hs.eventtap.event.types.flagsChanged` event object
local whichShift = function(e)
local ret
local flags = e:getFlags()
local keyCode = e:getKeyCode()
-- local isDown = e:getType() == hs.eventtap.event.types.flagsChanged
local isDown = flags.shift
if keyCode == 0x38 then
if not leftShiftDown and flags.shift then -- NOTE: We check shift down in case it becomes out of sync with our local var.
leftShiftDown = true
ret = {"left", "down"}
else
leftShiftDown = false
ret = {"left", "up"}
end
-- if isDown then ret = {"left", "down"} else ret = {"left", "up"} end
elseif keyCode == 0x3C then
if not rightShiftDown and flags.shift then
rightShiftDown = true
ret = {"right", "down"}
else
rightShiftDown = false
ret = {"right", "up"}
end
-- if isDown then ret = {"right", "down"} else ret = {"right", "up"} end
else
ret = {}
end
return ret
end
--------------------------------------------------------------------------------
-- local leftShiftDelay = 0
-- local rightShiftDelay = 0
--------------------------------------------------------------------------------
event = require('hs.eventtap').event
--------------------------------------------------------------------------------
watchers = {}
secureEventInputEnabled = false
function handleGlobalAppEvent(name, event, app)
if event == hs.application.watcher.activated then
watchers[app:pid()] = watchApp(app)
-- On activate, we update secure event.
secureEventInputEnabled = isSecureEventInputEnabled()
print("isSecureEventInputEnabled", secureEventInputEnabled)
elseif event == hs.application.watcher.deactivated then
if watchers[app:pid()] then watchers[app:pid()]:stop() end
end
end
appsWatcher = hs.application.watcher.new(handleGlobalAppEvent)
function watchApp(app)
local watcher = app:newWatcher(function(element, event)
print("Finder application activated, role: "..element:role())
print("isApplication: "..tostring(element:isApplication()))
print("isWindow: "..tostring(element:isWindow()))
-- local mainWindow = element:mainWindow()
-- if mainWindow then
-- print("Main window: "..mainWindow:title())
-- end
secureEventInputEnabled = isSecureEventInputEnabled()
print("isSecureEventInputEnabled", secureEventInputEnabled)
end)
watcher:start({hs.uielement.watcher.focusedElementChanged})
return watcher
end
local js = "ObjC.bindFunction('IsSecureEventInputEnabled', ['bool', []]);\z
$.IsSecureEventInputEnabled();"
-- NOTE: Slow enough to notice.
isSecureEventInputEnabled = function()
local status, val, desc = hs.osascript._osascript(js, "JavaScript")
return val
-- return false -- DEBUG
end
--------------------------------------------------------------------------------
-- https://wiki.keyboardmaestro.com/Troubleshooting#Secure_Input_Mode
-- NOTE: This is slow when run in a webpage. It causes lag visible on Github issue input box.
-- local role = hs.uielement.focusedElement():role()
-- print(role, hs.inspect(hs.uielement.focusedElement()))
-- local role = "AXTextArea" -- Disabled.
-- TODO(vjpr): Remove module.leftShiftWasPressed. Use associative array for whether to ignore.
module.eventwatcher1 = hs.eventtap.new({hs.eventtap.event.types.keyUp, hs.eventtap.event.types.keyDown, hs.eventtap.event.types.flagsChanged}, function(e)
if not shiftUpToParensEnabled then return end
local flags = e:getFlags()
local shiftInfo = whichShift(e)
local shiftSide = shiftInfo[1]
local shiftState = shiftInfo[2]
-- print("shift side:", shiftSide, shiftState)
-- print("debug:", e:getKeyCode(), e:getType())
-- print("mods:", hs.inspect(hs.eventtap.checkKeyboardModifiers(true)))
-- print("debug", e:getKeyCode(), e:getType(), hs.inspect(e:getRawEventData()))
-- if (keyCode ~= 0x38 and keyCode ~= 0x3C) then
-- return nil
-- end
if shiftState == "up" and not (flags.alt or flags.cmd or flags.ctrl or flags.fn) then
-- local ev = e:getProperty()
-- secureEventInputEnabled = isSecureEventInputEnabled()
-- print("isSecureEventInputEnabled", secureEventInputEnabled)
-- If key was pressed while shift was down, then cancel.
if not module.cancelShiftModification
-- and role ~= "AXTextField" -- Prevent in text fields because password fields don't allow us to see keystrokes, only changes to modifiers which breaks our algorithm.
and not secureEventInputEnabled
then
module.shiftDown = true
if shiftSide == "left" then
event.newKeyEvent({"shift"}, "9", true):post()
event.newKeyEvent({"shift"}, "9", false):post()
else
event.newKeyEvent({"shift"}, "0", true):post()
event.newKeyEvent({"shift"}, "0", false):post()
end
end
end
-- Reset.
if not flags.shift then
-- print("reset cancelling shift")
module.cancelShiftModification = false
end
return false;
end)
module.eventwatcher2 = hs.eventtap.new({
hs.eventtap.event.types.keyUp,
hs.eventtap.event.types.keyDown,
hs.eventtap.event.types.leftMouseDown,
hs.eventtap.event.types.rightMouseDown
}, function(e)
if not shiftUpToParensEnabled then return end
local flags = e:getFlags()
local keyCode = e:getKeyCode()
local type_ = e:getType()
-- If shift is down and a key that is not a shift is pressed down then cancel the shift modification.
local keyCode = e:getKeyCode()
-- DEBUG
--print("key", keyCode, flags.shift, type_)
if (flags.shift
and keyCode ~= 0x38 and keyCode ~= 0x3C
and (type_ == hs.eventtap.event.types.keyDown or type_ == hs.eventtap.event.types.leftMouseDown) -- Sometimes the last keyup before the shift is pressed has the shift flag enabled because of the speed of typing.
and not module.shiftDown) -- Ensure it is not our generated shift event. function
then
--print("canceling shift")
module.cancelShiftModification = true
end
-- Indicate that generated shift down event finished.
if module.shiftDown then
module.shiftDown = false
end
return false;
end)
--------------------------------------------------------------------------------
init = function()
-- Key remapping
module.eventwatcher1:start()
module.eventwatcher2:start()
-- Watch for change in focused element and check whether secure input is enabled.
-- NOTE: Disabled because it caused Hammerspoon to crash. Probably a memory leak.
-- appsWatcher:start()
end
init()
-- Old
--------------------------------------------------------------------------------
-- local globalShiftState = {nil, nil}
-- local globalShiftIgnoreState = {nil, nil}
-- local foo = function()
-- if shiftState == "down" and not (flags.alt or flags.cmd or flags.ctrl or flags.fn) then
-- if shiftSide == "left" then
-- globalShiftState[1] = "down"
-- else
-- globalShiftState[2] = "up"
-- end
-- leftShiftDown = true
-- module.leftShiftShouldBeIgnored = false
-- return false;
-- end
-- if shiftState == "down"
-- and (flags.alt or flags.cmd or flags.ctrl or flags.fn)
-- and module.leftShiftWasPressed
-- then
-- module.leftShiftShouldBeIgnored = true
-- return false
-- end
-- if not flags.shift then
-- -- TODO: If one shift key is pressed before other released...only last key up event will fire. Need to track the individually.
-- if module.leftShiftWasPressed and not module.leftShiftShouldBeIgnored then
-- local keyCode = e:getKeyCode()
-- if keyCode == 0x38 then
-- fastKeyStroke({"shift"}, "9")
-- elseif keyCode == 0x3C then
-- fastKeyStroke({"shift"}, "0")
-- end
-- end
-- module.leftShiftWasPressed = false
-- module.leftShiftShouldBeIgnored = false
-- end
-- return false;
-- end