forked from jbensmann/mouseless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taphold.go
149 lines (133 loc) · 3.72 KB
/
taphold.go
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
package main
import (
"math"
"time"
log "github.com/sirupsen/logrus"
)
type TapHoldHandler struct {
inChannel chan KeyboardEvent
outChannel chan KeyboardEvent
isPressed map[uint16]bool
isHoldBack bool
holdBackEvents []*KeyboardEvent // pointers so that events can be manipulated
holdBackTimeout *time.Timer
tapHoldEvent *KeyboardEvent
tapHoldBinding *TapHoldBinding
holdBackStartIsPressed map[uint16]bool
}
func NewTapHoldHandler() *TapHoldHandler {
handler := TapHoldHandler{
inChannel: make(chan KeyboardEvent, 1000),
outChannel: make(chan KeyboardEvent, 1000),
holdBackTimeout: time.NewTimer(math.MaxInt64),
isHoldBack: false,
isPressed: make(map[uint16]bool),
holdBackStartIsPressed: make(map[uint16]bool),
}
return &handler
}
func (t *TapHoldHandler) StartProcessing() {
go func() {
for {
t.process()
}
}()
}
func (t *TapHoldHandler) process() {
// wait for a key or tapHold timeout
select {
case e := <-t.inChannel:
log.Debugf("Read from queue: %v", e)
t.handleKey(e)
case <-t.holdBackTimeout.C:
log.Debugf("tapHold timed out")
t.tapHoldEvent.holdKey = true
t.isHoldBack = false
t.holdBackTimeout = time.NewTimer(math.MaxInt64)
}
// when holdBack stopped, send all holdBack keys
if !t.isHoldBack && len(t.holdBackEvents) > 0 {
for _, e := range t.holdBackEvents {
log.Debugf("Read from holdback queue: %v", e)
t.outChannel <- *e
}
t.holdBackEvents = nil
}
}
func (t *TapHoldHandler) handleKey(event KeyboardEvent) {
previousIsHoldingBack := t.isHoldBack
if event.isPress {
// tapHold key pressed?
if binding, ok := currentLayer.Bindings[event.code].(TapHoldBinding); ok {
// activate holdBack only when no other key is pressed
if len(t.isPressed) == 0 && !t.isHoldBack {
log.Debugf("Activating holdBack")
t.isHoldBack = true
t.tapHoldEvent = &event
t.tapHoldBinding = &binding
// remember all pressed keys
t.holdBackStartIsPressed = make(map[uint16]bool)
for k, v := range t.isPressed {
t.holdBackStartIsPressed[k] = v
}
// set timeout
if binding.TimeoutMs > 0 {
t.holdBackTimeout = time.NewTimer(time.Duration(binding.TimeoutMs) * time.Millisecond)
}
}
}
} else {
// tapHold key released?
if t.tapHoldBinding != nil && t.tapHoldEvent.code == event.code {
if t.isHoldBack {
// execute tap binding
t.tapHoldEvent.holdKey = false
t.isHoldBack = false
}
}
}
// if tapOnNext and another key is pressed, activate tap hold
if t.isHoldBack && event.code != t.tapHoldEvent.code {
if event.isPress {
if t.tapHoldBinding.TapOnNext {
t.isHoldBack = false
t.tapHoldEvent.holdKey = true
}
} else {
if t.tapHoldBinding.TapOnNextRelease {
if _, ok := t.holdBackStartIsPressed[event.code]; !ok {
t.isHoldBack = false
t.tapHoldEvent.holdKey = true
}
}
}
}
// send to out channel or to holdBackEvents
holdBack := t.isHoldBack || previousIsHoldingBack
// do not hold back if the key pressed before tap started to avoid unwanted key repetitions
if _, ok := t.holdBackStartIsPressed[event.code]; ok {
holdBack = false
}
if holdBack {
log.Debugf("tapHold: putting event back to queue")
t.holdBackEvents = append(t.holdBackEvents, &event)
} else {
t.outChannel <- event
}
// remember if key is pressed
if event.isPress {
t.isPressed[event.code] = true
} else {
delete(t.isPressed, event.code)
}
}
func (t *TapHoldHandler) InChannel() chan<- KeyboardEvent {
return t.inChannel
}
func (t *TapHoldHandler) OutChannel() <-chan KeyboardEvent {
return t.outChannel
}
func (t *TapHoldHandler) isKeyPressed(code uint16) bool {
pr, ok := t.isPressed[code]
return ok && pr
}