Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # keycode
  2. Simple map of keyboard codes.
  3. [![Build Status](https://travis-ci.org/timoxley/keycode.png?branch=master)](https://travis-ci.org/timoxley/keycode)
  4. ## Installation
  5. #### npm
  6. ```sh
  7. $ npm install keycode
  8. ```
  9. #### component
  10. ```sh
  11. $ component install timoxley/keycode
  12. ```
  13. ## Example
  14. ```js
  15. var keycode = require('keycode');
  16. document.addEventListener('keydown', function(e) {
  17. console.log("You pressed", keycode(e))
  18. })
  19. ```
  20. ## API
  21. `keycode` tries to make an intelligent guess as to what
  22. you're trying to discover based on the type of argument
  23. you supply.
  24. ### keycode(keycode:Event)
  25. Returns the name of the key associated with this event.
  26. ```js
  27. document.body.addEventListener('keyup', function(e) {
  28. console.log(keycode(e)) // prints name of key
  29. })
  30. ```
  31. [Due to the keypress event being weird](https://github.com/timoxley/keycode/wiki/wtf%3F-keydown,-keyup-vs-keypress),`keycode `currently does not support the `keypress` event, but this should not be an issue as `keydown` and `keyup` work perfectly fine.
  32. ### keycode(keycode:Number)
  33. Returns the lowercase name of a given numeric keycode.
  34. ```js
  35. keycode(13) // => 'enter'
  36. ```
  37. ### keycode(name:String)
  38. Returns the numeric keycode for given key name.
  39. ```js
  40. keycode('Enter') // => 13
  41. // keycode is not case sensitive
  42. keycode('eNtEr') // => 13
  43. ```
  44. ### Name Aliases
  45. Common aliases are also supplied:
  46. ```js
  47. > for (var alias in keycode.aliases) { console.log(alias, keycode(keycode(alias))) }
  48. ctl ctrl
  49. pause pause/break
  50. break pause/break
  51. caps caps lock
  52. escape esc
  53. pgup page up
  54. pgdn page down
  55. ins insert
  56. del delete
  57. spc space
  58. ```
  59. ## keycode.isEventKey(event: Event, nameOrCode: String | Number)
  60. Tests if an keyboard event against a given name or keycode.
  61. Will return `true` if the event matches the given name or keycode, `false` otherwise.
  62. ```js
  63. // assume event is an keydown event with key 'enter'
  64. keycode.isEventKey(event, 'enter') // => true
  65. keycode.isEventKey(event, 'down') // => false
  66. keycode.isEventKey(event, 13) // => true
  67. keycode.isEventKey(event, 40) // => false
  68. ```
  69. ## Maps
  70. Key code/name maps are available directly as `keycode.codes` and `keycode.names` respectively.
  71. ```js
  72. keycode.names[13] // => 'enter'
  73. keycode.codes['Enter'] // => 13
  74. ```
  75. ## Credit
  76. ```
  77. project : keycode
  78. repo age : 3 years, 8 months
  79. active : 29 days
  80. commits : 66
  81. files : 13
  82. authors :
  83. 49 Tim Oxley 74.3%
  84. 4 jkroso 6.1%
  85. 3 Amir Abu Shareb 4.5%
  86. 1 Greg Reimer 1.5%
  87. 1 Kenan Yildirim 1.5%
  88. 1 Abel Toledano 1.5%
  89. 1 Sam 1.5%
  90. 1 TJ Holowaychuk 1.5%
  91. 1 Yoshua Wuyts 1.5%
  92. 1 Nathan Zadoks 1.5%
  93. 1 Brenton Simpson 1.5%
  94. 1 Brian Noguchi 1.5%
  95. 1 Gilad Peleg 1.5%
  96. ```
  97. Original key mappings lifted from http://jsfiddle.net/vWx8V/ via http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes
  98. ## License
  99. [MIT](http://opensource.org/licenses/mit-license.php)