keyboardevent-key-polyfill.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* global define, KeyboardEvent, module */
  2. (function () {
  3. var keyboardeventKeyPolyfill = {
  4. polyfill: polyfill,
  5. keys: {
  6. 3: 'Cancel',
  7. 6: 'Help',
  8. 8: 'Backspace',
  9. 9: 'Tab',
  10. 12: 'Clear',
  11. 13: 'Enter',
  12. 16: 'Shift',
  13. 17: 'Control',
  14. 18: 'Alt',
  15. 19: 'Pause',
  16. 20: 'CapsLock',
  17. 27: 'Escape',
  18. 28: 'Convert',
  19. 29: 'NonConvert',
  20. 30: 'Accept',
  21. 31: 'ModeChange',
  22. 32: ' ',
  23. 33: 'PageUp',
  24. 34: 'PageDown',
  25. 35: 'End',
  26. 36: 'Home',
  27. 37: 'ArrowLeft',
  28. 38: 'ArrowUp',
  29. 39: 'ArrowRight',
  30. 40: 'ArrowDown',
  31. 41: 'Select',
  32. 42: 'Print',
  33. 43: 'Execute',
  34. 44: 'PrintScreen',
  35. 45: 'Insert',
  36. 46: 'Delete',
  37. 48: ['0', ')'],
  38. 49: ['1', '!'],
  39. 50: ['2', '@'],
  40. 51: ['3', '#'],
  41. 52: ['4', '$'],
  42. 53: ['5', '%'],
  43. 54: ['6', '^'],
  44. 55: ['7', '&'],
  45. 56: ['8', '*'],
  46. 57: ['9', '('],
  47. 91: 'OS',
  48. 93: 'ContextMenu',
  49. 144: 'NumLock',
  50. 145: 'ScrollLock',
  51. 181: 'VolumeMute',
  52. 182: 'VolumeDown',
  53. 183: 'VolumeUp',
  54. 186: [';', ':'],
  55. 187: ['=', '+'],
  56. 188: [',', '<'],
  57. 189: ['-', '_'],
  58. 190: ['.', '>'],
  59. 191: ['/', '?'],
  60. 192: ['`', '~'],
  61. 219: ['[', '{'],
  62. 220: ['\\', '|'],
  63. 221: [']', '}'],
  64. 222: ["'", '"'],
  65. 224: 'Meta',
  66. 225: 'AltGraph',
  67. 246: 'Attn',
  68. 247: 'CrSel',
  69. 248: 'ExSel',
  70. 249: 'EraseEof',
  71. 250: 'Play',
  72. 251: 'ZoomOut'
  73. }
  74. };
  75. // Function keys (F1-24).
  76. var i;
  77. for (i = 1; i < 25; i++) {
  78. keyboardeventKeyPolyfill.keys[111 + i] = 'F' + i;
  79. }
  80. // Printable ASCII characters.
  81. var letter = '';
  82. for (i = 65; i < 91; i++) {
  83. letter = String.fromCharCode(i);
  84. keyboardeventKeyPolyfill.keys[i] = [letter.toLowerCase(), letter.toUpperCase()];
  85. }
  86. function polyfill () {
  87. if (!('KeyboardEvent' in window) ||
  88. 'key' in KeyboardEvent.prototype) {
  89. return false;
  90. }
  91. // Polyfill `key` on `KeyboardEvent`.
  92. var proto = {
  93. get: function (x) {
  94. var key = keyboardeventKeyPolyfill.keys[this.which || this.keyCode];
  95. if (Array.isArray(key)) {
  96. key = key[+this.shiftKey];
  97. }
  98. return key;
  99. }
  100. };
  101. Object.defineProperty(KeyboardEvent.prototype, 'key', proto);
  102. return proto;
  103. }
  104. if (typeof define === 'function' && define.amd) {
  105. define('keyboardevent-key-polyfill', keyboardeventKeyPolyfill);
  106. } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
  107. module.exports = keyboardeventKeyPolyfill;
  108. } else if (window) {
  109. window.keyboardeventKeyPolyfill = keyboardeventKeyPolyfill;
  110. }
  111. })();