getKeyCode.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // key maps and utils retrieved from https://github.com/jaywcjlove/hotkeys
  2. /**
  3. * Includes a lot of leftover unused codes for the future in case we
  4. * want glyphs for them
  5. */
  6. const keyNameCodeMapping = {
  7. backspace: 8,
  8. tab: 9,
  9. clear: 12,
  10. enter: 13,
  11. return: 13,
  12. esc: 27,
  13. escape: 27,
  14. space: 32,
  15. left: 37,
  16. up: 38,
  17. right: 39,
  18. down: 40,
  19. del: 46,
  20. delete: 46,
  21. ins: 45,
  22. insert: 45,
  23. home: 36,
  24. end: 35,
  25. pageup: 33,
  26. pagedown: 34,
  27. capslock: 20,
  28. num_0: 96,
  29. num_1: 97,
  30. num_2: 98,
  31. num_3: 99,
  32. num_4: 100,
  33. num_5: 101,
  34. num_6: 102,
  35. num_7: 103,
  36. num_8: 104,
  37. num_9: 105,
  38. num_multiply: 106,
  39. num_add: 107,
  40. num_enter: 108,
  41. num_subtract: 109,
  42. num_decimal: 110,
  43. num_divide: 111,
  44. '⇪': 20,
  45. ',': 188,
  46. '.': 190,
  47. '/': 191,
  48. '`': 192,
  49. '-': 189,
  50. '=': 187,
  51. ';': 186,
  52. "'": 222,
  53. '[': 219,
  54. ']': 221,
  55. '\\': 220,
  56. };
  57. // Modifier Keys
  58. const modifierNameKeyCodeMapping = {
  59. // shiftKey
  60. '⇧': 16,
  61. shift: 16,
  62. // altKey
  63. '⌥': 18,
  64. alt: 18,
  65. option: 18,
  66. // ctrlKey
  67. '⌃': 17,
  68. ctrl: 17,
  69. control: 17,
  70. // metaKey
  71. '⌘': 91,
  72. cmd: 91,
  73. command: 91,
  74. };
  75. export const getKeyCode = (x: string): number =>
  76. keyNameCodeMapping[x.toLowerCase()] ||
  77. modifierNameKeyCodeMapping[x.toLowerCase()] ||
  78. x.toUpperCase().charCodeAt(0);