Debian Codegraph @

input/key.go

raw
1 package input
2
3 import (
4 "fmt"
5 "strings"
6 "unicode"
7
8 "github.com/charmbracelet/x/ansi"
9 )
10
11 const (
12 // KeyExtended is a special key code used to signify that a key event
13 // contains multiple runes.
14 KeyExtended = unicode.MaxRune + 1
15 )
16
17 // Special key symbols.
18 const (
19
20 // Special keys.
21
22 KeyUp rune = KeyExtended + iota + 1
23 KeyDown
24 KeyRight
25 KeyLeft
26 KeyBegin
27 KeyFind
28 KeyInsert
29 KeyDelete
30 KeySelect
31 KeyPgUp
32 KeyPgDown
33 KeyHome
34 KeyEnd
35
36 // Keypad keys.
37
38 KeyKpEnter
39 KeyKpEqual
40 KeyKpMultiply
41 KeyKpPlus
42 KeyKpComma
43 KeyKpMinus
44 KeyKpDecimal
45 KeyKpDivide
46 KeyKp0
47 KeyKp1
48 KeyKp2
49 KeyKp3
50 KeyKp4
51 KeyKp5
52 KeyKp6
53 KeyKp7
54 KeyKp8
55 KeyKp9
56
57 //nolint:godox
58 // The following are keys defined in the Kitty keyboard protocol.
59 // TODO: Investigate the names of these keys.
60
61 KeyKpSep
62 KeyKpUp
63 KeyKpDown
64 KeyKpLeft
65 KeyKpRight
66 KeyKpPgUp
67 KeyKpPgDown
68 KeyKpHome
69 KeyKpEnd
70 KeyKpInsert
71 KeyKpDelete
72 KeyKpBegin
73
74 // Function keys.
75
76 KeyF1
77 KeyF2
78 KeyF3
79 KeyF4
80 KeyF5
81 KeyF6
82 KeyF7
83 KeyF8
84 KeyF9
85 KeyF10
86 KeyF11
87 KeyF12
88 KeyF13
89 KeyF14
90 KeyF15
91 KeyF16
92 KeyF17
93 KeyF18
94 KeyF19
95 KeyF20
96 KeyF21
97 KeyF22
98 KeyF23
99 KeyF24
100 KeyF25
101 KeyF26
102 KeyF27
103 KeyF28
104 KeyF29
105 KeyF30
106 KeyF31
107 KeyF32
108 KeyF33
109 KeyF34
110 KeyF35
111 KeyF36
112 KeyF37
113 KeyF38
114 KeyF39
115 KeyF40
116 KeyF41
117 KeyF42
118 KeyF43
119 KeyF44
120 KeyF45
121 KeyF46
122 KeyF47
123 KeyF48
124 KeyF49
125 KeyF50
126 KeyF51
127 KeyF52
128 KeyF53
129 KeyF54
130 KeyF55
131 KeyF56
132 KeyF57
133 KeyF58
134 KeyF59
135 KeyF60
136 KeyF61
137 KeyF62
138 KeyF63
139
140 //nolint:godox
141 // The following are keys defined in the Kitty keyboard protocol.
142 // TODO: Investigate the names of these keys.
143
144 KeyCapsLock
145 KeyScrollLock
146 KeyNumLock
147 KeyPrintScreen
148 KeyPause
149 KeyMenu
150
151 KeyMediaPlay
152 KeyMediaPause
153 KeyMediaPlayPause
154 KeyMediaReverse
155 KeyMediaStop
156 KeyMediaFastForward
157 KeyMediaRewind
158 KeyMediaNext
159 KeyMediaPrev
160 KeyMediaRecord
161
162 KeyLowerVol
163 KeyRaiseVol
164 KeyMute
165
166 KeyLeftShift
167 KeyLeftAlt
168 KeyLeftCtrl
169 KeyLeftSuper
170 KeyLeftHyper
171 KeyLeftMeta
172 KeyRightShift
173 KeyRightAlt
174 KeyRightCtrl
175 KeyRightSuper
176 KeyRightHyper
177 KeyRightMeta
178 KeyIsoLevel3Shift
179 KeyIsoLevel5Shift
180
181 // Special names in C0.
182
183 KeyBackspace = rune(ansi.DEL)
184 KeyTab = rune(ansi.HT)
185 KeyEnter = rune(ansi.CR)
186 KeyReturn = KeyEnter
187 KeyEscape = rune(ansi.ESC)
188 KeyEsc = KeyEscape
189
190 // Special names in G0.
191
192 KeySpace = rune(ansi.SP)
193 )
194
195 // KeyPressEvent represents a key press event.
196 type KeyPressEvent Key
197
198 // String implements [fmt.Stringer] and is quite useful for matching key
199 // events. For details, on what this returns see [Key.String].
200 func (k KeyPressEvent) String() string {
201 return Key(k).String()
202 }
203
204 // Keystroke returns the keystroke representation of the [Key]. While less type
205 // safe than looking at the individual fields, it will usually be more
206 // convenient and readable to use this method when matching against keys.
207 //
208 // Note that modifier keys are always printed in the following order:
209 // - ctrl
210 // - alt
211 // - shift
212 // - meta
213 // - hyper
214 // - super
215 //
216 // For example, you'll always see "ctrl+shift+alt+a" and never
217 // "shift+ctrl+alt+a".
218 func (k KeyPressEvent) Keystroke() string {
219 return Key(k).Keystroke()
220 }
221
222 // Key returns the underlying key event. This is a syntactic sugar for casting
223 // the key event to a [Key].
224 func (k KeyPressEvent) Key() Key {
225 return Key(k)
226 }
227
228 // KeyReleaseEvent represents a key release event.
229 type KeyReleaseEvent Key
230
231 // String implements [fmt.Stringer] and is quite useful for matching key
232 // events. For details, on what this returns see [Key.String].
233 func (k KeyReleaseEvent) String() string {
234 return Key(k).String()
235 }
236
237 // Keystroke returns the keystroke representation of the [Key]. While less type
238 // safe than looking at the individual fields, it will usually be more
239 // convenient and readable to use this method when matching against keys.
240 //
241 // Note that modifier keys are always printed in the following order:
242 // - ctrl
243 // - alt
244 // - shift
245 // - meta
246 // - hyper
247 // - super
248 //
249 // For example, you'll always see "ctrl+shift+alt+a" and never
250 // "shift+ctrl+alt+a".
251 func (k KeyReleaseEvent) Keystroke() string {
252 return Key(k).Keystroke()
253 }
254
255 // Key returns the underlying key event. This is a convenience method and
256 // syntactic sugar to satisfy the [KeyEvent] interface, and cast the key event to
257 // [Key].
258 func (k KeyReleaseEvent) Key() Key {
259 return Key(k)
260 }
261
262 // KeyEvent represents a key event. This can be either a key press or a key
263 // release event.
264 type KeyEvent interface {
265 fmt.Stringer
266
267 // Key returns the underlying key event.
268 Key() Key
269 }
270
271 // Key represents a Key press or release event. It contains information about
272 // the Key pressed, like the runes, the type of Key, and the modifiers pressed.
273 // There are a couple general patterns you could use to check for key presses
274 // or releases:
275 //
276 // // Switch on the string representation of the key (shorter)
277 // switch ev := ev.(type) {
278 // case KeyPressEvent:
279 // switch ev.String() {
280 // case "enter":
281 // fmt.Println("you pressed enter!")
282 // case "a":
283 // fmt.Println("you pressed a!")
284 // }
285 // }
286 //
287 // // Switch on the key type (more foolproof)
288 // switch ev := ev.(type) {
289 // case KeyEvent:
290 // // catch both KeyPressEvent and KeyReleaseEvent
291 // switch key := ev.Key(); key.Code {
292 // case KeyEnter:
293 // fmt.Println("you pressed enter!")
294 // default:
295 // switch key.Text {
296 // case "a":
297 // fmt.Println("you pressed a!")
298 // }
299 // }
300 // }
301 //
302 // Note that [Key.Text] will be empty for special keys like [KeyEnter],
303 // [KeyTab], and for keys that don't represent printable characters like key
304 // combos with modifier keys. In other words, [Key.Text] is populated only for
305 // keys that represent printable characters shifted or unshifted (like 'a',
306 // 'A', '1', '!', etc.).
307 type Key struct {
308 // Text contains the actual characters received. This usually the same as
309 // [Key.Code]. When [Key.Text] is non-empty, it indicates that the key
310 // pressed represents printable character(s).
311 Text string
312
313 // Mod represents modifier keys, like [ModCtrl], [ModAlt], and so on.
314 Mod KeyMod
315
316 // Code represents the key pressed. This is usually a special key like
317 // [KeyTab], [KeyEnter], [KeyF1], or a printable character like 'a'.
318 Code rune
319
320 // ShiftedCode is the actual, shifted key pressed by the user. For example,
321 // if the user presses shift+a, or caps lock is on, [Key.ShiftedCode] will
322 // be 'A' and [Key.Code] will be 'a'.
323 //
324 // In the case of non-latin keyboards, like Arabic, [Key.ShiftedCode] is the
325 // unshifted key on the keyboard.
326 //
327 // This is only available with the Kitty Keyboard Protocol or the Windows
328 // Console API.
329 ShiftedCode rune
330
331 // BaseCode is the key pressed according to the standard PC-101 key layout.
332 // On international keyboards, this is the key that would be pressed if the
333 // keyboard was set to US PC-101 layout.
334 //
335 // For example, if the user presses 'q' on a French AZERTY keyboard,
336 // [Key.BaseCode] will be 'q'.
337 //
338 // This is only available with the Kitty Keyboard Protocol or the Windows
339 // Console API.
340 BaseCode rune
341
342 // IsRepeat indicates whether the key is being held down and sending events
343 // repeatedly.
344 //
345 // This is only available with the Kitty Keyboard Protocol or the Windows
346 // Console API.
347 IsRepeat bool
348 }
349
350 // String implements [fmt.Stringer] and is quite useful for matching key
351 // events. It will return the textual representation of the [Key] if there is
352 // one, otherwise, it will fallback to [Key.Keystroke].
353 //
354 // For example, you'll always get "?" and instead of "shift+/" on a US ANSI
355 // keyboard.
356 func (k Key) String() string {
357 if len(k.Text) > 0 && k.Text != " " {
358 return k.Text
359 }
360 return k.Keystroke()
361 }
362
363 // Keystroke returns the keystroke representation of the [Key]. While less type
364 // safe than looking at the individual fields, it will usually be more
365 // convenient and readable to use this method when matching against keys.
366 //
367 // Note that modifier keys are always printed in the following order:
368 // - ctrl
369 // - alt
370 // - shift
371 // - meta
372 // - hyper
373 // - super
374 //
375 // For example, you'll always see "ctrl+shift+alt+a" and never
376 // "shift+ctrl+alt+a".
377 func (k Key) Keystroke() string {
378 var sb strings.Builder
379 if k.Mod.Contains(ModCtrl) && k.Code != KeyLeftCtrl && k.Code != KeyRightCtrl {
380 sb.WriteString("ctrl+")
381 }
382 if k.Mod.Contains(ModAlt) && k.Code != KeyLeftAlt && k.Code != KeyRightAlt {
383 sb.WriteString("alt+")
384 }
385 if k.Mod.Contains(ModShift) && k.Code != KeyLeftShift && k.Code != KeyRightShift {
386 sb.WriteString("shift+")
387 }
388 if k.Mod.Contains(ModMeta) && k.Code != KeyLeftMeta && k.Code != KeyRightMeta {
389 sb.WriteString("meta+")
390 }
391 if k.Mod.Contains(ModHyper) && k.Code != KeyLeftHyper && k.Code != KeyRightHyper {
392 sb.WriteString("hyper+")
393 }
394 if k.Mod.Contains(ModSuper) && k.Code != KeyLeftSuper && k.Code != KeyRightSuper {
395 sb.WriteString("super+")
396 }
397
398 if kt, ok := keyTypeString[k.Code]; ok {
399 sb.WriteString(kt)
400 } else {
401 code := k.Code
402 if k.BaseCode != 0 {
403 // If a [Key.BaseCode] is present, use it to represent a key using the standard
404 // PC-101 key layout.
405 code = k.BaseCode
406 }
407
408 switch code {
409 case KeySpace:
410 // Space is the only invisible printable character.
411 sb.WriteString("space")
412 case KeyExtended:
413 // Write the actual text of the key when the key contains multiple
414 // runes.
415 sb.WriteString(k.Text)
416 default:
417 sb.WriteRune(code)
418 }
419 }
420
421 return sb.String()
422 }
423
424 var keyTypeString = map[rune]string{
425 KeyEnter: "enter",
426 KeyTab: "tab",
427 KeyBackspace: "backspace",
428 KeyEscape: "esc",
429 KeySpace: "space",
430 KeyUp: "up",
431 KeyDown: "down",
432 KeyLeft: "left",
433 KeyRight: "right",
434 KeyBegin: "begin",
435 KeyFind: "find",
436 KeyInsert: "insert",
437 KeyDelete: "delete",
438 KeySelect: "select",
439 KeyPgUp: "pgup",
440 KeyPgDown: "pgdown",
441 KeyHome: "home",
442 KeyEnd: "end",
443 KeyKpEnter: "kpenter",
444 KeyKpEqual: "kpequal",
445 KeyKpMultiply: "kpmul",
446 KeyKpPlus: "kpplus",
447 KeyKpComma: "kpcomma",
448 KeyKpMinus: "kpminus",
449 KeyKpDecimal: "kpperiod",
450 KeyKpDivide: "kpdiv",
451 KeyKp0: "kp0",
452 KeyKp1: "kp1",
453 KeyKp2: "kp2",
454 KeyKp3: "kp3",
455 KeyKp4: "kp4",
456 KeyKp5: "kp5",
457 KeyKp6: "kp6",
458 KeyKp7: "kp7",
459 KeyKp8: "kp8",
460 KeyKp9: "kp9",
461
462 // Kitty keyboard extension
463 KeyKpSep: "kpsep",
464 KeyKpUp: "kpup",
465 KeyKpDown: "kpdown",
466 KeyKpLeft: "kpleft",
467 KeyKpRight: "kpright",
468 KeyKpPgUp: "kppgup",
469 KeyKpPgDown: "kppgdown",
470 KeyKpHome: "kphome",
471 KeyKpEnd: "kpend",
472 KeyKpInsert: "kpinsert",
473 KeyKpDelete: "kpdelete",
474 KeyKpBegin: "kpbegin",
475
476 KeyF1: "f1",
477 KeyF2: "f2",
478 KeyF3: "f3",
479 KeyF4: "f4",
480 KeyF5: "f5",
481 KeyF6: "f6",
482 KeyF7: "f7",
483 KeyF8: "f8",
484 KeyF9: "f9",
485 KeyF10: "f10",
486 KeyF11: "f11",
487 KeyF12: "f12",
488 KeyF13: "f13",
489 KeyF14: "f14",
490 KeyF15: "f15",
491 KeyF16: "f16",
492 KeyF17: "f17",
493 KeyF18: "f18",
494 KeyF19: "f19",
495 KeyF20: "f20",
496 KeyF21: "f21",
497 KeyF22: "f22",
498 KeyF23: "f23",
499 KeyF24: "f24",
500 KeyF25: "f25",
501 KeyF26: "f26",
502 KeyF27: "f27",
503 KeyF28: "f28",
504 KeyF29: "f29",
505 KeyF30: "f30",
506 KeyF31: "f31",
507 KeyF32: "f32",
508 KeyF33: "f33",
509 KeyF34: "f34",
510 KeyF35: "f35",
511 KeyF36: "f36",
512 KeyF37: "f37",
513 KeyF38: "f38",
514 KeyF39: "f39",
515 KeyF40: "f40",
516 KeyF41: "f41",
517 KeyF42: "f42",
518 KeyF43: "f43",
519 KeyF44: "f44",
520 KeyF45: "f45",
521 KeyF46: "f46",
522 KeyF47: "f47",
523 KeyF48: "f48",
524 KeyF49: "f49",
525 KeyF50: "f50",
526 KeyF51: "f51",
527 KeyF52: "f52",
528 KeyF53: "f53",
529 KeyF54: "f54",
530 KeyF55: "f55",
531 KeyF56: "f56",
532 KeyF57: "f57",
533 KeyF58: "f58",
534 KeyF59: "f59",
535 KeyF60: "f60",
536 KeyF61: "f61",
537 KeyF62: "f62",
538 KeyF63: "f63",
539
540 // Kitty keyboard extension
541 KeyCapsLock: "capslock",
542 KeyScrollLock: "scrolllock",
543 KeyNumLock: "numlock",
544 KeyPrintScreen: "printscreen",
545 KeyPause: "pause",
546 KeyMenu: "menu",
547 KeyMediaPlay: "mediaplay",
548 KeyMediaPause: "mediapause",
549 KeyMediaPlayPause: "mediaplaypause",
550 KeyMediaReverse: "mediareverse",
551 KeyMediaStop: "mediastop",
552 KeyMediaFastForward: "mediafastforward",
553 KeyMediaRewind: "mediarewind",
554 KeyMediaNext: "medianext",
555 KeyMediaPrev: "mediaprev",
556 KeyMediaRecord: "mediarecord",
557 KeyLowerVol: "lowervol",
558 KeyRaiseVol: "raisevol",
559 KeyMute: "mute",
560 KeyLeftShift: "leftshift",
561 KeyLeftAlt: "leftalt",
562 KeyLeftCtrl: "leftctrl",
563 KeyLeftSuper: "leftsuper",
564 KeyLeftHyper: "lefthyper",
565 KeyLeftMeta: "leftmeta",
566 KeyRightShift: "rightshift",
567 KeyRightAlt: "rightalt",
568 KeyRightCtrl: "rightctrl",
569 KeyRightSuper: "rightsuper",
570 KeyRightHyper: "righthyper",
571 KeyRightMeta: "rightmeta",
572 KeyIsoLevel3Shift: "isolevel3shift",
573 KeyIsoLevel5Shift: "isolevel5shift",
574 }