MCPcopy Index your code
hub / github.com/CodeboxIDE/codebox / _handleKey

Function _handleKey

client/vendors/mousetrap.js:466–544  ·  view source on GitHub ↗

* handles a character key event * * @param {string} character * @param {Array} modifiers * @param {Event} e * @returns void

(character, modifiers, e)

Source from the content-addressed store, hash-verified

464 * @returns void
465 */
466 function _handleKey(character, modifiers, e) {
467 var callbacks = _getMatches(character, modifiers, e),
468 i,
469 doNotReset = {},
470 maxLevel = 0,
471 processedSequenceCallback = false;
472
473 // Calculate the maxLevel for sequences so we can only execute the longest callback sequence
474 for (i = 0; i < callbacks.length; ++i) {
475 if (callbacks[i].seq) {
476 maxLevel = Math.max(maxLevel, callbacks[i].level);
477 }
478 }
479
480 // loop through matching callbacks for this key event
481 for (i = 0; i < callbacks.length; ++i) {
482
483 // fire for all sequence callbacks
484 // this is because if for example you have multiple sequences
485 // bound such as "g i" and "g t" they both need to fire the
486 // callback for matching g cause otherwise you can only ever
487 // match the first one
488 if (callbacks[i].seq) {
489
490 // only fire callbacks for the maxLevel to prevent
491 // subsequences from also firing
492 //
493 // for example 'a option b' should not cause 'option b' to fire
494 // even though 'option b' is part of the other sequence
495 //
496 // any sequences that do not match here will be discarded
497 // below by the _resetSequences call
498 if (callbacks[i].level != maxLevel) {
499 continue;
500 }
501
502 processedSequenceCallback = true;
503
504 // keep a list of which sequences were matches for later
505 doNotReset[callbacks[i].seq] = 1;
506 _fireCallback(callbacks[i].callback, e, callbacks[i].combo, callbacks[i].seq);
507 continue;
508 }
509
510 // if there were no sequence matches but we are still here
511 // that means this is a regular match so we should fire that
512 if (!processedSequenceCallback) {
513 _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
514 }
515 }
516
517 // if the key you pressed matches the type of sequence without
518 // being a modifier (ie "keyup" or "keypress") then we should
519 // reset all sequences that were not matched by this event
520 //
521 // this is so, for example, if you have the sequence "h a t" and you
522 // type "h e a r t" it does not match. in this case the "e" will
523 // cause the sequence to reset

Callers

nothing calls this directly

Calls 4

_getMatchesFunction · 0.85
_fireCallbackFunction · 0.85
_isModifierFunction · 0.85
_resetSequencesFunction · 0.85

Tested by

no test coverage detected