index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Source: http://jsfiddle.net/vWx8V/
  2. // http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes
  3. /**
  4. * Conenience method returns corresponding value for given keyName or keyCode.
  5. *
  6. * @param {Mixed} keyCode {Number} or keyName {String}
  7. * @return {Mixed}
  8. * @api public
  9. */
  10. function keyCode(searchInput) {
  11. // Keyboard Events
  12. if (searchInput && 'object' === typeof searchInput) {
  13. var hasKeyCode = searchInput.which || searchInput.keyCode || searchInput.charCode
  14. if (hasKeyCode) searchInput = hasKeyCode
  15. }
  16. // Numbers
  17. if ('number' === typeof searchInput) return names[searchInput]
  18. // Everything else (cast to string)
  19. var search = String(searchInput)
  20. // check codes
  21. var foundNamedKey = codes[search.toLowerCase()]
  22. if (foundNamedKey) return foundNamedKey
  23. // check aliases
  24. var foundNamedKey = aliases[search.toLowerCase()]
  25. if (foundNamedKey) return foundNamedKey
  26. // weird character?
  27. if (search.length === 1) return search.charCodeAt(0)
  28. return undefined
  29. }
  30. /**
  31. * Compares a keyboard event with a given keyCode or keyName.
  32. *
  33. * @param {Event} event Keyboard event that should be tested
  34. * @param {Mixed} keyCode {Number} or keyName {String}
  35. * @return {Boolean}
  36. * @api public
  37. */
  38. keyCode.isEventKey = function isEventKey(event, nameOrCode) {
  39. if (event && 'object' === typeof event) {
  40. var keyCode = event.which || event.keyCode || event.charCode
  41. if (keyCode === null || keyCode === undefined) { return false; }
  42. if (typeof nameOrCode === 'string') {
  43. // check codes
  44. var foundNamedKey = codes[nameOrCode.toLowerCase()]
  45. if (foundNamedKey) { return foundNamedKey === keyCode; }
  46. // check aliases
  47. var foundNamedKey = aliases[nameOrCode.toLowerCase()]
  48. if (foundNamedKey) { return foundNamedKey === keyCode; }
  49. } else if (typeof nameOrCode === 'number') {
  50. return nameOrCode === keyCode;
  51. }
  52. return false;
  53. }
  54. }
  55. exports = module.exports = keyCode;
  56. /**
  57. * Get by name
  58. *
  59. * exports.code['enter'] // => 13
  60. */
  61. var codes = exports.code = exports.codes = {
  62. 'backspace': 8,
  63. 'tab': 9,
  64. 'enter': 13,
  65. 'shift': 16,
  66. 'ctrl': 17,
  67. 'alt': 18,
  68. 'pause/break': 19,
  69. 'caps lock': 20,
  70. 'esc': 27,
  71. 'space': 32,
  72. 'page up': 33,
  73. 'page down': 34,
  74. 'end': 35,
  75. 'home': 36,
  76. 'left': 37,
  77. 'up': 38,
  78. 'right': 39,
  79. 'down': 40,
  80. 'insert': 45,
  81. 'delete': 46,
  82. 'command': 91,
  83. 'left command': 91,
  84. 'right command': 93,
  85. 'numpad *': 106,
  86. 'numpad +': 107,
  87. 'numpad -': 109,
  88. 'numpad .': 110,
  89. 'numpad /': 111,
  90. 'num lock': 144,
  91. 'scroll lock': 145,
  92. 'my computer': 182,
  93. 'my calculator': 183,
  94. ';': 186,
  95. '=': 187,
  96. ',': 188,
  97. '-': 189,
  98. '.': 190,
  99. '/': 191,
  100. '`': 192,
  101. '[': 219,
  102. '\\': 220,
  103. ']': 221,
  104. "'": 222
  105. }
  106. // Helper aliases
  107. var aliases = exports.aliases = {
  108. 'windows': 91,
  109. '⇧': 16,
  110. '⌥': 18,
  111. '⌃': 17,
  112. '⌘': 91,
  113. 'ctl': 17,
  114. 'control': 17,
  115. 'option': 18,
  116. 'pause': 19,
  117. 'break': 19,
  118. 'caps': 20,
  119. 'return': 13,
  120. 'escape': 27,
  121. 'spc': 32,
  122. 'spacebar': 32,
  123. 'pgup': 33,
  124. 'pgdn': 34,
  125. 'ins': 45,
  126. 'del': 46,
  127. 'cmd': 91
  128. }
  129. /*!
  130. * Programatically add the following
  131. */
  132. // lower case chars
  133. for (i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32
  134. // numbers
  135. for (var i = 48; i < 58; i++) codes[i - 48] = i
  136. // function keys
  137. for (i = 1; i < 13; i++) codes['f'+i] = i + 111
  138. // numpad keys
  139. for (i = 0; i < 10; i++) codes['numpad '+i] = i + 96
  140. /**
  141. * Get by code
  142. *
  143. * exports.name[13] // => 'Enter'
  144. */
  145. var names = exports.names = exports.title = {} // title for backward compat
  146. // Create reverse mapping
  147. for (i in codes) names[codes[i]] = i
  148. // Add aliases
  149. for (var alias in aliases) {
  150. codes[alias] = aliases[alias]
  151. }