index.es.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. export var base = {
  2. 8: "Backspace",
  3. 9: "Tab",
  4. 10: "Enter",
  5. 12: "NumLock",
  6. 13: "Enter",
  7. 16: "Shift",
  8. 17: "Control",
  9. 18: "Alt",
  10. 20: "CapsLock",
  11. 27: "Escape",
  12. 32: " ",
  13. 33: "PageUp",
  14. 34: "PageDown",
  15. 35: "End",
  16. 36: "Home",
  17. 37: "ArrowLeft",
  18. 38: "ArrowUp",
  19. 39: "ArrowRight",
  20. 40: "ArrowDown",
  21. 44: "PrintScreen",
  22. 45: "Insert",
  23. 46: "Delete",
  24. 59: ";",
  25. 61: "=",
  26. 91: "Meta",
  27. 92: "Meta",
  28. 106: "*",
  29. 107: "+",
  30. 108: ",",
  31. 109: "-",
  32. 110: ".",
  33. 111: "/",
  34. 144: "NumLock",
  35. 145: "ScrollLock",
  36. 160: "Shift",
  37. 161: "Shift",
  38. 162: "Control",
  39. 163: "Control",
  40. 164: "Alt",
  41. 165: "Alt",
  42. 173: "-",
  43. 186: ";",
  44. 187: "=",
  45. 188: ",",
  46. 189: "-",
  47. 190: ".",
  48. 191: "/",
  49. 192: "`",
  50. 219: "[",
  51. 220: "\\",
  52. 221: "]",
  53. 222: "'",
  54. 229: "q"
  55. }
  56. export var shift = {
  57. 48: ")",
  58. 49: "!",
  59. 50: "@",
  60. 51: "#",
  61. 52: "$",
  62. 53: "%",
  63. 54: "^",
  64. 55: "&",
  65. 56: "*",
  66. 57: "(",
  67. 59: ":",
  68. 61: "+",
  69. 173: "_",
  70. 186: ":",
  71. 187: "+",
  72. 188: "<",
  73. 189: "_",
  74. 190: ">",
  75. 191: "?",
  76. 192: "~",
  77. 219: "{",
  78. 220: "|",
  79. 221: "}",
  80. 222: "\"",
  81. 229: "Q"
  82. }
  83. var chrome = typeof navigator != "undefined" && /Chrome\/(\d+)/.exec(navigator.userAgent)
  84. var safari = typeof navigator != "undefined" && /Apple Computer/.test(navigator.vendor)
  85. var gecko = typeof navigator != "undefined" && /Gecko\/\d+/.test(navigator.userAgent)
  86. var mac = typeof navigator != "undefined" && /Mac/.test(navigator.platform)
  87. var ie = typeof navigator != "undefined" && /MSIE \d|Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent)
  88. var brokenModifierNames = chrome && (mac || +chrome[1] < 57) || gecko && mac
  89. // Fill in the digit keys
  90. for (var i = 0; i < 10; i++) base[48 + i] = base[96 + i] = String(i)
  91. // The function keys
  92. for (var i = 1; i <= 24; i++) base[i + 111] = "F" + i
  93. // And the alphabetic keys
  94. for (var i = 65; i <= 90; i++) {
  95. base[i] = String.fromCharCode(i + 32)
  96. shift[i] = String.fromCharCode(i)
  97. }
  98. // For each code that doesn't have a shift-equivalent, copy the base name
  99. for (var code in base) if (!shift.hasOwnProperty(code)) shift[code] = base[code]
  100. export function keyName(event) {
  101. // Don't trust event.key in Chrome when there are modifiers until
  102. // they fix https://bugs.chromium.org/p/chromium/issues/detail?id=633838
  103. var ignoreKey = brokenModifierNames && (event.ctrlKey || event.altKey || event.metaKey) ||
  104. (safari || ie) && event.shiftKey && event.key && event.key.length == 1
  105. var name = (!ignoreKey && event.key) ||
  106. (event.shiftKey ? shift : base)[event.keyCode] ||
  107. event.key || "Unidentified"
  108. // Edge sometimes produces wrong names (Issue #3)
  109. if (name == "Esc") name = "Escape"
  110. if (name == "Del") name = "Delete"
  111. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
  112. if (name == "Left") name = "ArrowLeft"
  113. if (name == "Up") name = "ArrowUp"
  114. if (name == "Right") name = "ArrowRight"
  115. if (name == "Down") name = "ArrowDown"
  116. return name
  117. }