key.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package input
  2. // KeySym is a keyboard symbol.
  3. type KeySym int
  4. // Symbol constants.
  5. const (
  6. KeyNone KeySym = iota
  7. // Special names in C0
  8. KeyBackspace
  9. KeyTab
  10. KeyEnter
  11. KeyEscape
  12. // Special names in G0
  13. KeySpace
  14. KeyDelete
  15. // Special keys
  16. KeyUp
  17. KeyDown
  18. KeyRight
  19. KeyLeft
  20. KeyBegin
  21. KeyFind
  22. KeyInsert
  23. KeySelect
  24. KeyPgUp
  25. KeyPgDown
  26. KeyHome
  27. KeyEnd
  28. // Keypad keys
  29. KeyKpEnter
  30. KeyKpEqual
  31. KeyKpMultiply
  32. KeyKpPlus
  33. KeyKpComma
  34. KeyKpMinus
  35. KeyKpDecimal
  36. KeyKpDivide
  37. KeyKp0
  38. KeyKp1
  39. KeyKp2
  40. KeyKp3
  41. KeyKp4
  42. KeyKp5
  43. KeyKp6
  44. KeyKp7
  45. KeyKp8
  46. KeyKp9
  47. // The following are keys defined in the Kitty keyboard protocol.
  48. // TODO: Investigate the names of these keys
  49. KeyKpSep
  50. KeyKpUp
  51. KeyKpDown
  52. KeyKpLeft
  53. KeyKpRight
  54. KeyKpPgUp
  55. KeyKpPgDown
  56. KeyKpHome
  57. KeyKpEnd
  58. KeyKpInsert
  59. KeyKpDelete
  60. KeyKpBegin
  61. // Function keys
  62. KeyF1
  63. KeyF2
  64. KeyF3
  65. KeyF4
  66. KeyF5
  67. KeyF6
  68. KeyF7
  69. KeyF8
  70. KeyF9
  71. KeyF10
  72. KeyF11
  73. KeyF12
  74. KeyF13
  75. KeyF14
  76. KeyF15
  77. KeyF16
  78. KeyF17
  79. KeyF18
  80. KeyF19
  81. KeyF20
  82. KeyF21
  83. KeyF22
  84. KeyF23
  85. KeyF24
  86. KeyF25
  87. KeyF26
  88. KeyF27
  89. KeyF28
  90. KeyF29
  91. KeyF30
  92. KeyF31
  93. KeyF32
  94. KeyF33
  95. KeyF34
  96. KeyF35
  97. KeyF36
  98. KeyF37
  99. KeyF38
  100. KeyF39
  101. KeyF40
  102. KeyF41
  103. KeyF42
  104. KeyF43
  105. KeyF44
  106. KeyF45
  107. KeyF46
  108. KeyF47
  109. KeyF48
  110. KeyF49
  111. KeyF50
  112. KeyF51
  113. KeyF52
  114. KeyF53
  115. KeyF54
  116. KeyF55
  117. KeyF56
  118. KeyF57
  119. KeyF58
  120. KeyF59
  121. KeyF60
  122. KeyF61
  123. KeyF62
  124. KeyF63
  125. // The following are keys defined in the Kitty keyboard protocol.
  126. // TODO: Investigate the names of these keys
  127. KeyCapsLock
  128. KeyScrollLock
  129. KeyNumLock
  130. KeyPrintScreen
  131. KeyPause
  132. KeyMenu
  133. KeyMediaPlay
  134. KeyMediaPause
  135. KeyMediaPlayPause
  136. KeyMediaReverse
  137. KeyMediaStop
  138. KeyMediaFastForward
  139. KeyMediaRewind
  140. KeyMediaNext
  141. KeyMediaPrev
  142. KeyMediaRecord
  143. KeyLowerVol
  144. KeyRaiseVol
  145. KeyMute
  146. KeyLeftShift
  147. KeyLeftAlt
  148. KeyLeftCtrl
  149. KeyLeftSuper
  150. KeyLeftHyper
  151. KeyLeftMeta
  152. KeyRightShift
  153. KeyRightAlt
  154. KeyRightCtrl
  155. KeyRightSuper
  156. KeyRightHyper
  157. KeyRightMeta
  158. KeyIsoLevel3Shift
  159. KeyIsoLevel5Shift
  160. )
  161. // Key represents a key event.
  162. type Key struct {
  163. // Sym is a special key, like enter, tab, backspace, and so on.
  164. Sym KeySym
  165. // Rune is the actual character received. If the user presses shift+a, the
  166. // Rune will be 'A'.
  167. Rune rune
  168. // AltRune is the actual, unshifted key pressed by the user. For example,
  169. // if the user presses shift+a, or caps lock is on, the AltRune will be
  170. // 'a'.
  171. //
  172. // In the case of non-latin keyboards, like Arabic, AltRune is the
  173. // unshifted key on the keyboard.
  174. //
  175. // This is only available with the Kitty Keyboard Protocol or the Windows
  176. // Console API.
  177. AltRune rune
  178. // baseRune is the key pressed according to the standard PC-101 key layout.
  179. // On internaltional keyboards, this is the key that would be pressed if
  180. // the keyboard was set to US layout.
  181. //
  182. // For example, if the user presses 'q' on a French AZERTY keyboard, the
  183. // baseRune will be 'q'.
  184. //
  185. // This is only available with the Kitty Keyboard Protocol or the Windows
  186. // Console API.
  187. baseRune rune
  188. // Mod is a modifier key, like ctrl, alt, and so on.
  189. Mod KeyMod
  190. // IsRepeat indicates whether the key is being held down and sending events
  191. // repeatedly.
  192. //
  193. // This is only available with the Kitty Keyboard Protocol or the Windows
  194. // Console API.
  195. IsRepeat bool
  196. }
  197. // KeyPressEvent represents a key press event.
  198. type KeyPressEvent Key
  199. // String implements fmt.Stringer and is quite useful for matching key
  200. // events. For details, on what this returns see [Key.String].
  201. func (k KeyPressEvent) String() string {
  202. return Key(k).String()
  203. }
  204. // KeyReleaseEvent represents a key release event.
  205. type KeyReleaseEvent Key
  206. // String implements fmt.Stringer and is quite useful for matching complex key
  207. // events. For details, on what this returns see [Key.String].
  208. func (k KeyReleaseEvent) String() string {
  209. return Key(k).String()
  210. }
  211. // String implements fmt.Stringer and is used to convert a key to a string.
  212. // While less type safe than looking at the individual fields, it will usually
  213. // be more convenient and readable to use this method when matching against
  214. // keys.
  215. //
  216. // Note that modifier keys are always printed in the following order:
  217. // - ctrl
  218. // - alt
  219. // - shift
  220. // - meta
  221. // - hyper
  222. // - super
  223. //
  224. // For example, you'll always see "ctrl+shift+alt+a" and never
  225. // "shift+ctrl+alt+a".
  226. func (k Key) String() string {
  227. var s string
  228. if k.Mod.HasCtrl() && k.Sym != KeyLeftCtrl && k.Sym != KeyRightCtrl {
  229. s += "ctrl+"
  230. }
  231. if k.Mod.HasAlt() && k.Sym != KeyLeftAlt && k.Sym != KeyRightAlt {
  232. s += "alt+"
  233. }
  234. if k.Mod.HasShift() && k.Sym != KeyLeftShift && k.Sym != KeyRightShift {
  235. s += "shift+"
  236. }
  237. if k.Mod.HasMeta() && k.Sym != KeyLeftMeta && k.Sym != KeyRightMeta {
  238. s += "meta+"
  239. }
  240. if k.Mod.HasHyper() && k.Sym != KeyLeftHyper && k.Sym != KeyRightHyper {
  241. s += "hyper+"
  242. }
  243. if k.Mod.HasSuper() && k.Sym != KeyLeftSuper && k.Sym != KeyRightSuper {
  244. s += "super+"
  245. }
  246. runeStr := func(r rune) string {
  247. // Space is the only invisible printable character.
  248. if r == ' ' {
  249. return "space"
  250. }
  251. return string(r)
  252. }
  253. if k.baseRune != 0 {
  254. // If a baseRune is present, use it to represent a key using the standard
  255. // PC-101 key layout.
  256. s += runeStr(k.baseRune)
  257. } else if k.AltRune != 0 {
  258. // Otherwise, use the AltRune aka the non-shifted one if present.
  259. s += runeStr(k.AltRune)
  260. } else if k.Rune != 0 {
  261. // Else, just print the rune.
  262. s += runeStr(k.Rune)
  263. } else {
  264. s += k.Sym.String()
  265. }
  266. return s
  267. }
  268. // String implements fmt.Stringer and prints the string representation of a of
  269. // a Symbol key.
  270. func (k KeySym) String() string {
  271. s, ok := keySymString[k]
  272. if !ok {
  273. return "unknown"
  274. }
  275. return s
  276. }
  277. var keySymString = map[KeySym]string{
  278. KeyEnter: "enter",
  279. KeyTab: "tab",
  280. KeyBackspace: "backspace",
  281. KeyEscape: "esc",
  282. KeySpace: "space",
  283. KeyUp: "up",
  284. KeyDown: "down",
  285. KeyLeft: "left",
  286. KeyRight: "right",
  287. KeyBegin: "begin",
  288. KeyFind: "find",
  289. KeyInsert: "insert",
  290. KeyDelete: "delete",
  291. KeySelect: "select",
  292. KeyPgUp: "pgup",
  293. KeyPgDown: "pgdown",
  294. KeyHome: "home",
  295. KeyEnd: "end",
  296. KeyKpEnter: "kpenter",
  297. KeyKpEqual: "kpequal",
  298. KeyKpMultiply: "kpmul",
  299. KeyKpPlus: "kpplus",
  300. KeyKpComma: "kpcomma",
  301. KeyKpMinus: "kpminus",
  302. KeyKpDecimal: "kpperiod",
  303. KeyKpDivide: "kpdiv",
  304. KeyKp0: "kp0",
  305. KeyKp1: "kp1",
  306. KeyKp2: "kp2",
  307. KeyKp3: "kp3",
  308. KeyKp4: "kp4",
  309. KeyKp5: "kp5",
  310. KeyKp6: "kp6",
  311. KeyKp7: "kp7",
  312. KeyKp8: "kp8",
  313. KeyKp9: "kp9",
  314. // Kitty keyboard extension
  315. KeyKpSep: "kpsep",
  316. KeyKpUp: "kpup",
  317. KeyKpDown: "kpdown",
  318. KeyKpLeft: "kpleft",
  319. KeyKpRight: "kpright",
  320. KeyKpPgUp: "kppgup",
  321. KeyKpPgDown: "kppgdown",
  322. KeyKpHome: "kphome",
  323. KeyKpEnd: "kpend",
  324. KeyKpInsert: "kpinsert",
  325. KeyKpDelete: "kpdelete",
  326. KeyKpBegin: "kpbegin",
  327. KeyF1: "f1",
  328. KeyF2: "f2",
  329. KeyF3: "f3",
  330. KeyF4: "f4",
  331. KeyF5: "f5",
  332. KeyF6: "f6",
  333. KeyF7: "f7",
  334. KeyF8: "f8",
  335. KeyF9: "f9",
  336. KeyF10: "f10",
  337. KeyF11: "f11",
  338. KeyF12: "f12",
  339. KeyF13: "f13",
  340. KeyF14: "f14",
  341. KeyF15: "f15",
  342. KeyF16: "f16",
  343. KeyF17: "f17",
  344. KeyF18: "f18",
  345. KeyF19: "f19",
  346. KeyF20: "f20",
  347. KeyF21: "f21",
  348. KeyF22: "f22",
  349. KeyF23: "f23",
  350. KeyF24: "f24",
  351. KeyF25: "f25",
  352. KeyF26: "f26",
  353. KeyF27: "f27",
  354. KeyF28: "f28",
  355. KeyF29: "f29",
  356. KeyF30: "f30",
  357. KeyF31: "f31",
  358. KeyF32: "f32",
  359. KeyF33: "f33",
  360. KeyF34: "f34",
  361. KeyF35: "f35",
  362. KeyF36: "f36",
  363. KeyF37: "f37",
  364. KeyF38: "f38",
  365. KeyF39: "f39",
  366. KeyF40: "f40",
  367. KeyF41: "f41",
  368. KeyF42: "f42",
  369. KeyF43: "f43",
  370. KeyF44: "f44",
  371. KeyF45: "f45",
  372. KeyF46: "f46",
  373. KeyF47: "f47",
  374. KeyF48: "f48",
  375. KeyF49: "f49",
  376. KeyF50: "f50",
  377. KeyF51: "f51",
  378. KeyF52: "f52",
  379. KeyF53: "f53",
  380. KeyF54: "f54",
  381. KeyF55: "f55",
  382. KeyF56: "f56",
  383. KeyF57: "f57",
  384. KeyF58: "f58",
  385. KeyF59: "f59",
  386. KeyF60: "f60",
  387. KeyF61: "f61",
  388. KeyF62: "f62",
  389. KeyF63: "f63",
  390. // Kitty keyboard extension
  391. KeyCapsLock: "capslock",
  392. KeyScrollLock: "scrolllock",
  393. KeyNumLock: "numlock",
  394. KeyPrintScreen: "printscreen",
  395. KeyPause: "pause",
  396. KeyMenu: "menu",
  397. KeyMediaPlay: "mediaplay",
  398. KeyMediaPause: "mediapause",
  399. KeyMediaPlayPause: "mediaplaypause",
  400. KeyMediaReverse: "mediareverse",
  401. KeyMediaStop: "mediastop",
  402. KeyMediaFastForward: "mediafastforward",
  403. KeyMediaRewind: "mediarewind",
  404. KeyMediaNext: "medianext",
  405. KeyMediaPrev: "mediaprev",
  406. KeyMediaRecord: "mediarecord",
  407. KeyLowerVol: "lowervol",
  408. KeyRaiseVol: "raisevol",
  409. KeyMute: "mute",
  410. KeyLeftShift: "leftshift",
  411. KeyLeftAlt: "leftalt",
  412. KeyLeftCtrl: "leftctrl",
  413. KeyLeftSuper: "leftsuper",
  414. KeyLeftHyper: "lefthyper",
  415. KeyLeftMeta: "leftmeta",
  416. KeyRightShift: "rightshift",
  417. KeyRightAlt: "rightalt",
  418. KeyRightCtrl: "rightctrl",
  419. KeyRightSuper: "rightsuper",
  420. KeyRightHyper: "righthyper",
  421. KeyRightMeta: "rightmeta",
  422. KeyIsoLevel3Shift: "isolevel3shift",
  423. KeyIsoLevel5Shift: "isolevel5shift",
  424. }