util.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import keysyms from "./keysymdef.js";
  2. import vkeys from "./vkeys.js";
  3. import fixedkeys from "./fixedkeys.js";
  4. import DOMKeyTable from "./domkeytable.js";
  5. import * as browser from "../util/browser.js";
  6. // Get 'KeyboardEvent.code', handling legacy browsers
  7. export function getKeycode(evt) {
  8. // Are we getting proper key identifiers?
  9. // (unfortunately Firefox and Chrome are crappy here and gives
  10. // us an empty string on some platforms, rather than leaving it
  11. // undefined)
  12. if (evt.code) {
  13. // Mozilla isn't fully in sync with the spec yet
  14. switch (evt.code) {
  15. case 'OSLeft': return 'MetaLeft';
  16. case 'OSRight': return 'MetaRight';
  17. }
  18. return evt.code;
  19. }
  20. // The de-facto standard is to use Windows Virtual-Key codes
  21. // in the 'keyCode' field for non-printable characters. However
  22. // Webkit sets it to the same as charCode in 'keypress' events.
  23. if ((evt.type !== 'keypress') && (evt.keyCode in vkeys)) {
  24. let code = vkeys[evt.keyCode];
  25. // macOS has messed up this code for some reason
  26. if (browser.isMac() && (code === 'ContextMenu')) {
  27. code = 'MetaRight';
  28. }
  29. // The keyCode doesn't distinguish between left and right
  30. // for the standard modifiers
  31. if (evt.location === 2) {
  32. switch (code) {
  33. case 'ShiftLeft': return 'ShiftRight';
  34. case 'ControlLeft': return 'ControlRight';
  35. case 'AltLeft': return 'AltRight';
  36. }
  37. }
  38. // Nor a bunch of the numpad keys
  39. if (evt.location === 3) {
  40. switch (code) {
  41. case 'Delete': return 'NumpadDecimal';
  42. case 'Insert': return 'Numpad0';
  43. case 'End': return 'Numpad1';
  44. case 'ArrowDown': return 'Numpad2';
  45. case 'PageDown': return 'Numpad3';
  46. case 'ArrowLeft': return 'Numpad4';
  47. case 'ArrowRight': return 'Numpad6';
  48. case 'Home': return 'Numpad7';
  49. case 'ArrowUp': return 'Numpad8';
  50. case 'PageUp': return 'Numpad9';
  51. case 'Enter': return 'NumpadEnter';
  52. }
  53. }
  54. return code;
  55. }
  56. return 'Unidentified';
  57. }
  58. // Get 'KeyboardEvent.key', handling legacy browsers
  59. export function getKey(evt) {
  60. // Are we getting a proper key value?
  61. if (evt.key !== undefined) {
  62. // IE and Edge use some ancient version of the spec
  63. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
  64. switch (evt.key) {
  65. case 'Spacebar': return ' ';
  66. case 'Esc': return 'Escape';
  67. case 'Scroll': return 'ScrollLock';
  68. case 'Win': return 'Meta';
  69. case 'Apps': return 'ContextMenu';
  70. case 'Up': return 'ArrowUp';
  71. case 'Left': return 'ArrowLeft';
  72. case 'Right': return 'ArrowRight';
  73. case 'Down': return 'ArrowDown';
  74. case 'Del': return 'Delete';
  75. case 'Divide': return '/';
  76. case 'Multiply': return '*';
  77. case 'Subtract': return '-';
  78. case 'Add': return '+';
  79. case 'Decimal': return evt.char;
  80. }
  81. // Mozilla isn't fully in sync with the spec yet
  82. switch (evt.key) {
  83. case 'OS': return 'Meta';
  84. }
  85. // iOS leaks some OS names
  86. switch (evt.key) {
  87. case 'UIKeyInputUpArrow': return 'ArrowUp';
  88. case 'UIKeyInputDownArrow': return 'ArrowDown';
  89. case 'UIKeyInputLeftArrow': return 'ArrowLeft';
  90. case 'UIKeyInputRightArrow': return 'ArrowRight';
  91. case 'UIKeyInputEscape': return 'Escape';
  92. }
  93. // IE and Edge have broken handling of AltGraph so we cannot
  94. // trust them for printable characters
  95. if ((evt.key.length !== 1) || (!browser.isIE() && !browser.isEdge())) {
  96. return evt.key;
  97. }
  98. }
  99. // Try to deduce it based on the physical key
  100. const code = getKeycode(evt);
  101. if (code in fixedkeys) {
  102. return fixedkeys[code];
  103. }
  104. // If that failed, then see if we have a printable character
  105. if (evt.charCode) {
  106. return String.fromCharCode(evt.charCode);
  107. }
  108. // At this point we have nothing left to go on
  109. return 'Unidentified';
  110. }
  111. // Get the most reliable keysym value we can get from a key event
  112. export function getKeysym(evt) {
  113. const key = getKey(evt);
  114. if (key === 'Unidentified') {
  115. return null;
  116. }
  117. // First look up special keys
  118. if (key in DOMKeyTable) {
  119. let location = evt.location;
  120. // Safari screws up location for the right cmd key
  121. if ((key === 'Meta') && (location === 0)) {
  122. location = 2;
  123. }
  124. if ((location === undefined) || (location > 3)) {
  125. location = 0;
  126. }
  127. return DOMKeyTable[key][location];
  128. }
  129. // Now we need to look at the Unicode symbol instead
  130. // Special key? (FIXME: Should have been caught earlier)
  131. if (key.length !== 1) {
  132. return null;
  133. }
  134. const codepoint = key.charCodeAt();
  135. if (codepoint) {
  136. return keysyms.lookup(codepoint);
  137. }
  138. return null;
  139. }