keyboard.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license
  3. * Copyright 2020 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * KEY provides normalized string values for keys.
  25. */
  26. export var KEY = {
  27. UNKNOWN: 'Unknown',
  28. BACKSPACE: 'Backspace',
  29. ENTER: 'Enter',
  30. SPACEBAR: 'Spacebar',
  31. PAGE_UP: 'PageUp',
  32. PAGE_DOWN: 'PageDown',
  33. END: 'End',
  34. HOME: 'Home',
  35. ARROW_LEFT: 'ArrowLeft',
  36. ARROW_UP: 'ArrowUp',
  37. ARROW_RIGHT: 'ArrowRight',
  38. ARROW_DOWN: 'ArrowDown',
  39. DELETE: 'Delete',
  40. ESCAPE: 'Escape',
  41. TAB: 'Tab',
  42. };
  43. var normalizedKeys = new Set();
  44. // IE11 has no support for new Map with iterable so we need to initialize this
  45. // by hand.
  46. normalizedKeys.add(KEY.BACKSPACE);
  47. normalizedKeys.add(KEY.ENTER);
  48. normalizedKeys.add(KEY.SPACEBAR);
  49. normalizedKeys.add(KEY.PAGE_UP);
  50. normalizedKeys.add(KEY.PAGE_DOWN);
  51. normalizedKeys.add(KEY.END);
  52. normalizedKeys.add(KEY.HOME);
  53. normalizedKeys.add(KEY.ARROW_LEFT);
  54. normalizedKeys.add(KEY.ARROW_UP);
  55. normalizedKeys.add(KEY.ARROW_RIGHT);
  56. normalizedKeys.add(KEY.ARROW_DOWN);
  57. normalizedKeys.add(KEY.DELETE);
  58. normalizedKeys.add(KEY.ESCAPE);
  59. normalizedKeys.add(KEY.TAB);
  60. var KEY_CODE = {
  61. BACKSPACE: 8,
  62. ENTER: 13,
  63. SPACEBAR: 32,
  64. PAGE_UP: 33,
  65. PAGE_DOWN: 34,
  66. END: 35,
  67. HOME: 36,
  68. ARROW_LEFT: 37,
  69. ARROW_UP: 38,
  70. ARROW_RIGHT: 39,
  71. ARROW_DOWN: 40,
  72. DELETE: 46,
  73. ESCAPE: 27,
  74. TAB: 9,
  75. };
  76. var mappedKeyCodes = new Map();
  77. // IE11 has no support for new Map with iterable so we need to initialize this
  78. // by hand.
  79. mappedKeyCodes.set(KEY_CODE.BACKSPACE, KEY.BACKSPACE);
  80. mappedKeyCodes.set(KEY_CODE.ENTER, KEY.ENTER);
  81. mappedKeyCodes.set(KEY_CODE.SPACEBAR, KEY.SPACEBAR);
  82. mappedKeyCodes.set(KEY_CODE.PAGE_UP, KEY.PAGE_UP);
  83. mappedKeyCodes.set(KEY_CODE.PAGE_DOWN, KEY.PAGE_DOWN);
  84. mappedKeyCodes.set(KEY_CODE.END, KEY.END);
  85. mappedKeyCodes.set(KEY_CODE.HOME, KEY.HOME);
  86. mappedKeyCodes.set(KEY_CODE.ARROW_LEFT, KEY.ARROW_LEFT);
  87. mappedKeyCodes.set(KEY_CODE.ARROW_UP, KEY.ARROW_UP);
  88. mappedKeyCodes.set(KEY_CODE.ARROW_RIGHT, KEY.ARROW_RIGHT);
  89. mappedKeyCodes.set(KEY_CODE.ARROW_DOWN, KEY.ARROW_DOWN);
  90. mappedKeyCodes.set(KEY_CODE.DELETE, KEY.DELETE);
  91. mappedKeyCodes.set(KEY_CODE.ESCAPE, KEY.ESCAPE);
  92. mappedKeyCodes.set(KEY_CODE.TAB, KEY.TAB);
  93. var navigationKeys = new Set();
  94. // IE11 has no support for new Set with iterable so we need to initialize this
  95. // by hand.
  96. navigationKeys.add(KEY.PAGE_UP);
  97. navigationKeys.add(KEY.PAGE_DOWN);
  98. navigationKeys.add(KEY.END);
  99. navigationKeys.add(KEY.HOME);
  100. navigationKeys.add(KEY.ARROW_LEFT);
  101. navigationKeys.add(KEY.ARROW_UP);
  102. navigationKeys.add(KEY.ARROW_RIGHT);
  103. navigationKeys.add(KEY.ARROW_DOWN);
  104. /**
  105. * normalizeKey returns the normalized string for a navigational action.
  106. */
  107. export function normalizeKey(evt) {
  108. var key = evt.key;
  109. // If the event already has a normalized key, return it
  110. if (normalizedKeys.has(key)) {
  111. return key;
  112. }
  113. // tslint:disable-next-line:deprecation
  114. var mappedKey = mappedKeyCodes.get(evt.keyCode);
  115. if (mappedKey) {
  116. return mappedKey;
  117. }
  118. return KEY.UNKNOWN;
  119. }
  120. /**
  121. * isNavigationEvent returns whether the event is a navigation event
  122. */
  123. export function isNavigationEvent(evt) {
  124. return navigationKeys.has(normalizeKey(evt));
  125. }
  126. //# sourceMappingURL=keyboard.js.map