Debian Codegraph @

debian/vendor/github.com/micro-editor/tcell/v2/key.go

raw
1 // Copyright 2016 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use file except in compliance with the License.
5 // You may obtain a copy of the license at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tcell
16
17 import (
18 "fmt"
19 "strings"
20 "time"
21 )
22
23 // EventKey represents a key press. Usually this is a key press followed
24 // by a key release, but since terminal programs don't have a way to report
25 // key release events, we usually get just one event. If a key is held down
26 // then the terminal may synthesize repeated key presses at some predefined
27 // rate. We have no control over that, nor visibility into it.
28 //
29 // In some cases, we can have a modifier key, such as ModAlt, that can be
30 // generated with a key press. (This usually is represented by having the
31 // high bit set, or in some cases, by sending an ESC prior to the rune.)
32 //
33 // If the value of Key() is KeyRune, then the actual key value will be
34 // available with the Rune() method. This will be the case for most keys.
35 // In most situations, the modifiers will not be set. For example, if the
36 // rune is 'A', this will be reported without the ModShift bit set, since
37 // really can't tell if the Shift key was pressed (it might have been CAPSLOCK,
38 // or a terminal that only can send capitals, or keyboard with separate
39 // capital letters from lower case letters).
40 //
41 // Generally, terminal applications have far less visibility into keyboard
42 // activity than graphical applications. Hence, they should avoid depending
43 // overly much on availability of modifiers, or the availability of any
44 // specific keys.
45 type EventKey struct {
46 t time.Time
47 mod ModMask
48 key Key
49 esc string
50 ch rune
51 }
52
53 // When returns the time when this Event was created, which should closely
54 // match the time when the key was pressed.
55 func (ev *EventKey) When() time.Time {
56 return ev.t
57 }
58
59 // Rune returns the rune corresponding to the key press, if it makes sense.
60 // The result is only defined if the value of Key() is KeyRune.
61 func (ev *EventKey) Rune() rune {
62 return ev.ch
63 }
64
65 // Key returns a virtual key code. We use this to identify specific key
66 // codes, such as KeyEnter, etc. Most control and function keys are reported
67 // with unique Key values. Normal alphanumeric and punctuation keys will
68 // generally return KeyRune here; the specific key can be further decoded
69 // using the Rune() function.
70 func (ev *EventKey) Key() Key {
71 return ev.key
72 }
73
74 // Modifiers returns the modifiers that were present with the key press. Note
75 // that not all platforms and terminals support this equally well, and some
76 // cases we will not not know for sure. Hence, applications should avoid
77 // using this in most circumstances.
78 func (ev *EventKey) Modifiers() ModMask {
79 return ev.mod
80 }
81
82 // KeyNames holds the written names of special keys. Useful to echo back a key
83 // name, or to look up a key from a string value.
84 var KeyNames = map[Key]string{
85 KeyEnter: "Enter",
86 KeyBackspace: "Backspace",
87 KeyTab: "Tab",
88 KeyBacktab: "Backtab",
89 KeyEsc: "Esc",
90 KeyBackspace2: "Backspace2",
91 KeyDelete: "Delete",
92 KeyInsert: "Insert",
93 KeyUp: "Up",
94 KeyDown: "Down",
95 KeyLeft: "Left",
96 KeyRight: "Right",
97 KeyHome: "Home",
98 KeyEnd: "End",
99 KeyUpLeft: "UpLeft",
100 KeyUpRight: "UpRight",
101 KeyDownLeft: "DownLeft",
102 KeyDownRight: "DownRight",
103 KeyCenter: "Center",
104 KeyPgDn: "PgDn",
105 KeyPgUp: "PgUp",
106 KeyClear: "Clear",
107 KeyExit: "Exit",
108 KeyCancel: "Cancel",
109 KeyPause: "Pause",
110 KeyPrint: "Print",
111 KeyF1: "F1",
112 KeyF2: "F2",
113 KeyF3: "F3",
114 KeyF4: "F4",
115 KeyF5: "F5",
116 KeyF6: "F6",
117 KeyF7: "F7",
118 KeyF8: "F8",
119 KeyF9: "F9",
120 KeyF10: "F10",
121 KeyF11: "F11",
122 KeyF12: "F12",
123 KeyF13: "F13",
124 KeyF14: "F14",
125 KeyF15: "F15",
126 KeyF16: "F16",
127 KeyF17: "F17",
128 KeyF18: "F18",
129 KeyF19: "F19",
130 KeyF20: "F20",
131 KeyF21: "F21",
132 KeyF22: "F22",
133 KeyF23: "F23",
134 KeyF24: "F24",
135 KeyF25: "F25",
136 KeyF26: "F26",
137 KeyF27: "F27",
138 KeyF28: "F28",
139 KeyF29: "F29",
140 KeyF30: "F30",
141 KeyF31: "F31",
142 KeyF32: "F32",
143 KeyF33: "F33",
144 KeyF34: "F34",
145 KeyF35: "F35",
146 KeyF36: "F36",
147 KeyF37: "F37",
148 KeyF38: "F38",
149 KeyF39: "F39",
150 KeyF40: "F40",
151 KeyF41: "F41",
152 KeyF42: "F42",
153 KeyF43: "F43",
154 KeyF44: "F44",
155 KeyF45: "F45",
156 KeyF46: "F46",
157 KeyF47: "F47",
158 KeyF48: "F48",
159 KeyF49: "F49",
160 KeyF50: "F50",
161 KeyF51: "F51",
162 KeyF52: "F52",
163 KeyF53: "F53",
164 KeyF54: "F54",
165 KeyF55: "F55",
166 KeyF56: "F56",
167 KeyF57: "F57",
168 KeyF58: "F58",
169 KeyF59: "F59",
170 KeyF60: "F60",
171 KeyF61: "F61",
172 KeyF62: "F62",
173 KeyF63: "F63",
174 KeyF64: "F64",
175 KeyCtrlA: "Ctrl-A",
176 KeyCtrlB: "Ctrl-B",
177 KeyCtrlC: "Ctrl-C",
178 KeyCtrlD: "Ctrl-D",
179 KeyCtrlE: "Ctrl-E",
180 KeyCtrlF: "Ctrl-F",
181 KeyCtrlG: "Ctrl-G",
182 KeyCtrlJ: "Ctrl-J",
183 KeyCtrlK: "Ctrl-K",
184 KeyCtrlL: "Ctrl-L",
185 KeyCtrlN: "Ctrl-N",
186 KeyCtrlO: "Ctrl-O",
187 KeyCtrlP: "Ctrl-P",
188 KeyCtrlQ: "Ctrl-Q",
189 KeyCtrlR: "Ctrl-R",
190 KeyCtrlS: "Ctrl-S",
191 KeyCtrlT: "Ctrl-T",
192 KeyCtrlU: "Ctrl-U",
193 KeyCtrlV: "Ctrl-V",
194 KeyCtrlW: "Ctrl-W",
195 KeyCtrlX: "Ctrl-X",
196 KeyCtrlY: "Ctrl-Y",
197 KeyCtrlZ: "Ctrl-Z",
198 KeyCtrlSpace: "Ctrl-Space",
199 KeyCtrlUnderscore: "Ctrl-_",
200 KeyCtrlRightSq: "Ctrl-]",
201 KeyCtrlBackslash: "Ctrl-\\",
202 KeyCtrlCarat: "Ctrl-^",
203 }
204
205 // Name returns a printable value or the key stroke. This can be used
206 // when printing the event, for example.
207 func (ev *EventKey) Name() string {
208 s := ""
209 m := []string{}
210 if ev.mod&ModShift != 0 {
211 m = append(m, "Shift")
212 }
213 if ev.mod&ModAlt != 0 {
214 m = append(m, "Alt")
215 }
216 if ev.mod&ModMeta != 0 {
217 m = append(m, "Meta")
218 }
219 if ev.mod&ModCtrl != 0 {
220 m = append(m, "Ctrl")
221 }
222
223 ok := false
224 if s, ok = KeyNames[ev.key]; !ok {
225 if ev.key == KeyRune {
226 s = "Rune[" + string(ev.ch) + "]"
227 } else {
228 s = fmt.Sprintf("Key[%d,%d]", ev.key, int(ev.ch))
229 }
230 }
231 if len(m) != 0 {
232 if ev.mod&ModCtrl != 0 && strings.HasPrefix(s, "Ctrl-") {
233 s = s[5:]
234 }
235 return fmt.Sprintf("%s+%s", strings.Join(m, "+"), s)
236 }
237 return s
238 }
239
240 func (ev *EventKey) EscSeq() string {
241 return ev.esc
242 }
243
244 // NewEventKey attempts to create a suitable event. It parses the various
245 // ASCII control sequences if KeyRune is passed for Key, but if the caller
246 // has more precise information it should set that specifically. Callers
247 // that aren't sure about modifier state (most) should just pass ModNone.
248 func NewEventKey(k Key, ch rune, mod ModMask, esc string) *EventKey {
249 if k == KeyRune && (ch < ' ' || ch == 0x7f) {
250 // Turn specials into proper key codes. This is for
251 // control characters and the DEL.
252 k = Key(ch)
253 if mod == ModNone && ch < ' ' {
254 switch Key(ch) {
255 case KeyBackspace, KeyTab, KeyEsc, KeyEnter:
256 // these keys are directly typeable without CTRL
257 default:
258 // most likely entered with a CTRL keypress
259 mod = ModCtrl
260 }
261 }
262 }
263 return &EventKey{t: time.Now(), key: k, ch: ch, mod: mod, esc: esc}
264 }
265
266 // ModMask is a mask of modifier keys. Note that it will not always be
267 // possible to report modifier keys.
268 type ModMask int16
269
270 // These are the modifiers keys that can be sent either with a key press,
271 // or a mouse event. Note that as of now, due to the confusion associated
272 // with Meta, and the lack of support for it on many/most platforms, the
273 // current implementations never use it. Instead, they use ModAlt, even for
274 // events that could possibly have been distinguished from ModAlt.
275 const (
276 ModShift ModMask = 1 << iota
277 ModCtrl
278 ModAlt
279 ModMeta
280 ModNone ModMask = 0
281 )
282
283 // Key is a generic value for representing keys, and especially special
284 // keys (function keys, cursor movement keys, etc.) For normal keys, like
285 // ASCII letters, we use KeyRune, and then expect the application to
286 // inspect the Rune() member of the EventKey.
287 type Key int16
288
289 // This is the list of named keys. KeyRune is special however, in that it is
290 // a place holder key indicating that a printable character was sent. The
291 // actual value of the rune will be transported in the Rune of the associated
292 // EventKey.
293 const (
294 KeyRune Key = iota + 256
295 KeyUp
296 KeyDown
297 KeyRight
298 KeyLeft
299 KeyUpLeft
300 KeyUpRight
301 KeyDownLeft
302 KeyDownRight
303 KeyCenter
304 KeyPgUp
305 KeyPgDn
306 KeyHome
307 KeyEnd
308 KeyInsert
309 KeyDelete
310 KeyHelp
311 KeyExit
312 KeyClear
313 KeyCancel
314 KeyPrint
315 KeyPause
316 KeyBacktab
317 KeyF1
318 KeyF2
319 KeyF3
320 KeyF4
321 KeyF5
322 KeyF6
323 KeyF7
324 KeyF8
325 KeyF9
326 KeyF10
327 KeyF11
328 KeyF12
329 KeyF13
330 KeyF14
331 KeyF15
332 KeyF16
333 KeyF17
334 KeyF18
335 KeyF19
336 KeyF20
337 KeyF21
338 KeyF22
339 KeyF23
340 KeyF24
341 KeyF25
342 KeyF26
343 KeyF27
344 KeyF28
345 KeyF29
346 KeyF30
347 KeyF31
348 KeyF32
349 KeyF33
350 KeyF34
351 KeyF35
352 KeyF36
353 KeyF37
354 KeyF38
355 KeyF39
356 KeyF40
357 KeyF41
358 KeyF42
359 KeyF43
360 KeyF44
361 KeyF45
362 KeyF46
363 KeyF47
364 KeyF48
365 KeyF49
366 KeyF50
367 KeyF51
368 KeyF52
369 KeyF53
370 KeyF54
371 KeyF55
372 KeyF56
373 KeyF57
374 KeyF58
375 KeyF59
376 KeyF60
377 KeyF61
378 KeyF62
379 KeyF63
380 KeyF64
381 )
382
383 // These are the control keys. Note that they overlap with other keys,
384 // perhaps. For example, KeyCtrlH is the same as KeyBackspace.
385 const (
386 KeyCtrlSpace Key = iota
387 KeyCtrlA
388 KeyCtrlB
389 KeyCtrlC
390 KeyCtrlD
391 KeyCtrlE
392 KeyCtrlF
393 KeyCtrlG
394 KeyCtrlH
395 KeyCtrlI
396 KeyCtrlJ
397 KeyCtrlK
398 KeyCtrlL
399 KeyCtrlM
400 KeyCtrlN
401 KeyCtrlO
402 KeyCtrlP
403 KeyCtrlQ
404 KeyCtrlR
405 KeyCtrlS
406 KeyCtrlT
407 KeyCtrlU
408 KeyCtrlV
409 KeyCtrlW
410 KeyCtrlX
411 KeyCtrlY
412 KeyCtrlZ
413 KeyCtrlLeftSq // Escape
414 KeyCtrlBackslash
415 KeyCtrlRightSq
416 KeyCtrlCarat
417 KeyCtrlUnderscore
418 )
419
420 // Special values - these are fixed in an attempt to make it more likely
421 // that aliases will encode the same way.
422
423 // These are the defined ASCII values for key codes. They generally match
424 // with KeyCtrl values.
425 const (
426 KeyNUL Key = iota
427 KeySOH
428 KeySTX
429 KeyETX
430 KeyEOT
431 KeyENQ
432 KeyACK
433 KeyBEL
434 KeyBS
435 KeyTAB
436 KeyLF
437 KeyVT
438 KeyFF
439 KeyCR
440 KeySO
441 KeySI
442 KeyDLE
443 KeyDC1
444 KeyDC2
445 KeyDC3
446 KeyDC4
447 KeyNAK
448 KeySYN
449 KeyETB
450 KeyCAN
451 KeyEM
452 KeySUB
453 KeyESC
454 KeyFS
455 KeyGS
456 KeyRS
457 KeyUS
458 KeyDEL Key = 0x7F
459 )
460
461 // These keys are aliases for other names.
462 const (
463 KeyBackspace = KeyBS
464 KeyTab = KeyTAB
465 KeyEsc = KeyESC
466 KeyEscape = KeyESC
467 KeyEnter = KeyCR
468 KeyBackspace2 = KeyDEL
469 )