-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimouse.lua
231 lines (203 loc) · 6.61 KB
/
vimouse.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
-- Save to ~/.hammerspoon
-- In ~/.hammerspoon/init.lua:
-- local vimouse = require('vimouse')
-- vimouse('cmd', 'm')
--
-- This sets cmd-m as the key that toggles Vi Mouse.
--
-- h/j/k/l moves the mouse cursor by 20 pixels. Holding shift moves by 100
-- pixels, and holding alt moves by 5 pixels.
--
-- Pressing <space> sends left mouse down. Releasing <space> sends left mouse
-- up. Holding <space> and pressing h/j/k/l is mouse dragging. Tapping
-- <space> quickly sends double and triple clicks. Holding ctrl sends right
-- mouse events.
--
-- <c-y> and <c-e> sends the scroll wheel event. Holding the keys will speed
-- up the scrolling.
--
-- Press <esc> or the configured toggle key to end Vi Mouse mode.
return function(tmod, tkey)
-- local overlay = nil
local log = hs.logger.new('vimouse', 'debug')
local tap = nil
local orig_coords = nil
local dragging = false
local scrolling = 0
local mousedown_time = 0
local mousepress_time = 0
local mousepress = 0
local tapmods = {['cmd']=false, ['ctrl']=false, ['alt']=false, ['shift']=false}
if type(tmod) == 'string' then
tapmods[tmod] = true
else
for _, name in ipairs(tmod) do
tapmods[name] = true
end
end
local eventTypes = hs.eventtap.event.types
local eventPropTypes = hs.eventtap.event.properties
local keycodes = hs.keycodes.map
function postEvent(et, coords, modkeys, clicks)
local e = hs.eventtap.event.newMouseEvent(et, coords, modkeys)
if clicks > 3 then
clicks = 3
end
e:setProperty(eventPropTypes.mouseEventClickState, clicks)
e:post()
end
tap = hs.eventtap.new({eventTypes.keyDown, eventTypes.keyUp}, function(event)
local code = event:getKeyCode()
local flags = event:getFlags()
local repeating = event:getProperty(eventPropTypes.keyboardEventAutorepeat)
local coords = hs.mouse.absolutePosition()
local screen = hs.mouse.getCurrentScreen()
local frame = screen:fullFrame()
local center = hs.geometry.rect(frame).center
if (code == keycodes.tab or code == keycodes['`']) and flags.cmd then
-- Window cycling
return false
end
if code == keycodes.space then
-- Mouse clicking
if repeating ~= 0 then
return true
end
local btn = 'left'
if flags.ctrl then
btn = 'right'
end
local now = hs.timer.secondsSinceEpoch()
if now - mousepress_time > hs.eventtap.doubleClickInterval() then
mousepress = 1
end
if event:getType() == eventTypes.keyUp then
dragging = false
postEvent(eventTypes[btn..'MouseUp'], coords, flags, mousepress)
elseif event:getType() == eventTypes.keyDown then
dragging = true
if now - mousedown_time <= 0.3 then
mousepress = mousepress + 1
mousepress_time = now
end
mousedown_time = hs.timer.secondsSinceEpoch()
postEvent(eventTypes[btn..'MouseDown'], coords, flags, mousepress)
end
orig_coords = coords
elseif event:getType() == eventTypes.keyDown then
local mul = 0
local step = 20
local x_delta = 0
local y_delta = 0
local scroll_y_delta = 0
local scroll_x_delta = 0
local is_tapkey = code == keycodes[tkey]
if is_tapkey == true then
for name, _ in pairs(tapmods) do
if flags[name] == nil then
flags[name] = false
end
if tapmods[name] ~= flags[name] then
is_tapkey = false
break
end
end
end
if flags.alt then
step = 5
end
if flags.shift then
mul = 10
else
mul = 1
end
if flags.ctrl then
mul_ctrl = 1
else
mul_ctrl = 0
end
if is_tapkey or code == keycodes['escape'] then
if dragging then
postEvent(eventTypes.leftMouseUp, coords, flags, 0)
end
dragging = false
-- overlay:delete()
-- overlay = nil
hs.alert('Vi Mouse Off')
tap:stop()
hs.mouse.setAbsolutePosition(orig_coords)
return true
-- elseif (code == keycodes['y'] or code == keycodes['e']) and flags.ctrl then
elseif (code == keycodes['m'] or code == keycodes['n']) then
if repeating ~= 0 then
scrolling = scrolling + 1
else
scrolling = 1
end
local scroll_mul = 1 + math.log(scrolling)
--if code == keycodes['y'] then
if code == keycodes['m'] then
scroll_y_delta = math.ceil(-1 * scroll_mul)
else
scroll_y_delta = math.floor(1 * scroll_mul)
end
log.d("Scrolling", scrolling, '-', scroll_y_delta)
elseif (code == keycodes[','] or code == keycodes['.']) then
if repeating ~= 0 then
scrolling = scrolling + 1
else
scrolling = 1
end
local scroll_mul = 1 + math.log(scrolling)
--if code == keycodes['y'] then
if code == keycodes['.'] then
scroll_x_delta = math.ceil(-1 * scroll_mul)
else
scroll_x_delta = math.floor(1 * scroll_mul)
end
elseif code == keycodes['h'] then
x_delta = step * mul * -1
y_delta = step * mul * mul_ctrl * -1
elseif code == keycodes['l'] then
x_delta = step * mul
y_delta = step * mul * mul_ctrl
elseif code == keycodes['j'] then
y_delta = step * mul
x_delta = step * mul * mul_ctrl * -1
elseif code == keycodes['k'] then
y_delta = step * mul * -1
x_delta = step * mul * mul_ctrl
elseif code == keycodes['p'] then
coords = center
end
if scroll_y_delta ~= 0 then
hs.eventtap.event.newScrollEvent({0, scroll_y_delta}, flags, 'line'):post()
end
if scroll_x_delta ~= 0 then
hs.eventtap.event.newScrollEvent({scroll_x_delta, 0}, flags, 'line'):post()
end
if x_delta or y_delta then
coords.x = coords.x + x_delta
coords.y = coords.y + y_delta
if dragging then
postEvent(eventTypes.leftMouseDragged, coords, flags, 0)
else
hs.mouse.absolutePosition(coords)
end
end
end
return true
end)
hs.hotkey.bind(tmod, tkey, nil, function(event)
local screen = hs.mouse.getCurrentScreen()
local frame = screen:fullFrame()
-- overlay = hs.drawing.rectangle(frame)
-- overlay:setFillColor({['red']=0, ['blue']=0, ['green']=0, ['alpha']=0.2})
-- overlay:setFill(true)
-- overlay:setLevel(hs.drawing.windowLevels['assistiveTechHigh'])
-- overlay:show()
hs.alert('Vi Mouse On')
orig_coords = hs.mouse.absolutePosition()
tap:start()
end)
end