├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── dist └── reactKeypress.js ├── index.html ├── package.json ├── react-keypress.js ├── test.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | bower_components 4 | dummy -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Hector Leon Zarco Garcia 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 all 13 | 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 THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React keypress [![NPM](https://img.shields.io/npm/v/react-keypress.svg)](https://www.npmjs.org/package/react-keypress) [![Bower version](https://badge.fury.io/bo/react-keypress.svg)](http://badge.fury.io/bo/react-keypress) 2 | 3 | > multi Keypress detection handlers for React.js 4 | 5 | React wrapper for Keypress.js - http://dmauro.github.io/Keypress 6 | 7 | Keypress is an input capture library which can be used to capture combination of keys in a simple way. 8 | 9 | ## Installation 10 | 11 | ``` 12 | $ npm install react-keypress 13 | ``` 14 | or 15 | ``` 16 | $ bower install react-keypress 17 | ``` 18 | 19 | 20 | ## Usage 21 | 22 | Lets say that you want to fire a function when the user press `shift + enter + a`: 23 | 24 | ```javascript 25 | var React = require("react"); 26 | var Keypress = require("react-keypress"); 27 | 28 | React.createClass({ 29 | sayHello() { 30 | alert("hello!"); 31 | }, 32 | 33 | render() { 34 |
35 | 36 |
37 | } 38 | }); 39 | ``` 40 | 41 | ## API 42 | 43 | ### Keypress(keysCombination, handler) 44 | 45 | #### keysCombination 46 | 47 | Type: `String` 48 | 49 | This option can be either an array of strings, or a single space separated string of key names that describe the keys that make up the combo. 50 | 51 | #### handler 52 | 53 | Type: `Function` 54 | 55 | This is a function that gets called everytime the keypress event for our combo is fired. 56 | 57 | ### Special keys support 58 | 59 | * backspace 60 | * tab 61 | * num 62 | * enter 63 | * shift 64 | * ctrl 65 | * alt 66 | * pause 67 | * caps 68 | * esc 69 | * space 70 | * pageup 71 | * pagedown 72 | * end 73 | * home 74 | * left 75 | * up 76 | * right 77 | * down 78 | * print 79 | * insert 80 | * delete 81 | * cmd 82 | 83 | ## Created by 84 | 85 | Hector Leon Zarco Garcia - [@zzarcon](https://twitter.com/zzarcon) 86 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-keypress", 3 | "main": "react-keypress.js", 4 | "version": "0.1.5", 5 | "homepage": "https://github.com/zzarcon/react-keypress", 6 | "authors": [ 7 | "zzarcon" 8 | ], 9 | "license": "MIT", 10 | "ignore": [ 11 | "**/.*", 12 | "node_modules", 13 | "bower_components", 14 | "test", 15 | "tests" 16 | ], 17 | "dependencies": { 18 | "Keypress": "2.1.3" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /dist/reactKeypress.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define(factory); 6 | else if(typeof exports === 'object') 7 | exports["ReactKeypress"] = factory(); 8 | else 9 | root["ReactKeypress"] = factory(); 10 | })(this, function() { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | /******/ 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | /******/ 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) 20 | /******/ return installedModules[moduleId].exports; 21 | /******/ 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ exports: {}, 25 | /******/ id: moduleId, 26 | /******/ loaded: false 27 | /******/ }; 28 | /******/ 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | /******/ 32 | /******/ // Flag the module as loaded 33 | /******/ module.loaded = true; 34 | /******/ 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | /******/ 39 | /******/ 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | /******/ 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | /******/ 46 | /******/ // __webpack_public_path__ 47 | /******/ __webpack_require__.p = "http://localhost:8090/assets"; 48 | /******/ 49 | /******/ // Load entry module and return exports 50 | /******/ return __webpack_require__(0); 51 | /******/ }) 52 | /************************************************************************/ 53 | /******/ ([ 54 | /* 0 */ 55 | /***/ function(module, exports, __webpack_require__) { 56 | 57 | "use strict"; 58 | 59 | var Keypress = __webpack_require__(1); 60 | var elements = new WeakMap(); 61 | 62 | module.exports = function (keys, handler) { 63 | if (!keys || !handler) return; 64 | 65 | return function (e) { 66 | var el = e.target; 67 | 68 | //Check for no register the same element multiple times 69 | if (elements.has(el)) return; 70 | 71 | var listener = new Keypress.Listener(el); 72 | 73 | elements.set(el, true); 74 | listener.register_combo({ 75 | "keys": keys, 76 | "on_keyup": handler 77 | }); 78 | }; 79 | }; 80 | 81 | /***/ }, 82 | /* 1 */ 83 | /***/ function(module, exports, __webpack_require__) { 84 | 85 | var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;// Generated by CoffeeScript 1.8.0 86 | 87 | /* 88 | Copyright 2014 David Mauro 89 | 90 | Licensed under the Apache License, Version 2.0 (the "License"); 91 | you may not use this file except in compliance with the License. 92 | You may obtain a copy of the License at 93 | 94 | http://www.apache.org/licenses/LICENSE-2.0 95 | 96 | Unless required by applicable law or agreed to in writing, software 97 | distributed under the License is distributed on an "AS IS" BASIS, 98 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 99 | See the License for the specific language governing permissions and 100 | limitations under the License. 101 | 102 | Keypress is a robust keyboard input capturing Javascript utility 103 | focused on input for games. 104 | 105 | version 2.1.3 106 | */ 107 | 108 | 109 | /* 110 | Combo options available and their defaults: 111 | keys : [] - An array of the keys pressed together to activate combo. 112 | count : 0 - The number of times a counting combo has been pressed. Reset on release. 113 | is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. 114 | is_counting : false - Makes this a counting combo (see documentation). 115 | is_exclusive : false - This combo will replace other exclusive combos when true. 116 | is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. 117 | is_sequence : false - Rather than a key combo, this is an ordered key sequence. 118 | prevent_default : false - Prevent default behavior for all component key keypresses. 119 | prevent_repeat : false - Prevent the combo from repeating when keydown is held. 120 | on_keydown : null - A function that is called when the combo is pressed. 121 | on_keyup : null - A function that is called when the combo is released. 122 | on_release : null - A function that is called when all keys in the combo are released. 123 | this : undefined - Defines the scope for your callback functions. 124 | */ 125 | 126 | (function() { 127 | var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, 128 | __hasProp = {}.hasOwnProperty, 129 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; 130 | 131 | _factory_defaults = { 132 | is_unordered: false, 133 | is_counting: false, 134 | is_exclusive: false, 135 | is_solitary: false, 136 | prevent_default: false, 137 | prevent_repeat: false 138 | }; 139 | 140 | _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; 141 | 142 | _metakey = "ctrl"; 143 | 144 | keypress = {}; 145 | 146 | keypress.debug = false; 147 | 148 | Combo = (function() { 149 | function Combo(dictionary) { 150 | var property, value; 151 | for (property in dictionary) { 152 | if (!__hasProp.call(dictionary, property)) continue; 153 | value = dictionary[property]; 154 | if (value !== false) { 155 | this[property] = value; 156 | } 157 | } 158 | this.keys = this.keys || []; 159 | this.count = this.count || 0; 160 | } 161 | 162 | Combo.prototype.allows_key_repeat = function() { 163 | return !this.prevent_repeat && typeof this.on_keydown === "function"; 164 | }; 165 | 166 | Combo.prototype.reset = function() { 167 | this.count = 0; 168 | return this.keyup_fired = null; 169 | }; 170 | 171 | return Combo; 172 | 173 | })(); 174 | 175 | keypress.Listener = (function() { 176 | function Listener(element, defaults) { 177 | var attach_handler, property, value; 178 | if ((typeof jQuery !== "undefined" && jQuery !== null) && element instanceof jQuery) { 179 | if (element.length !== 1) { 180 | _log_error("Warning: your jQuery selector should have exactly one object."); 181 | } 182 | element = element[0]; 183 | } 184 | this.should_suppress_event_defaults = false; 185 | this.should_force_event_defaults = false; 186 | this.sequence_delay = 800; 187 | this._registered_combos = []; 188 | this._keys_down = []; 189 | this._active_combos = []; 190 | this._sequence = []; 191 | this._sequence_timer = null; 192 | this._prevent_capture = false; 193 | this._defaults = defaults || {}; 194 | for (property in _factory_defaults) { 195 | if (!__hasProp.call(_factory_defaults, property)) continue; 196 | value = _factory_defaults[property]; 197 | this._defaults[property] = this._defaults[property] || value; 198 | } 199 | this.element = element || document.body; 200 | attach_handler = function(target, event, handler) { 201 | if (target.addEventListener) { 202 | target.addEventListener(event, handler); 203 | } else if (target.attachEvent) { 204 | target.attachEvent("on" + event, handler); 205 | } 206 | return handler; 207 | }; 208 | this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { 209 | return function(e) { 210 | e = e || window.event; 211 | _this._receive_input(e, true); 212 | return _this._bug_catcher(e); 213 | }; 214 | })(this)); 215 | this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { 216 | return function(e) { 217 | e = e || window.event; 218 | return _this._receive_input(e, false); 219 | }; 220 | })(this)); 221 | this.blur_event = attach_handler(window, "blur", (function(_this) { 222 | return function() { 223 | var key, _i, _len, _ref; 224 | _ref = _this._keys_down; 225 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 226 | key = _ref[_i]; 227 | _this._key_up(key, {}); 228 | } 229 | return _this._keys_down = []; 230 | }; 231 | })(this)); 232 | } 233 | 234 | Listener.prototype.destroy = function() { 235 | var remove_handler; 236 | remove_handler = function(target, event, handler) { 237 | if (target.removeEventListener != null) { 238 | return target.removeEventListener(event, handler); 239 | } else if (target.removeEvent != null) { 240 | return target.removeEvent("on" + event, handler); 241 | } 242 | }; 243 | remove_handler(this.element, "keydown", this.keydown_event); 244 | remove_handler(this.element, "keyup", this.keyup_event); 245 | return remove_handler(window, "blur", this.blur_event); 246 | }; 247 | 248 | Listener.prototype._bug_catcher = function(e) { 249 | var _ref; 250 | if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable(e.keyCode)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { 251 | return this._receive_input(e, false); 252 | } 253 | }; 254 | 255 | Listener.prototype._cmd_bug_check = function(combo_keys) { 256 | if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { 257 | return false; 258 | } 259 | return true; 260 | }; 261 | 262 | Listener.prototype._prevent_default = function(e, should_prevent) { 263 | if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { 264 | if (e.preventDefault) { 265 | e.preventDefault(); 266 | } else { 267 | e.returnValue = false; 268 | } 269 | if (e.stopPropagation) { 270 | return e.stopPropagation(); 271 | } 272 | } 273 | }; 274 | 275 | Listener.prototype._get_active_combos = function(key) { 276 | var active_combos, keys_down; 277 | active_combos = []; 278 | keys_down = _filter_array(this._keys_down, function(down_key) { 279 | return down_key !== key; 280 | }); 281 | keys_down.push(key); 282 | this._match_combo_arrays(keys_down, (function(_this) { 283 | return function(match) { 284 | if (_this._cmd_bug_check(match.keys)) { 285 | return active_combos.push(match); 286 | } 287 | }; 288 | })(this)); 289 | this._fuzzy_match_combo_arrays(keys_down, (function(_this) { 290 | return function(match) { 291 | if (__indexOf.call(active_combos, match) >= 0) { 292 | return; 293 | } 294 | if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { 295 | return active_combos.push(match); 296 | } 297 | }; 298 | })(this)); 299 | return active_combos; 300 | }; 301 | 302 | Listener.prototype._get_potential_combos = function(key) { 303 | var combo, potentials, _i, _len, _ref; 304 | potentials = []; 305 | _ref = this._registered_combos; 306 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 307 | combo = _ref[_i]; 308 | if (combo.is_sequence) { 309 | continue; 310 | } 311 | if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { 312 | potentials.push(combo); 313 | } 314 | } 315 | return potentials; 316 | }; 317 | 318 | Listener.prototype._add_to_active_combos = function(combo) { 319 | var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; 320 | should_replace = false; 321 | should_prepend = true; 322 | already_replaced = false; 323 | if (__indexOf.call(this._active_combos, combo) >= 0) { 324 | return true; 325 | } else if (this._active_combos.length) { 326 | for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 327 | active_combo = this._active_combos[i]; 328 | if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { 329 | continue; 330 | } 331 | active_keys = active_combo.keys; 332 | if (!should_replace) { 333 | for (_j = 0, _len = active_keys.length; _j < _len; _j++) { 334 | active_key = active_keys[_j]; 335 | should_replace = true; 336 | if (__indexOf.call(combo.keys, active_key) < 0) { 337 | should_replace = false; 338 | break; 339 | } 340 | } 341 | } 342 | if (should_prepend && !should_replace) { 343 | _ref1 = combo.keys; 344 | for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { 345 | combo_key = _ref1[_k]; 346 | should_prepend = false; 347 | if (__indexOf.call(active_keys, combo_key) < 0) { 348 | should_prepend = true; 349 | break; 350 | } 351 | } 352 | } 353 | if (should_replace) { 354 | if (already_replaced) { 355 | active_combo = this._active_combos.splice(i, 1)[0]; 356 | if (active_combo != null) { 357 | active_combo.reset(); 358 | } 359 | } else { 360 | active_combo = this._active_combos.splice(i, 1, combo)[0]; 361 | if (active_combo != null) { 362 | active_combo.reset(); 363 | } 364 | already_replaced = true; 365 | } 366 | should_prepend = false; 367 | } 368 | } 369 | } 370 | if (should_prepend) { 371 | this._active_combos.unshift(combo); 372 | } 373 | return should_replace || should_prepend; 374 | }; 375 | 376 | Listener.prototype._remove_from_active_combos = function(combo) { 377 | var active_combo, i, _i, _ref; 378 | for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 379 | active_combo = this._active_combos[i]; 380 | if (active_combo === combo) { 381 | combo = this._active_combos.splice(i, 1)[0]; 382 | combo.reset(); 383 | break; 384 | } 385 | } 386 | }; 387 | 388 | Listener.prototype._get_possible_sequences = function() { 389 | var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; 390 | matches = []; 391 | _ref = this._registered_combos; 392 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 393 | combo = _ref[_i]; 394 | for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { 395 | sequence = this._sequence.slice(-j); 396 | if (!combo.is_sequence) { 397 | continue; 398 | } 399 | if (__indexOf.call(combo.keys, "shift") < 0) { 400 | sequence = _filter_array(sequence, function(key) { 401 | return key !== "shift"; 402 | }); 403 | if (!sequence.length) { 404 | continue; 405 | } 406 | } 407 | for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { 408 | if (combo.keys[i] === sequence[i]) { 409 | match = true; 410 | } else { 411 | match = false; 412 | break; 413 | } 414 | } 415 | if (match) { 416 | matches.push(combo); 417 | } 418 | } 419 | } 420 | return matches; 421 | }; 422 | 423 | Listener.prototype._add_key_to_sequence = function(key, e) { 424 | var combo, sequence_combos, _i, _len; 425 | this._sequence.push(key); 426 | sequence_combos = this._get_possible_sequences(); 427 | if (sequence_combos.length) { 428 | for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { 429 | combo = sequence_combos[_i]; 430 | this._prevent_default(e, combo.prevent_default); 431 | } 432 | if (this._sequence_timer) { 433 | clearTimeout(this._sequence_timer); 434 | } 435 | if (this.sequence_delay > -1) { 436 | this._sequence_timer = setTimeout(function() { 437 | return this._sequence = []; 438 | }, this.sequence_delay); 439 | } 440 | } else { 441 | this._sequence = []; 442 | } 443 | }; 444 | 445 | Listener.prototype._get_sequence = function(key) { 446 | var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; 447 | _ref = this._registered_combos; 448 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 449 | combo = _ref[_i]; 450 | if (!combo.is_sequence) { 451 | continue; 452 | } 453 | for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { 454 | sequence = (_filter_array(this._sequence, function(seq_key) { 455 | if (__indexOf.call(combo.keys, "shift") >= 0) { 456 | return true; 457 | } 458 | return seq_key !== "shift"; 459 | })).slice(-j); 460 | if (combo.keys.length !== sequence.length) { 461 | continue; 462 | } 463 | for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { 464 | seq_key = sequence[i]; 465 | if (__indexOf.call(combo.keys, "shift") < 0) { 466 | if (seq_key === "shift") { 467 | continue; 468 | } 469 | } 470 | if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { 471 | continue; 472 | } 473 | if (combo.keys[i] === seq_key) { 474 | match = true; 475 | } else { 476 | match = false; 477 | break; 478 | } 479 | } 480 | } 481 | if (match) { 482 | debugger; 483 | if (combo.is_exclusive) { 484 | this._sequence = []; 485 | } 486 | return combo; 487 | } 488 | } 489 | return false; 490 | }; 491 | 492 | Listener.prototype._receive_input = function(e, is_keydown) { 493 | var key; 494 | if (this._prevent_capture) { 495 | if (this._keys_down.length) { 496 | this._keys_down = []; 497 | } 498 | return; 499 | } 500 | key = _convert_key_to_readable(e.keyCode); 501 | if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { 502 | return; 503 | } 504 | if (!key) { 505 | return; 506 | } 507 | if (is_keydown) { 508 | return this._key_down(key, e); 509 | } else { 510 | return this._key_up(key, e); 511 | } 512 | }; 513 | 514 | Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { 515 | if (typeof combo["on_" + event] === "function") { 516 | this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); 517 | } 518 | if (event === "release") { 519 | combo.count = 0; 520 | } 521 | if (event === "keyup") { 522 | return combo.keyup_fired = true; 523 | } 524 | }; 525 | 526 | Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { 527 | var source_combo, _i, _len, _ref; 528 | _ref = this._registered_combos; 529 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 530 | source_combo = _ref[_i]; 531 | if ((!source_combo.is_unordered && _compare_arrays_sorted(potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(potential_match, source_combo.keys))) { 532 | match_handler(source_combo); 533 | } 534 | } 535 | }; 536 | 537 | Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { 538 | var source_combo, _i, _len, _ref; 539 | _ref = this._registered_combos; 540 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 541 | source_combo = _ref[_i]; 542 | if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { 543 | match_handler(source_combo); 544 | } 545 | } 546 | }; 547 | 548 | Listener.prototype._keys_remain = function(combo) { 549 | var key, keys_remain, _i, _len, _ref; 550 | _ref = combo.keys; 551 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 552 | key = _ref[_i]; 553 | if (__indexOf.call(this._keys_down, key) >= 0) { 554 | keys_remain = true; 555 | break; 556 | } 557 | } 558 | return keys_remain; 559 | }; 560 | 561 | Listener.prototype._key_down = function(key, e) { 562 | var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; 563 | shifted_key = _convert_to_shifted_key(key, e); 564 | if (shifted_key) { 565 | key = shifted_key; 566 | } 567 | this._add_key_to_sequence(key, e); 568 | sequence_combo = this._get_sequence(key); 569 | if (sequence_combo) { 570 | this._fire("keydown", sequence_combo, e); 571 | } 572 | for (mod in _modifier_event_mapping) { 573 | event_mod = _modifier_event_mapping[mod]; 574 | if (!e[event_mod]) { 575 | continue; 576 | } 577 | if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { 578 | continue; 579 | } 580 | this._keys_down.push(mod); 581 | } 582 | for (mod in _modifier_event_mapping) { 583 | event_mod = _modifier_event_mapping[mod]; 584 | if (mod === key) { 585 | continue; 586 | } 587 | if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { 588 | if (mod === "cmd" && _metakey !== "cmd") { 589 | continue; 590 | } 591 | for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 592 | if (this._keys_down[i] === mod) { 593 | this._keys_down.splice(i, 1); 594 | } 595 | } 596 | } 597 | } 598 | combos = this._get_active_combos(key); 599 | potential_combos = this._get_potential_combos(key); 600 | for (_j = 0, _len = combos.length; _j < _len; _j++) { 601 | combo = combos[_j]; 602 | this._handle_combo_down(combo, potential_combos, key, e); 603 | } 604 | if (potential_combos.length) { 605 | for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { 606 | potential = potential_combos[_k]; 607 | this._prevent_default(e, potential.prevent_default); 608 | } 609 | } 610 | if (__indexOf.call(this._keys_down, key) < 0) { 611 | this._keys_down.push(key); 612 | } 613 | }; 614 | 615 | Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { 616 | var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; 617 | if (__indexOf.call(combo.keys, key) < 0) { 618 | return false; 619 | } 620 | this._prevent_default(e, combo && combo.prevent_default); 621 | is_autorepeat = false; 622 | if (__indexOf.call(this._keys_down, key) >= 0) { 623 | is_autorepeat = true; 624 | if (!combo.allows_key_repeat()) { 625 | return false; 626 | } 627 | } 628 | result = this._add_to_active_combos(combo, key); 629 | combo.keyup_fired = false; 630 | is_other_exclusive = false; 631 | if (combo.is_exclusive) { 632 | for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { 633 | potential_combo = potential_combos[_i]; 634 | if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { 635 | is_other_exclusive = true; 636 | break; 637 | } 638 | } 639 | } 640 | if (!is_other_exclusive) { 641 | if (combo.is_counting && typeof combo.on_keydown === "function") { 642 | combo.count += 1; 643 | } 644 | if (result) { 645 | return this._fire("keydown", combo, e, is_autorepeat); 646 | } 647 | } 648 | }; 649 | 650 | Listener.prototype._key_up = function(key, e) { 651 | var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; 652 | unshifted_key = key; 653 | shifted_key = _convert_to_shifted_key(key, e); 654 | if (shifted_key) { 655 | key = shifted_key; 656 | } 657 | shifted_key = _keycode_shifted_keys[unshifted_key]; 658 | if (e.shiftKey) { 659 | if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { 660 | key = unshifted_key; 661 | } 662 | } else { 663 | if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { 664 | key = shifted_key; 665 | } 666 | } 667 | sequence_combo = this._get_sequence(key); 668 | if (sequence_combo) { 669 | this._fire("keyup", sequence_combo, e); 670 | } 671 | if (__indexOf.call(this._keys_down, key) < 0) { 672 | return false; 673 | } 674 | for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 675 | if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { 676 | this._keys_down.splice(i, 1); 677 | break; 678 | } 679 | } 680 | active_combos_length = this._active_combos.length; 681 | combos = []; 682 | _ref2 = this._active_combos; 683 | for (_j = 0, _len = _ref2.length; _j < _len; _j++) { 684 | active_combo = _ref2[_j]; 685 | if (__indexOf.call(active_combo.keys, key) >= 0) { 686 | combos.push(active_combo); 687 | } 688 | } 689 | for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { 690 | combo = combos[_k]; 691 | this._handle_combo_up(combo, e, key); 692 | } 693 | if (active_combos_length > 1) { 694 | _ref3 = this._active_combos; 695 | for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { 696 | active_combo = _ref3[_l]; 697 | if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { 698 | continue; 699 | } 700 | if (!this._keys_remain(active_combo)) { 701 | this._remove_from_active_combos(active_combo); 702 | } 703 | } 704 | } 705 | }; 706 | 707 | Listener.prototype._handle_combo_up = function(combo, e, key) { 708 | var keys_down, keys_remaining; 709 | this._prevent_default(e, combo && combo.prevent_default); 710 | keys_remaining = this._keys_remain(combo); 711 | if (!combo.keyup_fired) { 712 | keys_down = this._keys_down.slice(); 713 | keys_down.push(key); 714 | if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { 715 | this._fire("keyup", combo, e); 716 | if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { 717 | combo.count += 1; 718 | } 719 | } 720 | } 721 | if (!keys_remaining) { 722 | this._fire("release", combo, e); 723 | this._remove_from_active_combos(combo); 724 | } 725 | }; 726 | 727 | Listener.prototype.simple_combo = function(keys, callback) { 728 | return this.register_combo({ 729 | keys: keys, 730 | on_keydown: callback 731 | }); 732 | }; 733 | 734 | Listener.prototype.counting_combo = function(keys, count_callback) { 735 | return this.register_combo({ 736 | keys: keys, 737 | is_counting: true, 738 | is_unordered: false, 739 | on_keydown: count_callback 740 | }); 741 | }; 742 | 743 | Listener.prototype.sequence_combo = function(keys, callback) { 744 | return this.register_combo({ 745 | keys: keys, 746 | on_keydown: callback, 747 | is_sequence: true, 748 | is_exclusive: true 749 | }); 750 | }; 751 | 752 | Listener.prototype.register_combo = function(combo_dictionary) { 753 | var combo, property, value, _ref; 754 | if (typeof combo_dictionary["keys"] === "string") { 755 | combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); 756 | } 757 | _ref = this._defaults; 758 | for (property in _ref) { 759 | if (!__hasProp.call(_ref, property)) continue; 760 | value = _ref[property]; 761 | if (combo_dictionary[property] === void 0) { 762 | combo_dictionary[property] = value; 763 | } 764 | } 765 | combo = new Combo(combo_dictionary); 766 | if (_validate_combo(combo)) { 767 | this._registered_combos.push(combo); 768 | return combo; 769 | } 770 | }; 771 | 772 | Listener.prototype.register_many = function(combo_array) { 773 | var combo, _i, _len, _results; 774 | _results = []; 775 | for (_i = 0, _len = combo_array.length; _i < _len; _i++) { 776 | combo = combo_array[_i]; 777 | _results.push(this.register_combo(combo)); 778 | } 779 | return _results; 780 | }; 781 | 782 | Listener.prototype.unregister_combo = function(keys_or_combo) { 783 | var combo, unregister_combo, _i, _len, _ref, _results; 784 | if (!keys_or_combo) { 785 | return false; 786 | } 787 | unregister_combo = (function(_this) { 788 | return function(combo) { 789 | var i, _i, _ref, _results; 790 | _results = []; 791 | for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 792 | if (combo === _this._registered_combos[i]) { 793 | _this._registered_combos.splice(i, 1); 794 | break; 795 | } else { 796 | _results.push(void 0); 797 | } 798 | } 799 | return _results; 800 | }; 801 | })(this); 802 | if (keys_or_combo instanceof Combo) { 803 | return unregister_combo(keys_or_combo); 804 | } else { 805 | if (typeof keys_or_combo === "string") { 806 | keys_or_combo = keys_or_combo.split(" "); 807 | } 808 | _ref = this._registered_combos; 809 | _results = []; 810 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { 811 | combo = _ref[_i]; 812 | if (combo == null) { 813 | continue; 814 | } 815 | if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { 816 | _results.push(unregister_combo(combo)); 817 | } else { 818 | _results.push(void 0); 819 | } 820 | } 821 | return _results; 822 | } 823 | }; 824 | 825 | Listener.prototype.unregister_many = function(combo_array) { 826 | var combo, _i, _len, _results; 827 | _results = []; 828 | for (_i = 0, _len = combo_array.length; _i < _len; _i++) { 829 | combo = combo_array[_i]; 830 | _results.push(this.unregister_combo(combo)); 831 | } 832 | return _results; 833 | }; 834 | 835 | Listener.prototype.get_registered_combos = function() { 836 | return this._registered_combos; 837 | }; 838 | 839 | Listener.prototype.reset = function() { 840 | return this._registered_combos = []; 841 | }; 842 | 843 | Listener.prototype.listen = function() { 844 | return this._prevent_capture = false; 845 | }; 846 | 847 | Listener.prototype.stop_listening = function() { 848 | return this._prevent_capture = true; 849 | }; 850 | 851 | Listener.prototype.get_meta_key = function() { 852 | return _metakey; 853 | }; 854 | 855 | return Listener; 856 | 857 | })(); 858 | 859 | _decide_meta_key = function() { 860 | if (navigator.userAgent.indexOf("Mac OS X") !== -1) { 861 | _metakey = "cmd"; 862 | } 863 | }; 864 | 865 | _change_keycodes_by_browser = function() { 866 | if (navigator.userAgent.indexOf("Opera") !== -1) { 867 | _keycode_dictionary["17"] = "cmd"; 868 | } 869 | }; 870 | 871 | _convert_key_to_readable = function(k) { 872 | return _keycode_dictionary[k]; 873 | }; 874 | 875 | _filter_array = function(array, callback) { 876 | var element; 877 | if (array.filter) { 878 | return array.filter(callback); 879 | } else { 880 | return (function() { 881 | var _i, _len, _results; 882 | _results = []; 883 | for (_i = 0, _len = array.length; _i < _len; _i++) { 884 | element = array[_i]; 885 | if (callback(element)) { 886 | _results.push(element); 887 | } 888 | } 889 | return _results; 890 | })(); 891 | } 892 | }; 893 | 894 | _compare_arrays = function(a1, a2) { 895 | var item, _i, _len; 896 | if (a1.length !== a2.length) { 897 | return false; 898 | } 899 | for (_i = 0, _len = a1.length; _i < _len; _i++) { 900 | item = a1[_i]; 901 | if (__indexOf.call(a2, item) >= 0) { 902 | continue; 903 | } 904 | return false; 905 | } 906 | return true; 907 | }; 908 | 909 | _compare_arrays_sorted = function(a1, a2) { 910 | var i, _i, _ref; 911 | if (a1.length !== a2.length) { 912 | return false; 913 | } 914 | for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 915 | if (a1[i] !== a2[i]) { 916 | return false; 917 | } 918 | } 919 | return true; 920 | }; 921 | 922 | _is_array_in_array = function(a1, a2) { 923 | var item, _i, _len; 924 | for (_i = 0, _len = a1.length; _i < _len; _i++) { 925 | item = a1[_i]; 926 | if (__indexOf.call(a2, item) < 0) { 927 | return false; 928 | } 929 | } 930 | return true; 931 | }; 932 | 933 | _index_of_in_array = Array.prototype.indexOf || function(a, item) { 934 | var i, _i, _ref; 935 | for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { 936 | if (a[i] === item) { 937 | return i; 938 | } 939 | } 940 | return -1; 941 | }; 942 | 943 | _is_array_in_array_sorted = function(a1, a2) { 944 | var index, item, prev, _i, _len; 945 | prev = 0; 946 | for (_i = 0, _len = a1.length; _i < _len; _i++) { 947 | item = a1[_i]; 948 | index = _index_of_in_array.call(a2, item); 949 | if (index >= prev) { 950 | prev = index; 951 | } else { 952 | return false; 953 | } 954 | } 955 | return true; 956 | }; 957 | 958 | _log_error = function() { 959 | if (keypress.debug) { 960 | return console.log.apply(console, arguments); 961 | } 962 | }; 963 | 964 | _key_is_valid = function(key) { 965 | var valid, valid_key, _; 966 | valid = false; 967 | for (_ in _keycode_dictionary) { 968 | valid_key = _keycode_dictionary[_]; 969 | if (key === valid_key) { 970 | valid = true; 971 | break; 972 | } 973 | } 974 | if (!valid) { 975 | for (_ in _keycode_shifted_keys) { 976 | valid_key = _keycode_shifted_keys[_]; 977 | if (key === valid_key) { 978 | valid = true; 979 | break; 980 | } 981 | } 982 | } 983 | return valid; 984 | }; 985 | 986 | _validate_combo = function(combo) { 987 | var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; 988 | validated = true; 989 | if (!combo.keys.length) { 990 | _log_error("You're trying to bind a combo with no keys:", combo); 991 | } 992 | for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { 993 | key = combo.keys[i]; 994 | alt_name = _keycode_alternate_names[key]; 995 | if (alt_name) { 996 | key = combo.keys[i] = alt_name; 997 | } 998 | if (key === "meta") { 999 | combo.keys.splice(i, 1, _metakey); 1000 | } 1001 | if (key === "cmd") { 1002 | _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); 1003 | } 1004 | } 1005 | _ref1 = combo.keys; 1006 | for (_j = 0, _len = _ref1.length; _j < _len; _j++) { 1007 | key = _ref1[_j]; 1008 | if (!_key_is_valid(key)) { 1009 | _log_error("Do not recognize the key \"" + key + "\""); 1010 | validated = false; 1011 | } 1012 | } 1013 | if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { 1014 | non_modifier_keys = combo.keys.slice(); 1015 | for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { 1016 | mod_key = _modifier_keys[_k]; 1017 | if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { 1018 | non_modifier_keys.splice(i, 1); 1019 | } 1020 | } 1021 | if (non_modifier_keys.length > 1) { 1022 | _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); 1023 | validated = false; 1024 | } 1025 | } 1026 | for (property in combo) { 1027 | value = combo[property]; 1028 | if (_factory_defaults[property] === "undefined") { 1029 | _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); 1030 | } 1031 | } 1032 | return validated; 1033 | }; 1034 | 1035 | _convert_to_shifted_key = function(key, e) { 1036 | var k; 1037 | if (!e.shiftKey) { 1038 | return false; 1039 | } 1040 | k = _keycode_shifted_keys[key]; 1041 | if (k != null) { 1042 | return k; 1043 | } 1044 | return false; 1045 | }; 1046 | 1047 | _modifier_event_mapping = { 1048 | "cmd": "metaKey", 1049 | "ctrl": "ctrlKey", 1050 | "shift": "shiftKey", 1051 | "alt": "altKey" 1052 | }; 1053 | 1054 | _keycode_alternate_names = { 1055 | "escape": "esc", 1056 | "control": "ctrl", 1057 | "command": "cmd", 1058 | "break": "pause", 1059 | "windows": "cmd", 1060 | "option": "alt", 1061 | "caps_lock": "caps", 1062 | "apostrophe": "\'", 1063 | "semicolon": ";", 1064 | "tilde": "~", 1065 | "accent": "`", 1066 | "scroll_lock": "scroll", 1067 | "num_lock": "num" 1068 | }; 1069 | 1070 | _keycode_shifted_keys = { 1071 | "/": "?", 1072 | ".": ">", 1073 | ",": "<", 1074 | "\'": "\"", 1075 | ";": ":", 1076 | "[": "{", 1077 | "]": "}", 1078 | "\\": "|", 1079 | "`": "~", 1080 | "=": "+", 1081 | "-": "_", 1082 | "1": "!", 1083 | "2": "@", 1084 | "3": "#", 1085 | "4": "$", 1086 | "5": "%", 1087 | "6": "^", 1088 | "7": "&", 1089 | "8": "*", 1090 | "9": "(", 1091 | "0": ")" 1092 | }; 1093 | 1094 | _keycode_dictionary = { 1095 | 0: "\\", 1096 | 8: "backspace", 1097 | 9: "tab", 1098 | 12: "num", 1099 | 13: "enter", 1100 | 16: "shift", 1101 | 17: "ctrl", 1102 | 18: "alt", 1103 | 19: "pause", 1104 | 20: "caps", 1105 | 27: "esc", 1106 | 32: "space", 1107 | 33: "pageup", 1108 | 34: "pagedown", 1109 | 35: "end", 1110 | 36: "home", 1111 | 37: "left", 1112 | 38: "up", 1113 | 39: "right", 1114 | 40: "down", 1115 | 44: "print", 1116 | 45: "insert", 1117 | 46: "delete", 1118 | 48: "0", 1119 | 49: "1", 1120 | 50: "2", 1121 | 51: "3", 1122 | 52: "4", 1123 | 53: "5", 1124 | 54: "6", 1125 | 55: "7", 1126 | 56: "8", 1127 | 57: "9", 1128 | 65: "a", 1129 | 66: "b", 1130 | 67: "c", 1131 | 68: "d", 1132 | 69: "e", 1133 | 70: "f", 1134 | 71: "g", 1135 | 72: "h", 1136 | 73: "i", 1137 | 74: "j", 1138 | 75: "k", 1139 | 76: "l", 1140 | 77: "m", 1141 | 78: "n", 1142 | 79: "o", 1143 | 80: "p", 1144 | 81: "q", 1145 | 82: "r", 1146 | 83: "s", 1147 | 84: "t", 1148 | 85: "u", 1149 | 86: "v", 1150 | 87: "w", 1151 | 88: "x", 1152 | 89: "y", 1153 | 90: "z", 1154 | 91: "cmd", 1155 | 92: "cmd", 1156 | 93: "cmd", 1157 | 96: "num_0", 1158 | 97: "num_1", 1159 | 98: "num_2", 1160 | 99: "num_3", 1161 | 100: "num_4", 1162 | 101: "num_5", 1163 | 102: "num_6", 1164 | 103: "num_7", 1165 | 104: "num_8", 1166 | 105: "num_9", 1167 | 106: "num_multiply", 1168 | 107: "num_add", 1169 | 108: "num_enter", 1170 | 109: "num_subtract", 1171 | 110: "num_decimal", 1172 | 111: "num_divide", 1173 | 112: "f1", 1174 | 113: "f2", 1175 | 114: "f3", 1176 | 115: "f4", 1177 | 116: "f5", 1178 | 117: "f6", 1179 | 118: "f7", 1180 | 119: "f8", 1181 | 120: "f9", 1182 | 121: "f10", 1183 | 122: "f11", 1184 | 123: "f12", 1185 | 124: "print", 1186 | 144: "num", 1187 | 145: "scroll", 1188 | 186: ";", 1189 | 187: "=", 1190 | 188: ",", 1191 | 189: "-", 1192 | 190: ".", 1193 | 191: "/", 1194 | 192: "`", 1195 | 219: "[", 1196 | 220: "\\", 1197 | 221: "]", 1198 | 222: "\'", 1199 | 223: "`", 1200 | 224: "cmd", 1201 | 225: "alt", 1202 | 57392: "ctrl", 1203 | 63289: "num", 1204 | 59: ";", 1205 | 61: "=", 1206 | 173: "-" 1207 | }; 1208 | 1209 | keypress._keycode_dictionary = _keycode_dictionary; 1210 | 1211 | keypress._is_array_in_array_sorted = _is_array_in_array_sorted; 1212 | 1213 | _decide_meta_key(); 1214 | 1215 | _change_keycodes_by_browser(); 1216 | 1217 | if (true) { 1218 | !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function() { 1219 | return keypress; 1220 | }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); 1221 | } else if (typeof exports !== "undefined" && exports !== null) { 1222 | exports.keypress = keypress; 1223 | } else { 1224 | window.keypress = keypress; 1225 | } 1226 | 1227 | }).call(this); 1228 | 1229 | 1230 | /***/ } 1231 | /******/ ]) 1232 | }); 1233 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | reactKeypress 6 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-keypress", 3 | "version": "0.1.5", 4 | "description": "", 5 | "main": "./dist/reactKeypress.js", 6 | "scripts": { 7 | "start": "npm run serve | npm run dev", 8 | "serve": "./node_modules/.bin/http-server -p 8080", 9 | "dev": "webpack-dev-server --progress --colors --port 8090", 10 | "watch": "webpack --progress --colors --watch", 11 | "build": "node_modules/webpack/bin/webpack.js", 12 | "amend": "git add . && git commit --amend --reuse-message=HEAD", 13 | "push": "git push --tags && git push", 14 | "release": "bower version patch -f && npm version patch --force --no-git-tag-version && npm run build && npm run amend && npm run push && npm publish" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/zzarcon/react-keypress.git" 19 | }, 20 | "keywords": [ 21 | "react-keypress" 22 | ], 23 | "author": "zzarcon", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/zzarcon/react-keypress/issues" 27 | }, 28 | "homepage": "https://github.com/zzarcon/react-keypress", 29 | "dependencies": {}, 30 | "devDependencies": { 31 | "babel-core": "^5.4.3", 32 | "babel-loader": "^5.1.2", 33 | "http-server": "~0.7.1", 34 | "webpack": "~1.4.4", 35 | "webpack-dev-server": "~1.6.5" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /react-keypress.js: -------------------------------------------------------------------------------- 1 | var Keypress = require("./bower_components/Keypress/keypress.js"); 2 | var elements = new WeakMap(); 3 | 4 | module.exports = function(keys, handler) { 5 | if (!keys || !handler) return; 6 | 7 | return function(e) { 8 | var el = e.target; 9 | 10 | //Check for no register the same element multiple times 11 | if (elements.has(el)) return; 12 | 13 | var listener = new Keypress.Listener(el); 14 | 15 | elements.set(el, true); 16 | listener.register_combo({ 17 | "keys": keys, 18 | "on_keyup": handler 19 | }); 20 | }; 21 | }; 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var el1; 2 | var el2; 3 | 4 | window.onload = function() { 5 | // debugger; 6 | }; -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | entry: './react-keypress.js', 3 | output: { 4 | publicPath: 'http://localhost:8090/assets', 5 | path: './dist', 6 | filename: 'reactKeypress.js', 7 | libraryTarget: 'umd', 8 | library: 'ReactKeypress' 9 | }, 10 | module: { 11 | loaders: [{ 12 | test: /\.js$/, 13 | exclude: /(node_modules|bower_components)/, 14 | loader: 'babel' 15 | }] 16 | }, 17 | resolve: { 18 | extensions: ['', '.js'] 19 | } 20 | }; --------------------------------------------------------------------------------