-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeyboard.go
More file actions
328 lines (293 loc) · 7.13 KB
/
Copy pathkeyboard.go
File metadata and controls
328 lines (293 loc) · 7.13 KB
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
package main
import (
"math"
"time"
)
type KeyboardState struct {
Layout [][]KeyDef
CursorRow int
CursorCol int
ShiftActive bool
ShiftHeld bool // true when held via gamepad trigger (not auto-released)
CapsActive bool
CtrlActive bool
AltActive bool
MetaActive bool
LongPressStart time.Time
LongPressActive bool
AccentPopup *AccentPopupState
targetX float64
targetXSet bool
// Alt+Tab cycling: Alt held at OS level across multiple Tab presses
AltTabHeld bool
AltTabLastTab time.Time // last Tab press, for auto-release timeout
// Visual flash for shortcut keys
FlashCode int
FlashUntil time.Time
// Callbacks for Cfg key
OnThemeCycle func()
OnThemeCycleReverse func()
// Callbacks for live mouse sensitivity adjustment (Shift+↑/↓)
OnSensitivityUp func()
OnSensitivityDown func()
}
type AccentPopupState struct {
Accents []AccentDef
Selected int
}
func NewKeyboardState(layout [][]KeyDef) *KeyboardState {
return &KeyboardState{
Layout: layout,
CursorRow: 2,
CursorCol: 1, // land on 'q'
}
}
func (kb *KeyboardState) Navigate(dx, dy int) {
if kb.AccentPopup != nil {
newIdx := kb.AccentPopup.Selected + dx
if newIdx >= 0 && newIdx < len(kb.AccentPopup.Accents) {
kb.AccentPopup.Selected = newIdx
}
return
}
if dy != 0 {
if !kb.targetXSet {
kb.targetX = kb.keyCenterX(kb.CursorRow, kb.CursorCol)
kb.targetXSet = true
}
kb.CursorRow = (kb.CursorRow + dy + len(kb.Layout)) % len(kb.Layout)
kb.CursorCol = kb.findClosestCol(kb.CursorRow, kb.targetX)
} else if dx != 0 {
row := kb.Layout[kb.CursorRow]
kb.CursorCol = (kb.CursorCol + dx + len(row)) % len(row)
kb.targetXSet = false
}
}
func (kb *KeyboardState) keyCenterX(rowIdx, colIdx int) float64 {
x := 0.0
for i, key := range kb.Layout[rowIdx] {
if i == colIdx {
return x + key.Width/2.0
}
x += key.Width
}
return x
}
func (kb *KeyboardState) findClosestCol(rowIdx int, targetX float64) int {
bestCol := 0
bestDist := math.Inf(1)
x := 0.0
for i, key := range kb.Layout[rowIdx] {
center := x + key.Width/2.0
dist := math.Abs(center - targetX)
if dist < bestDist {
bestDist = dist
bestCol = i
}
x += key.Width
}
return bestCol
}
func (kb *KeyboardState) CurrentKey() KeyDef {
row := kb.Layout[kb.CursorRow]
col := kb.CursorCol
if col >= len(row) {
col = len(row) - 1
}
return row[col]
}
func (kb *KeyboardState) DisplayLabel(key KeyDef) string {
if (kb.ShiftActive != kb.CapsActive) && key.ShiftLabel != "" {
return key.ShiftLabel
}
return key.Label
}
func (kb *KeyboardState) PressCurrent(inj *Injector) {
if kb.AccentPopup != nil {
accent := kb.AccentPopup.Accents[kb.AccentPopup.Selected]
if inj != nil {
inj.TypeUnicode(accent.Codepoint)
}
kb.CloseAccentPopup()
return
}
key := kb.CurrentKey()
if key.IsModifier {
kb.toggleModifier(key)
return
}
// Paste/Copy button - Paste normally, Copy when shifted
if key.Label == "Paste" {
shiftOn := kb.ShiftActive != kb.CapsActive
if shiftOn {
if inj != nil {
inj.PressKey(KEY_C, []int{KEY_LEFTCTRL})
}
if kb.ShiftActive {
kb.ShiftActive = false
}
} else if inj != nil {
inj.PressKey(KEY_V, []int{KEY_LEFTCTRL})
}
return
}
// Cfg button - cycle theme (shift = reverse)
if key.Label == "Cfg" {
shiftOn := kb.ShiftActive != kb.CapsActive
if shiftOn {
if kb.OnThemeCycleReverse != nil {
kb.OnThemeCycleReverse()
}
if kb.ShiftActive {
kb.ShiftActive = false
}
} else if kb.OnThemeCycle != nil {
kb.OnThemeCycle()
}
return
}
shiftOn := kb.ShiftActive != kb.CapsActive
// AltTab cycling: hold Alt across multiple Tab presses
if key.Label == "AltTab" && !shiftOn {
if !kb.AltTabHeld {
if inj != nil {
inj.HoldKey(KEY_LEFTALT)
}
kb.AltTabHeld = true
}
kb.AltTabLastTab = time.Now()
if inj != nil {
inj.PressKey(KEY_TAB, nil)
}
return
}
// Release held Alt if pressing any other key
kb.ReleaseAltTab(inj)
// Shift+↑/↓: adjust mouse sensitivity instead of sending key
if shiftOn && (key.Code == KEY_UP || key.Code == KEY_DOWN) {
if key.Code == KEY_UP && kb.OnSensitivityUp != nil {
kb.OnSensitivityUp()
} else if key.Code == KEY_DOWN && kb.OnSensitivityDown != nil {
kb.OnSensitivityDown()
}
if kb.ShiftActive {
kb.ShiftActive = false
}
return
}
// Combo keys (shortcut row): default sends combo, shift sends ShiftCode
if len(key.Combo) > 0 || key.ShiftCode != 0 {
if inj != nil {
if shiftOn && key.ShiftCode != 0 {
inj.PressKey(key.ShiftCode, nil)
} else {
inj.PressKey(key.Code, key.Combo)
}
}
if kb.ShiftActive {
kb.ShiftActive = false
}
kb.CtrlActive = false
kb.AltActive = false
kb.MetaActive = false
return
}
code := key.Code
var mods []int
if shiftOn && code != KEY_UP && code != KEY_DOWN && code != KEY_LEFT && code != KEY_RIGHT &&
code != KEY_ESC && code != KEY_DELETE {
mods = append(mods, KEY_LEFTSHIFT)
}
if kb.CtrlActive {
mods = append(mods, KEY_LEFTCTRL)
}
if kb.AltActive {
mods = append(mods, KEY_LEFTALT)
}
if kb.MetaActive {
mods = append(mods, KEY_LEFTMETA)
}
if inj != nil {
inj.PressKey(code, mods)
}
if kb.ShiftActive && !kb.ShiftHeld {
kb.ShiftActive = false
}
kb.CtrlActive = false
kb.AltActive = false
kb.MetaActive = false
}
// CheckAltTabTimeout auto-releases Alt if the cycling has been idle too long.
// Returns true if Alt was released.
func (kb *KeyboardState) CheckAltTabTimeout(inj *Injector) bool {
if kb.AltTabHeld && time.Since(kb.AltTabLastTab) >= 3*time.Second {
kb.ReleaseAltTab(inj)
return true
}
return false
}
// ReleaseAltTab releases held Alt from Alt+Tab cycling if active.
func (kb *KeyboardState) ReleaseAltTab(inj *Injector) {
if !kb.AltTabHeld {
return
}
if inj != nil {
inj.ReleaseKey(KEY_LEFTALT)
}
kb.AltTabHeld = false
}
func (kb *KeyboardState) StartLongPress() {
key := kb.CurrentKey()
if len(key.Accents) > 0 {
kb.LongPressStart = time.Now()
kb.LongPressActive = true
}
}
func (kb *KeyboardState) CancelLongPress() {
kb.LongPressActive = false
}
func (kb *KeyboardState) CheckLongPress(thresholdMs int) bool {
if !kb.LongPressActive {
return false
}
if time.Since(kb.LongPressStart).Milliseconds() >= int64(thresholdMs) {
key := kb.CurrentKey()
if len(key.Accents) > 0 {
kb.AccentPopup = &AccentPopupState{Accents: key.Accents, Selected: 0}
kb.LongPressActive = false
return true
}
}
return false
}
func (kb *KeyboardState) CloseAccentPopup() {
kb.AccentPopup = nil
kb.LongPressActive = false
}
func (kb *KeyboardState) FlashKey(code int) {
kb.FlashCode = code
kb.FlashUntil = time.Now().Add(150 * time.Millisecond)
}
func (kb *KeyboardState) IsFlashed(key KeyDef) bool {
if kb.FlashCode != 0 && key.Code == kb.FlashCode {
if time.Now().Before(kb.FlashUntil) {
return true
}
kb.FlashCode = 0
}
return false
}
func (kb *KeyboardState) toggleModifier(key KeyDef) {
switch key.ModifierType {
case "shift":
kb.ShiftActive = !kb.ShiftActive
case "caps":
kb.CapsActive = !kb.CapsActive
case "ctrl":
kb.CtrlActive = !kb.CtrlActive
case "alt":
kb.AltActive = !kb.AltActive
case "meta":
kb.MetaActive = !kb.MetaActive
}
}