key.go 11 KB

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