keysim
Advanced tools
Comparing version 1.3.0 to 1.4.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
factory((global.Keysim = {})) | ||
(factory((global.Keysim = global.Keysim || {}))); | ||
}(this, function (exports) { 'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
function isEditable (element) { | ||
if (element.ownerDocument.designMode && element.ownerDocument.designMode.toLowerCase() === 'on') { | ||
return true; | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
switch (element.tagName.toLowerCase()) { | ||
case 'input': | ||
return isEditableInput(element); | ||
case 'textarea': | ||
return true; | ||
} | ||
/* jshint esnext:true, undef:true, unused:true */ | ||
if (isContentEditable(element)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function isContentEditable(element) { | ||
if (element.contentEditable && element.contentEditable.toLowerCase() === 'true') { | ||
return true; | ||
} | ||
if (element.contentEditable && element.contentEditable.toLowerCase() === 'inherit' && element.parentNode) { | ||
return isContentEditable(element.parentNode); | ||
} | ||
return false; | ||
} | ||
function isEditableInput(input) { | ||
switch (input.type) { | ||
case 'text': | ||
return true; | ||
case 'email': | ||
return true; | ||
case 'password': | ||
return true; | ||
case 'search': | ||
return true; | ||
case 'tel': | ||
return true; | ||
case 'url': | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var CTRL = 1 << 0; | ||
@@ -32,71 +98,55 @@ var META = 1 << 1; | ||
*/ | ||
var Keystroke = | ||
/** | ||
* @param {number} modifiers A bitmask formed by CTRL, META, ALT, and SHIFT. | ||
* @param {number} keyCode | ||
*/ | ||
function Keystroke(modifiers, keyCode) { | ||
classCallCheck(this, Keystroke); | ||
var Keystroke = (function () { | ||
/** | ||
* @param {number} modifiers A bitmask formed by CTRL, META, ALT, and SHIFT. | ||
* @param {number} keyCode | ||
*/ | ||
this.modifiers = modifiers; | ||
this.ctrlKey = !!(modifiers & CTRL); | ||
this.metaKey = !!(modifiers & META); | ||
this.altKey = !!(modifiers & ALT); | ||
this.shiftKey = !!(modifiers & SHIFT); | ||
this.keyCode = keyCode; | ||
} | ||
function Keystroke(modifiers, keyCode) { | ||
_classCallCheck(this, Keystroke); | ||
/** | ||
* Gets the bitmask value for the "control" modifier. | ||
* | ||
* @type {number} | ||
*/ | ||
this.modifiers = modifiers; | ||
this.ctrlKey = !!(modifiers & CTRL); | ||
this.metaKey = !!(modifiers & META); | ||
this.altKey = !!(modifiers & ALT); | ||
this.shiftKey = !!(modifiers & SHIFT); | ||
this.keyCode = keyCode; | ||
} | ||
/** | ||
* Simulates a keyboard with a particular key-to-character and key-to-action | ||
* mapping. Use `US_ENGLISH` to get a pre-configured keyboard. | ||
*/ | ||
/** | ||
* Gets the bitmask value for the "meta" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
/** | ||
* Gets the bitmask value for the "control" modifier. | ||
* | ||
* @type {number} | ||
*/ | ||
_createClass(Keystroke, null, [{ | ||
key: 'CTRL', | ||
value: CTRL, | ||
/** | ||
* Gets the bitmask value for the "alt" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
/** | ||
* Gets the bitmask value for the "meta" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
enumerable: true | ||
}, { | ||
key: 'META', | ||
value: META, | ||
/** | ||
* Gets the bitmask value for the "alt" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
enumerable: true | ||
}, { | ||
key: 'ALT', | ||
value: ALT, | ||
/** | ||
* Gets the bitmask value for the "shift" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
; | ||
/** | ||
* Gets the bitmask value for the "shift" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
enumerable: true | ||
}, { | ||
key: 'SHIFT', | ||
value: SHIFT, | ||
enumerable: true | ||
}]); | ||
return Keystroke; | ||
})(); | ||
var Keyboard = (function () { | ||
/** | ||
* Simulates a keyboard with a particular key-to-character and key-to-action | ||
* mapping. Use `US_ENGLISH` to get a pre-configured keyboard. | ||
*/ | ||
Keystroke.CTRL = CTRL; | ||
Keystroke.META = META; | ||
Keystroke.ALT = ALT; | ||
Keystroke.SHIFT = SHIFT; | ||
var Keyboard = function () { | ||
/** | ||
@@ -108,3 +158,3 @@ * @param {Object.<number, Keystroke>} charCodeKeyCodeMap | ||
function Keyboard(charCodeKeyCodeMap, actionKeyCodeMap) { | ||
_classCallCheck(this, Keyboard); | ||
classCallCheck(this, Keyboard); | ||
@@ -122,318 +172,327 @@ this._charCodeKeyCodeMap = charCodeKeyCodeMap; | ||
Keyboard.prototype.charCodeForKeystroke = function charCodeForKeystroke(keystroke) { | ||
var map = this._charCodeKeyCodeMap; | ||
for (var charCode in map) { | ||
if (Object.prototype.hasOwnProperty.call(map, charCode)) { | ||
var keystrokeForCharCode = map[charCode]; | ||
if (keystroke.keyCode === keystrokeForCharCode.keyCode && keystroke.modifiers === keystrokeForCharCode.modifiers) { | ||
return parseInt(charCode, 10); | ||
createClass(Keyboard, [{ | ||
key: 'charCodeForKeystroke', | ||
value: function charCodeForKeystroke(keystroke) { | ||
var map = this._charCodeKeyCodeMap; | ||
for (var charCode in map) { | ||
if (Object.prototype.hasOwnProperty.call(map, charCode)) { | ||
var keystrokeForCharCode = map[charCode]; | ||
if (keystroke.keyCode === keystrokeForCharCode.keyCode && keystroke.modifiers === keystrokeForCharCode.modifiers) { | ||
return parseInt(charCode, 10); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Creates an event ready for dispatching onto the given target. | ||
* | ||
* @param {string} type One of "keydown", "keypress", "keyup", or "textInput". | ||
* @param {Keystroke} keystroke | ||
* @param {HTMLElement} target | ||
* @return {Event} | ||
*/ | ||
/** | ||
* Creates an event ready for dispatching onto the given target. | ||
* | ||
* @param {string} type One of "keydown", "keypress", "keyup", or "textInput". | ||
* @param {Keystroke} keystroke | ||
* @param {HTMLElement} target | ||
* @return {Event} | ||
*/ | ||
Keyboard.prototype.createEventFromKeystroke = function createEventFromKeystroke(type, keystroke, target) { | ||
var document = target.ownerDocument; | ||
var window = document.defaultView; | ||
var Event = window.Event; | ||
}, { | ||
key: 'createEventFromKeystroke', | ||
value: function createEventFromKeystroke(type, keystroke, target) { | ||
var document = target.ownerDocument; | ||
var window = document.defaultView; | ||
var Event = window.Event; | ||
var event = undefined; | ||
var event = void 0; | ||
try { | ||
event = new Event(type); | ||
} catch (e) { | ||
event = document.createEvent('UIEvents'); | ||
} | ||
try { | ||
event = new Event(type); | ||
} catch (e) { | ||
event = document.createEvent('UIEvents'); | ||
} | ||
event.initEvent(type, true, true); | ||
event.initEvent(type, true, true); | ||
switch (type) { | ||
case 'textInput': | ||
event.data = String.fromCharCode(this.charCodeForKeystroke(keystroke)); | ||
break; | ||
switch (type) { | ||
case 'textInput': | ||
event.data = String.fromCharCode(this.charCodeForKeystroke(keystroke)); | ||
break; | ||
case 'keydown':case 'keypress':case 'keyup': | ||
event.shiftKey = keystroke.shiftKey; | ||
event.altKey = keystroke.altKey; | ||
event.metaKey = keystroke.metaKey; | ||
event.ctrlKey = keystroke.ctrlKey; | ||
event.keyCode = type === 'keypress' ? this.charCodeForKeystroke(keystroke) : keystroke.keyCode; | ||
event.charCode = type === 'keypress' ? event.keyCode : 0; | ||
event.which = event.keyCode; | ||
break; | ||
case 'keydown':case 'keypress':case 'keyup': | ||
event.shiftKey = keystroke.shiftKey; | ||
event.altKey = keystroke.altKey; | ||
event.metaKey = keystroke.metaKey; | ||
event.ctrlKey = keystroke.ctrlKey; | ||
event.keyCode = type === 'keypress' ? this.charCodeForKeystroke(keystroke) : keystroke.keyCode; | ||
event.charCode = type === 'keypress' ? event.keyCode : 0; | ||
event.which = event.keyCode; | ||
break; | ||
} | ||
return event; | ||
} | ||
return event; | ||
}; | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* action was undertaken by a human. | ||
* | ||
* @param {string} action e.g. "alt+shift+left" or "backspace" | ||
* @param {HTMLElement} target | ||
*/ | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* action was undertaken by a human. | ||
* | ||
* @param {string} action e.g. "alt+shift+left" or "backspace" | ||
* @param {HTMLElement} target | ||
*/ | ||
}, { | ||
key: 'dispatchEventsForAction', | ||
value: function dispatchEventsForAction(action, target) { | ||
var keystroke = this.keystrokeForAction(action); | ||
this.dispatchEventsForKeystroke(keystroke, target); | ||
} | ||
Keyboard.prototype.dispatchEventsForAction = function dispatchEventsForAction(action, target) { | ||
var keystroke = this.keystrokeForAction(action); | ||
this.dispatchEventsForKeystroke(keystroke, target); | ||
}; | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* input had been typed by a human. | ||
* | ||
* @param {string} input | ||
* @param {HTMLElement} target | ||
*/ | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* input had been typed by a human. | ||
* | ||
* @param {string} input | ||
* @param {HTMLElement} target | ||
*/ | ||
Keyboard.prototype.dispatchEventsForInput = function dispatchEventsForInput(input, target) { | ||
var currentModifierState = 0; | ||
for (var i = 0, _length = input.length; i < _length; i++) { | ||
var keystroke = this.keystrokeForCharCode(input.charCodeAt(i)); | ||
this.dispatchModifierStateTransition(target, currentModifierState, keystroke.modifiers); | ||
this.dispatchEventsForKeystroke(keystroke, target, false); | ||
currentModifierState = keystroke.modifiers; | ||
}, { | ||
key: 'dispatchEventsForInput', | ||
value: function dispatchEventsForInput(input, target) { | ||
var currentModifierState = 0; | ||
for (var i = 0, length = input.length; i < length; i++) { | ||
var keystroke = this.keystrokeForCharCode(input.charCodeAt(i)); | ||
this.dispatchModifierStateTransition(target, currentModifierState, keystroke.modifiers); | ||
this.dispatchEventsForKeystroke(keystroke, target, false); | ||
currentModifierState = keystroke.modifiers; | ||
} | ||
this.dispatchModifierStateTransition(target, currentModifierState, 0); | ||
} | ||
this.dispatchModifierStateTransition(target, currentModifierState, 0); | ||
}; | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* keystroke was performed by a human. When simulating, for example, typing | ||
* the letter "A" (assuming a U.S. English keyboard) then the sequence will | ||
* look like this: | ||
* | ||
* keydown keyCode=16 (SHIFT) charCode=0 shiftKey=true | ||
* keydown keyCode=65 (A) charCode=0 shiftKey=true | ||
* keypress keyCode=65 (A) charCode=65 (A) shiftKey=true | ||
* textInput data=A | ||
* keyup keyCode=65 (A) charCode=0 shiftKey=true | ||
* keyup keyCode=16 (SHIFT) charCode=0 shiftKey=false | ||
* | ||
* If the keystroke would not cause a character to be input, such as when | ||
* pressing alt+shift+left, the sequence looks like this: | ||
* | ||
* keydown keyCode=16 (SHIFT) charCode=0 altKey=false shiftKey=true | ||
* keydown keyCode=18 (ALT) charCode=0 altKey=true shiftKey=true | ||
* keydown keyCode=37 (LEFT) charCode=0 altKey=true shiftKey=true | ||
* keyup keyCode=37 (LEFT) charCode=0 altKey=true shiftKey=true | ||
* keyup keyCode=18 (ALT) charCode=0 altKey=false shiftKey=true | ||
* keyup keyCode=16 (SHIFT) charCode=0 altKey=false shiftKey=false | ||
* | ||
* To disable handling of modifier keys, call with `transitionModifers` set | ||
* to false. Doing so will omit the keydown and keyup events associated with | ||
* shift, ctrl, alt, and meta keys surrounding the actual keystroke. | ||
* | ||
* @param {Keystroke} keystroke | ||
* @param {HTMLElement} target | ||
* @param {boolean=} transitionModifiers | ||
* @param {number} events | ||
*/ | ||
/** | ||
* Fires the correct sequence of events on the given target as if the given | ||
* keystroke was performed by a human. When simulating, for example, typing | ||
* the letter "A" (assuming a U.S. English keyboard) then the sequence will | ||
* look like this: | ||
* | ||
* keydown keyCode=16 (SHIFT) charCode=0 shiftKey=true | ||
* keydown keyCode=65 (A) charCode=0 shiftKey=true | ||
* keypress keyCode=65 (A) charCode=65 (A) shiftKey=true | ||
* textInput data=A | ||
* keyup keyCode=65 (A) charCode=0 shiftKey=true | ||
* keyup keyCode=16 (SHIFT) charCode=0 shiftKey=false | ||
* | ||
* If the keystroke would not cause a character to be input, such as when | ||
* pressing alt+shift+left, the sequence looks like this: | ||
* | ||
* keydown keyCode=16 (SHIFT) charCode=0 altKey=false shiftKey=true | ||
* keydown keyCode=18 (ALT) charCode=0 altKey=true shiftKey=true | ||
* keydown keyCode=37 (LEFT) charCode=0 altKey=true shiftKey=true | ||
* keyup keyCode=37 (LEFT) charCode=0 altKey=true shiftKey=true | ||
* keyup keyCode=18 (ALT) charCode=0 altKey=false shiftKey=true | ||
* keyup keyCode=16 (SHIFT) charCode=0 altKey=false shiftKey=false | ||
* | ||
* To disable handling of modifier keys, call with `transitionModifers` set | ||
* to false. Doing so will omit the keydown and keyup events associated with | ||
* shift, ctrl, alt, and meta keys surrounding the actual keystroke. | ||
* | ||
* @param {Keystroke} keystroke | ||
* @param {HTMLElement} target | ||
* @param {boolean=} transitionModifiers | ||
* @param {number} events | ||
*/ | ||
Keyboard.prototype.dispatchEventsForKeystroke = function dispatchEventsForKeystroke(keystroke, target) { | ||
var transitionModifiers = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; | ||
var events = arguments.length <= 3 || arguments[3] === undefined ? KeyEvents.ALL : arguments[3]; | ||
}, { | ||
key: 'dispatchEventsForKeystroke', | ||
value: function dispatchEventsForKeystroke(keystroke, target) { | ||
var transitionModifiers = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; | ||
var events = arguments.length <= 3 || arguments[3] === undefined ? KeyEvents.ALL : arguments[3]; | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, 0, keystroke.modifiers, events); | ||
} | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, 0, keystroke.modifiers, events); | ||
} | ||
var keydownEvent = undefined; | ||
if (events & KeyEvents.DOWN) { | ||
keydownEvent = this.createEventFromKeystroke('keydown', keystroke, target); | ||
} | ||
var keydownEvent = void 0; | ||
if (events & KeyEvents.DOWN) { | ||
keydownEvent = this.createEventFromKeystroke('keydown', keystroke, target); | ||
} | ||
if (keydownEvent && target.dispatchEvent(keydownEvent) && this.targetCanReceiveTextInput(target)) { | ||
var keypressEvent = undefined; | ||
if (events & KeyEvents.PRESS) { | ||
keypressEvent = this.createEventFromKeystroke('keypress', keystroke, target); | ||
} | ||
if (keypressEvent && keypressEvent.charCode && target.dispatchEvent(keypressEvent)) { | ||
if (events & KeyEvents.INPUT) { | ||
var textinputEvent = this.createEventFromKeystroke('textInput', keystroke, target); | ||
target.dispatchEvent(textinputEvent); | ||
if (keydownEvent && target.dispatchEvent(keydownEvent) && this.targetCanReceiveTextInput(target)) { | ||
var keypressEvent = void 0; | ||
if (events & KeyEvents.PRESS) { | ||
keypressEvent = this.createEventFromKeystroke('keypress', keystroke, target); | ||
} | ||
if (keypressEvent && keypressEvent.charCode && target.dispatchEvent(keypressEvent)) { | ||
if (events & KeyEvents.INPUT) { | ||
var textinputEvent = this.createEventFromKeystroke('textInput', keystroke, target); | ||
target.dispatchEvent(textinputEvent); | ||
} | ||
} | ||
} | ||
} | ||
if (events & KeyEvents.UP) { | ||
var keyupEvent = this.createEventFromKeystroke('keyup', keystroke, target); | ||
target.dispatchEvent(keyupEvent); | ||
} | ||
if (events & KeyEvents.UP) { | ||
var keyupEvent = this.createEventFromKeystroke('keyup', keystroke, target); | ||
target.dispatchEvent(keyupEvent); | ||
} | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, keystroke.modifiers, 0); | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, keystroke.modifiers, 0); | ||
} | ||
} | ||
}; | ||
/** | ||
* Transitions from one modifier state to another by dispatching key events. | ||
* | ||
* @param {EventTarget} target | ||
* @param {number} fromModifierState | ||
* @param {number} toModifierState | ||
* @param {number} events | ||
* @private | ||
*/ | ||
/** | ||
* Transitions from one modifier state to another by dispatching key events. | ||
* | ||
* @param {EventTarget} target | ||
* @param {number} fromModifierState | ||
* @param {number} toModifierState | ||
* @param {number} events | ||
* @private | ||
*/ | ||
Keyboard.prototype.dispatchModifierStateTransition = function dispatchModifierStateTransition(target, fromModifierState, toModifierState) { | ||
var events = arguments.length <= 3 || arguments[3] === undefined ? KeyEvents.ALL : arguments[3]; | ||
}, { | ||
key: 'dispatchModifierStateTransition', | ||
value: function dispatchModifierStateTransition(target, fromModifierState, toModifierState) { | ||
var events = arguments.length <= 3 || arguments[3] === undefined ? KeyEvents.ALL : arguments[3]; | ||
var currentModifierState = fromModifierState; | ||
var didHaveMeta = (fromModifierState & META) === META; | ||
var willHaveMeta = (toModifierState & META) === META; | ||
var didHaveCtrl = (fromModifierState & CTRL) === CTRL; | ||
var willHaveCtrl = (toModifierState & CTRL) === CTRL; | ||
var didHaveShift = (fromModifierState & SHIFT) === SHIFT; | ||
var willHaveShift = (toModifierState & SHIFT) === SHIFT; | ||
var didHaveAlt = (fromModifierState & ALT) === ALT; | ||
var willHaveAlt = (toModifierState & ALT) === ALT; | ||
var currentModifierState = fromModifierState; | ||
var didHaveMeta = (fromModifierState & META) === META; | ||
var willHaveMeta = (toModifierState & META) === META; | ||
var didHaveCtrl = (fromModifierState & CTRL) === CTRL; | ||
var willHaveCtrl = (toModifierState & CTRL) === CTRL; | ||
var didHaveShift = (fromModifierState & SHIFT) === SHIFT; | ||
var willHaveShift = (toModifierState & SHIFT) === SHIFT; | ||
var didHaveAlt = (fromModifierState & ALT) === ALT; | ||
var willHaveAlt = (toModifierState & ALT) === ALT; | ||
var includeKeyUp = events & KeyEvents.UP; | ||
var includeKeyPress = events & KeyEvents.PRESS; | ||
var includeKeyDown = events & KeyEvents.DOWN; | ||
var includeKeyUp = events & KeyEvents.UP; | ||
var includeKeyPress = events & KeyEvents.PRESS; | ||
var includeKeyDown = events & KeyEvents.DOWN; | ||
if (includeKeyUp && didHaveMeta === true && willHaveMeta === false) { | ||
// Release the meta key. | ||
currentModifierState &= ~META; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
if (includeKeyUp && didHaveMeta === true && willHaveMeta === false) { | ||
// Release the meta key. | ||
currentModifierState &= ~META; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
if (includeKeyUp && didHaveCtrl === true && willHaveCtrl === false) { | ||
// Release the ctrl key. | ||
currentModifierState &= ~CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (includeKeyUp && didHaveCtrl === true && willHaveCtrl === false) { | ||
// Release the ctrl key. | ||
currentModifierState &= ~CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (includeKeyUp && didHaveShift === true && willHaveShift === false) { | ||
// Release the shift key. | ||
currentModifierState &= ~SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
if (includeKeyUp && didHaveShift === true && willHaveShift === false) { | ||
// Release the shift key. | ||
currentModifierState &= ~SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
if (includeKeyUp && didHaveAlt === true && willHaveAlt === false) { | ||
// Release the alt key. | ||
currentModifierState &= ~ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (includeKeyUp && didHaveAlt === true && willHaveAlt === false) { | ||
// Release the alt key. | ||
currentModifierState &= ~ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keyup', new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (includeKeyDown && didHaveMeta === false && willHaveMeta === true) { | ||
// Press the meta key. | ||
currentModifierState |= META; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
if (includeKeyDown && didHaveMeta === false && willHaveMeta === true) { | ||
// Press the meta key. | ||
currentModifierState |= META; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
if (includeKeyDown && didHaveCtrl === false && willHaveCtrl === true) { | ||
// Press the ctrl key. | ||
currentModifierState |= CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (includeKeyDown && didHaveCtrl === false && willHaveCtrl === true) { | ||
// Press the ctrl key. | ||
currentModifierState |= CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (includeKeyDown && didHaveShift === false && willHaveShift === true) { | ||
// Press the shift key. | ||
currentModifierState |= SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
if (includeKeyDown && didHaveShift === false && willHaveShift === true) { | ||
// Press the shift key. | ||
currentModifierState |= SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
if (includeKeyDown && didHaveAlt === false && willHaveAlt === true) { | ||
// Press the alt key. | ||
currentModifierState |= ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (includeKeyDown && didHaveAlt === false && willHaveAlt === true) { | ||
// Press the alt key. | ||
currentModifierState |= ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke('keydown', new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (currentModifierState !== toModifierState) { | ||
throw new Error('internal error, expected modifier state: ' + toModifierState + (', got: ' + currentModifierState)); | ||
if (currentModifierState !== toModifierState) { | ||
throw new Error('internal error, expected modifier state: ' + toModifierState + (', got: ' + currentModifierState)); | ||
} | ||
} | ||
}; | ||
/** | ||
* Returns the keystroke associated with the given action. | ||
* | ||
* @param {string} action | ||
* @return {?Keystroke} | ||
*/ | ||
/** | ||
* Returns the keystroke associated with the given action. | ||
* | ||
* @param {string} action | ||
* @return {?Keystroke} | ||
*/ | ||
Keyboard.prototype.keystrokeForAction = function keystrokeForAction(action) { | ||
var keyCode = null; | ||
var modifiers = 0; | ||
}, { | ||
key: 'keystrokeForAction', | ||
value: function keystrokeForAction(action) { | ||
var keyCode = null; | ||
var modifiers = 0; | ||
var parts = action.split('+'); | ||
var lastPart = parts.pop(); | ||
var parts = action.split('+'); | ||
var lastPart = parts.pop(); | ||
parts.forEach(function (part) { | ||
switch (part.toUpperCase()) { | ||
case 'CTRL': | ||
modifiers |= CTRL;break; | ||
case 'META': | ||
modifiers |= META;break; | ||
case 'ALT': | ||
modifiers |= ALT;break; | ||
case 'SHIFT': | ||
modifiers |= SHIFT;break; | ||
default: | ||
throw new Error('in "' + action + '", invalid modifier: ' + part); | ||
break; | ||
parts.forEach(function (part) { | ||
switch (part.toUpperCase()) { | ||
case 'CTRL': | ||
modifiers |= CTRL;break; | ||
case 'META': | ||
modifiers |= META;break; | ||
case 'ALT': | ||
modifiers |= ALT;break; | ||
case 'SHIFT': | ||
modifiers |= SHIFT;break; | ||
default: | ||
throw new Error('in "' + action + '", invalid modifier: ' + part); | ||
break; | ||
} | ||
}); | ||
if (lastPart.toUpperCase() in this._actionKeyCodeMap) { | ||
keyCode = this._actionKeyCodeMap[lastPart.toUpperCase()]; | ||
} else if (lastPart.length === 1) { | ||
var lastPartKeystroke = this.keystrokeForCharCode(lastPart.charCodeAt(0)); | ||
modifiers |= lastPartKeystroke.modifiers; | ||
keyCode = lastPartKeystroke.keyCode; | ||
} else { | ||
throw new Error('in "' + action + '", invalid action: ' + lastPart); | ||
} | ||
}); | ||
if (lastPart.toUpperCase() in this._actionKeyCodeMap) { | ||
keyCode = this._actionKeyCodeMap[lastPart.toUpperCase()]; | ||
} else if (lastPart.length === 1) { | ||
var lastPartKeystroke = this.keystrokeForCharCode(lastPart.charCodeAt(0)); | ||
modifiers |= lastPartKeystroke.modifiers; | ||
keyCode = lastPartKeystroke.keyCode; | ||
} else { | ||
throw new Error('in "' + action + '", invalid action: ' + lastPart); | ||
return new Keystroke(modifiers, keyCode); | ||
} | ||
return new Keystroke(modifiers, keyCode); | ||
}; | ||
/** | ||
* Gets the keystroke used to generate the given character code. | ||
* | ||
* @param {number} charCode | ||
* @return {?Keystroke} | ||
*/ | ||
/** | ||
* Gets the keystroke used to generate the given character code. | ||
* | ||
* @param {number} charCode | ||
* @return {?Keystroke} | ||
*/ | ||
Keyboard.prototype.keystrokeForCharCode = function keystrokeForCharCode(charCode) { | ||
return this._charCodeKeyCodeMap[charCode] || null; | ||
}; | ||
/** | ||
* @param {EventTarget} target | ||
* @private | ||
*/ | ||
Keyboard.prototype.targetCanReceiveTextInput = function targetCanReceiveTextInput(target) { | ||
if (!target) { | ||
return false; | ||
}, { | ||
key: 'keystrokeForCharCode', | ||
value: function keystrokeForCharCode(charCode) { | ||
return this._charCodeKeyCodeMap[charCode] || null; | ||
} | ||
switch (target.nodeName && target.nodeName.toLowerCase()) { | ||
case 'input': | ||
var type = target.type; | ||
return !(type === 'hidden' || type === 'radio' || type === 'checkbox'); | ||
/** | ||
* @param {EventTarget} target | ||
* @private | ||
*/ | ||
case 'textarea': | ||
return true; | ||
}, { | ||
key: 'targetCanReceiveTextInput', | ||
value: function targetCanReceiveTextInput(target) { | ||
if (!target) { | ||
return false; | ||
} | ||
default: | ||
return false; | ||
return isEditable(target); | ||
} | ||
}; | ||
}]); | ||
return Keyboard; | ||
})(); | ||
}(); | ||
@@ -582,2 +641,4 @@ var US_ENGLISH_CHARCODE_KEYCODE_MAP = { | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); |
{ | ||
"name": "keysim", | ||
"version": "1.3.0", | ||
"description": "Keyboard simulator for JavaScript.", | ||
"main": "dist/keysim.js", | ||
"jsnext:main": "lib/keysim.js", | ||
"jsnext:main": "dist/keysim.mjs", | ||
"directories": { | ||
@@ -15,10 +14,12 @@ "test": "test" | ||
"scripts": { | ||
"build": "gobble build dist -f", | ||
"build": "rollup -c", | ||
"pretest": "npm run build", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:browser": "./node_modules/karma/bin/karma start karma.conf.js", | ||
"test:node": "mocha -R spec --recursive test" | ||
"test:node": "mocha", | ||
"prepublish": "npm run build", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
}, | ||
"files": [ | ||
"dist/keysim.js", | ||
"lib/keysim.js" | ||
"dist" | ||
], | ||
@@ -35,18 +36,27 @@ "keywords": [ | ||
"devDependencies": { | ||
"add-event-handler": "^1.0.0", | ||
"babel": "^5.8.21", | ||
"add-event-handler": "^1.0.3", | ||
"babel-plugin-syntax-class-properties": "^6.8.0", | ||
"babel-plugin-transform-class-properties": "^6.10.2", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"babelrc-rollup": "^2.0.0", | ||
"browserify": "^13.0.1", | ||
"detect-node": "^2.0.3", | ||
"es5-shim": "^4.1.10", | ||
"esperanto": "^0.7.4", | ||
"gobble": "^0.10.2", | ||
"gobble-cli": "^0.4.2", | ||
"jsdom": "3.1.2", | ||
"karma": "^0.13.9", | ||
"karma-browserify": "^4.3.0", | ||
"karma-chrome-launcher": "^0.2.0", | ||
"karma-firefox-launcher": "^0.1.6", | ||
"karma-mocha": "^0.2.0", | ||
"karma-sauce-launcher": "^0.2.14", | ||
"mocha": "^2.2.5" | ||
} | ||
"dom-element-is-natively-editable": "^1.0.3", | ||
"es5-shim": "^4.5.9", | ||
"jsdom": "^3.1.2", | ||
"karma": "^1.1.1", | ||
"karma-browserify": "^5.0.5", | ||
"karma-chrome-launcher": "^1.0.1", | ||
"karma-firefox-launcher": "^1.0.0", | ||
"karma-mocha": "^1.1.1", | ||
"karma-sauce-launcher": "^1.0.0", | ||
"mocha": "^2.5.3", | ||
"rollup": "^0.34.1", | ||
"rollup-plugin-babel": "^2.6.1", | ||
"rollup-plugin-node-resolve": "^1.7.1", | ||
"semantic-release": "^4.3.5", | ||
"watchify": "^3.7.0" | ||
}, | ||
"version": "1.4.0" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47255
1119
23
1