4
4
--- as the `escape` key. If the `control` key is held down and used in
5
5
--- combination with another key, then provide the normal `control` key
6
6
--- behavior.
7
-
8
- local obj = {}
7
+ local obj = {}
9
8
obj .__index = obj
10
9
11
10
-- Metadata
@@ -27,46 +26,43 @@ function obj:init()
27
26
28
27
-- Create an eventtap to run each time the modifier keys change (i.e., each
29
28
-- time a key like control, shift, option, or command is pressed or released)
30
- self .controlTap = hs .eventtap .new ({hs .eventtap .event .types .flagsChanged },
31
- function (event )
32
- local newModifiers = event :getFlags ()
29
+ self .controlTap = hs .eventtap .new ({ hs .eventtap .event .types .flagsChanged }, function (event )
30
+ local newModifiers = event :getFlags ()
33
31
34
- -- If this change to the modifier keys does not invole a *change* to the
35
- -- up/down state of the `control` key (i.e., it was up before and it's
36
- -- still up, or it was down before and it's still down), then don't take
37
- -- any action.
38
- if self .lastModifiers [' ctrl' ] == newModifiers [' ctrl' ] then
39
- return false
40
- end
32
+ -- If this flag change does not involve a *change* to the up/down state of
33
+ -- the `control` key (i.e., it was up before and it's still up, or it was
34
+ -- down before and it's still down), then don't take any action.
35
+ if self .lastModifiers [' ctrl' ] == newModifiers [' ctrl' ] then return false end
41
36
42
- -- If the `control` key has changed to the down state, then start the
43
- -- timer. If the `control` key changes to the up state before the timer
44
- -- expires, then send `escape`.
45
- if not self .lastModifiers [' ctrl' ] then
46
- self .lastModifiers = newModifiers
47
- self .sendEscape = true
48
- self .controlKeyTimer :start ()
49
- else
50
- if self .sendEscape then
51
- hs .eventtap .keyStroke ({}, ' escape' , 1 )
52
- end
53
- self .lastModifiers = newModifiers
54
- self .controlKeyTimer :stop ()
37
+ -- If the `control` key has changed to the down state, then start the
38
+ -- timer. If the `control` key changes to the up state before the timer
39
+ -- expires, then send `escape`.
40
+ if not self .lastModifiers [' ctrl' ] then
41
+ self .lastModifiers = newModifiers
42
+ self .sendEscape = true
43
+ self .controlKeyTimer :start ()
44
+ else
45
+ self .lastModifiers = newModifiers
46
+ self .controlKeyTimer :stop ()
47
+ if self .sendEscape then
48
+ -- Return an escape key event for being pressed and released.
49
+ return true , {
50
+ hs .eventtap .event .newKeyEvent ({}, ' escape' , true ),
51
+ hs .eventtap .event .newKeyEvent ({}, ' escape' , false ),
52
+ }
55
53
end
56
- return false
57
54
end
58
- )
55
+ return false
56
+ end )
59
57
60
58
-- Create an eventtap to run each time a normal key (i.e., a non-modifier key)
61
59
-- enters the down state. We only want to send `escape` if `control` is
62
60
-- pressed and released in isolation. If `control` is pressed in combination
63
61
-- with any other key, we don't want to send `escape`.
64
- self .keyDownEventTap = hs .eventtap .new ({hs .eventtap .event .types .keyDown },
65
- function (event )
66
- self .sendEscape = false
67
- return false
68
- end
69
- )
62
+ self .keyDownEventTap = hs .eventtap .new ({ hs .eventtap .event .types .keyDown }, function (event )
63
+ self .sendEscape = false
64
+ return false
65
+ end )
70
66
end
71
67
72
68
--- ControlEscape:start()
0 commit comments