keysim
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -1,521 +0,521 @@ | ||
(function() { | ||
"use strict"; | ||
(function (global, factory) { | ||
"use strict"; | ||
var index$$$__Object$defineProperty = Object.defineProperty; | ||
var index$$CTRL = 1 << 0; | ||
var index$$META = 1 << 1; | ||
var index$$ALT = 1 << 2; | ||
var index$$SHIFT = 1 << 3; | ||
if (typeof define === "function" && define.amd) { | ||
// export as AMD | ||
define(["exports"], factory); | ||
} else if (typeof module !== "undefined" && module.exports && typeof require === "function") { | ||
// node/browserify | ||
factory(exports); | ||
} else { | ||
// browser global | ||
global.Keysim = {}; | ||
factory(global.Keysim); | ||
} | ||
})(typeof window !== "undefined" ? window : this, function (exports) { | ||
"use strict"; | ||
var index$$Keystroke = function() { | ||
"use strict"; | ||
/* jshint esnext:true, undef:true, unused:true */ | ||
function Keystroke(modifiers, keyCode) { | ||
this.modifiers = modifiers; | ||
this.ctrlKey = !!(modifiers & index$$CTRL); | ||
this.metaKey = !!(modifiers & index$$META); | ||
this.altKey = !!(modifiers & index$$ALT); | ||
this.shiftKey = !!(modifiers & index$$SHIFT); | ||
this.keyCode = keyCode; | ||
} | ||
var CTRL = 1 << 0; | ||
var META = 1 << 1; | ||
var ALT = 1 << 2; | ||
var SHIFT = 1 << 3; | ||
return Keystroke; | ||
}(); | ||
/** | ||
* Represents a keystroke, or a single key code with a set of active modifiers. | ||
* | ||
* @class Keystroke | ||
*/ | ||
var Keystroke = | ||
/** | ||
* @param {number} modifiers A bitmask formed by CTRL, META, ALT, and SHIFT. | ||
* @param {number} keyCode | ||
*/ | ||
function Keystroke(modifiers, keyCode) { | ||
this.modifiers = modifiers; | ||
this.ctrlKey = !!(modifiers & CTRL); | ||
this.metaKey = !!(modifiers & META); | ||
this.altKey = !!(modifiers & ALT); | ||
this.shiftKey = !!(modifiers & SHIFT); | ||
this.keyCode = keyCode; | ||
}; | ||
/** | ||
* Gets the bitmask value for the "control" modifier. | ||
* | ||
* @type {number} | ||
*/ | ||
index$$Keystroke.CTRL = index$$CTRL; | ||
/** | ||
* Gets the bitmask value for the "control" modifier. | ||
* | ||
* @type {number} | ||
*/ | ||
Keystroke.CTRL = CTRL; | ||
/** | ||
* Gets the bitmask value for the "meta" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
index$$Keystroke.META = index$$META; | ||
/** | ||
* Gets the bitmask value for the "meta" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
Keystroke.META = META; | ||
/** | ||
* Gets the bitmask value for the "alt" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
index$$Keystroke.ALT = index$$ALT; | ||
/** | ||
* Gets the bitmask value for the "alt" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
Keystroke.ALT = ALT; | ||
/** | ||
* Gets the bitmask value for the "shift" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
index$$Keystroke.SHIFT = index$$SHIFT; | ||
/** | ||
* Gets the bitmask value for the "shift" modifier. | ||
* | ||
* @return {number} | ||
*/ | ||
Keystroke.SHIFT = SHIFT; | ||
var index$$Keyboard = function() { | ||
"use strict"; | ||
/** | ||
* Simulates a keyboard with a particular key-to-character and key-to-action | ||
* mapping. Use `US_ENGLISH` to get a pre-configured keyboard. | ||
*/ | ||
var Keyboard = | ||
/** | ||
* @param {Object.<number, Keystroke>} charCodeKeyCodeMap | ||
* @param {Object.<string, number>} actionKeyCodeMap | ||
*/ | ||
function Keyboard(charCodeKeyCodeMap, actionKeyCodeMap) { | ||
this._charCodeKeyCodeMap = charCodeKeyCodeMap; | ||
this._actionKeyCodeMap = actionKeyCodeMap; | ||
}; | ||
function Keyboard(charCodeKeyCodeMap, actionKeyCodeMap) { | ||
this._charCodeKeyCodeMap = charCodeKeyCodeMap; | ||
this._actionKeyCodeMap = actionKeyCodeMap; | ||
/** | ||
* Determines the character code generated by pressing the given keystroke. | ||
* | ||
* @param {Keystroke} keystroke | ||
* @return {?number} | ||
*/ | ||
Keyboard.prototype.charCodeForKeystroke = function (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; | ||
}; | ||
index$$$__Object$defineProperty(Keyboard.prototype, "charCodeForKeystroke", { | ||
value: function(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; | ||
}, | ||
/** | ||
* 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 (type, keystroke, target) { | ||
var document = target.ownerDocument; | ||
var window = document.defaultView; | ||
var Event = window.Event; | ||
enumerable: false, | ||
writable: true | ||
}); | ||
var event; | ||
index$$$__Object$defineProperty(Keyboard.prototype, "createEventFromKeystroke", { | ||
value: function(type, keystroke, target) { | ||
var document = target.ownerDocument; | ||
var window = document.defaultView; | ||
var Event = window.Event; | ||
try { | ||
event = new Event(type); | ||
} catch (e) { | ||
event = document.createEvent("UIEvents"); | ||
} | ||
var event; | ||
event.initEvent(type, true, true); | ||
try { | ||
event = new Event(type); | ||
} catch(e) { | ||
event = document.createEvent('UIEvents'); | ||
} | ||
switch (type) { | ||
case "textInput": | ||
event.data = String.fromCharCode(this.charCodeForKeystroke(keystroke)); | ||
break; | ||
event.initEvent(type, true, true); | ||
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; | ||
} | ||
switch (type) { | ||
case 'textInput': | ||
event.data = String.fromCharCode(this.charCodeForKeystroke(keystroke)); | ||
break; | ||
return event; | ||
}; | ||
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; | ||
} | ||
/** | ||
* 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 | ||
*/ | ||
Keyboard.prototype.dispatchEventsForAction = function (action, target) { | ||
var keystroke = this.keystrokeForAction(action); | ||
this.dispatchEventsForKeystroke(keystroke, target); | ||
}; | ||
return event; | ||
}, | ||
/** | ||
* 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 (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); | ||
}; | ||
enumerable: false, | ||
writable: true | ||
}); | ||
/** | ||
* 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 | ||
*/ | ||
Keyboard.prototype.dispatchEventsForKeystroke = function (keystroke, target) { | ||
var transitionModifiers = arguments[2] === undefined ? true : arguments[2]; | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, 0, keystroke.modifiers); | ||
} | ||
index$$$__Object$defineProperty(Keyboard.prototype, "dispatchEventsForAction", { | ||
value: function(action, target) { | ||
var keystroke = this.keystrokeForAction(action); | ||
this.dispatchEventsForKeystroke(keystroke, target); | ||
}, | ||
var keydownEvent = this.createEventFromKeystroke("keydown", keystroke, target); | ||
enumerable: false, | ||
writable: true | ||
}); | ||
if (target.dispatchEvent(keydownEvent) && this.targetCanReceiveTextInput(target)) { | ||
var keypressEvent = this.createEventFromKeystroke("keypress", keystroke, target); | ||
if (keypressEvent.charCode && target.dispatchEvent(keypressEvent)) { | ||
var textinputEvent = this.createEventFromKeystroke("textInput", keystroke, target); | ||
target.dispatchEvent(textinputEvent); | ||
} | ||
} | ||
index$$$__Object$defineProperty(Keyboard.prototype, "dispatchEventsForInput", { | ||
value: function(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); | ||
}, | ||
var keyupEvent = this.createEventFromKeystroke("keyup", keystroke, target); | ||
target.dispatchEvent(keyupEvent); | ||
enumerable: false, | ||
writable: true | ||
}); | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, keystroke.modifiers, 0); | ||
} | ||
}; | ||
index$$$__Object$defineProperty(Keyboard.prototype, "dispatchEventsForKeystroke", { | ||
value: function(keystroke, target) { | ||
var transitionModifiers = (arguments[2] !== void 0 ? arguments[2] : true); | ||
/** | ||
* Transitions from one modifier state to another by dispatching key events. | ||
* | ||
* @param {EventTarget} target | ||
* @param {number} fromModifierState | ||
* @param {number} toModifierState | ||
* @private | ||
*/ | ||
Keyboard.prototype.dispatchModifierStateTransition = function (target, fromModifierState, toModifierState) { | ||
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; | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, 0, keystroke.modifiers); | ||
} | ||
if (didHaveMeta === true && willHaveMeta === false) { | ||
// Release the meta key. | ||
currentModifierState &= ~META; | ||
target.dispatchEvent(this.createEventFromKeystroke("keyup", new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
var keydownEvent = this.createEventFromKeystroke('keydown', keystroke, target); | ||
if (didHaveCtrl === true && willHaveCtrl === false) { | ||
// Release the ctrl key. | ||
currentModifierState &= ~CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke("keyup", new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (target.dispatchEvent(keydownEvent) && this.targetCanReceiveTextInput(target)) { | ||
var keypressEvent = this.createEventFromKeystroke('keypress', keystroke, target); | ||
if (keypressEvent.charCode && target.dispatchEvent(keypressEvent)) { | ||
var textinputEvent = this.createEventFromKeystroke('textInput', keystroke, target); | ||
target.dispatchEvent(textinputEvent); | ||
} | ||
} | ||
if (didHaveShift === true && willHaveShift === false) { | ||
// Release the shift key. | ||
currentModifierState &= ~SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke("keyup", new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
var keyupEvent = this.createEventFromKeystroke('keyup', keystroke, target); | ||
target.dispatchEvent(keyupEvent); | ||
if (didHaveAlt === true && willHaveAlt === false) { | ||
// Release the alt key. | ||
currentModifierState &= ~ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke("keyup", new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (transitionModifiers) { | ||
this.dispatchModifierStateTransition(target, keystroke.modifiers, 0); | ||
} | ||
}, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
if (didHaveMeta === false && willHaveMeta === true) { | ||
// Press the meta key. | ||
currentModifierState |= META; | ||
target.dispatchEvent(this.createEventFromKeystroke("keydown", new Keystroke(currentModifierState, this._actionKeyCodeMap.META), target)); | ||
} | ||
index$$$__Object$defineProperty(Keyboard.prototype, "dispatchModifierStateTransition", { | ||
value: function(target, fromModifierState, toModifierState) { | ||
var currentModifierState = fromModifierState; | ||
var didHaveMeta = (fromModifierState & index$$META) === index$$META; | ||
var willHaveMeta = (toModifierState & index$$META) === index$$META; | ||
var didHaveCtrl = (fromModifierState & index$$CTRL) === index$$CTRL; | ||
var willHaveCtrl = (toModifierState & index$$CTRL) === index$$CTRL; | ||
var didHaveShift = (fromModifierState & index$$SHIFT) === index$$SHIFT; | ||
var willHaveShift = (toModifierState & index$$SHIFT) === index$$SHIFT; | ||
var didHaveAlt = (fromModifierState & index$$ALT) === index$$ALT; | ||
var willHaveAlt = (toModifierState & index$$ALT) === index$$ALT; | ||
if (didHaveCtrl === false && willHaveCtrl === true) { | ||
// Press the ctrl key. | ||
currentModifierState |= CTRL; | ||
target.dispatchEvent(this.createEventFromKeystroke("keydown", new Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), target)); | ||
} | ||
if (didHaveMeta === true && willHaveMeta === false) { | ||
// Release the meta key. | ||
currentModifierState &= ~index$$META; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keyup', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.META), | ||
target | ||
) | ||
); | ||
} | ||
if (didHaveShift === false && willHaveShift === true) { | ||
// Press the shift key. | ||
currentModifierState |= SHIFT; | ||
target.dispatchEvent(this.createEventFromKeystroke("keydown", new Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), target)); | ||
} | ||
if (didHaveCtrl === true && willHaveCtrl === false) { | ||
// Release the ctrl key. | ||
currentModifierState &= ~index$$CTRL; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keyup', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), | ||
target | ||
) | ||
); | ||
} | ||
if (didHaveAlt === false && willHaveAlt === true) { | ||
// Press the alt key. | ||
currentModifierState |= ALT; | ||
target.dispatchEvent(this.createEventFromKeystroke("keydown", new Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), target)); | ||
} | ||
if (didHaveShift === true && willHaveShift === false) { | ||
// Release the shift key. | ||
currentModifierState &= ~index$$SHIFT; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keyup', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), | ||
target | ||
) | ||
); | ||
} | ||
if (currentModifierState !== toModifierState) { | ||
throw new Error("internal error, expected modifier state: " + toModifierState + ", got: " + currentModifierState); | ||
} | ||
}; | ||
if (didHaveAlt === true && willHaveAlt === false) { | ||
// Release the alt key. | ||
currentModifierState &= ~index$$ALT; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keyup', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), | ||
target | ||
) | ||
); | ||
} | ||
/** | ||
* Returns the keystroke associated with the given action. | ||
* | ||
* @param {string} action | ||
* @return {?Keystroke} | ||
*/ | ||
Keyboard.prototype.keystrokeForAction = function (action) { | ||
var keyCode = null; | ||
var modifiers = 0; | ||
var parts = action.split("+"); | ||
var lastPart = parts.pop(); | ||
if (didHaveMeta === false && willHaveMeta === true) { | ||
// Press the meta key. | ||
currentModifierState |= index$$META; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keydown', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.META), | ||
target | ||
) | ||
); | ||
} | ||
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 (didHaveCtrl === false && willHaveCtrl === true) { | ||
// Press the ctrl key. | ||
currentModifierState |= index$$CTRL; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keydown', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.CTRL), | ||
target | ||
) | ||
); | ||
} | ||
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 (didHaveShift === false && willHaveShift === true) { | ||
// Press the shift key. | ||
currentModifierState |= index$$SHIFT; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keydown', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.SHIFT), | ||
target | ||
) | ||
); | ||
} | ||
return new Keystroke(modifiers, keyCode); | ||
}; | ||
if (didHaveAlt === false && willHaveAlt === true) { | ||
// Press the alt key. | ||
currentModifierState |= index$$ALT; | ||
target.dispatchEvent( | ||
this.createEventFromKeystroke( | ||
'keydown', | ||
new index$$Keystroke(currentModifierState, this._actionKeyCodeMap.ALT), | ||
target | ||
) | ||
); | ||
} | ||
/** | ||
* Gets the keystroke used to generate the given character code. | ||
* | ||
* @param {number} charCode | ||
* @return {?Keystroke} | ||
*/ | ||
Keyboard.prototype.keystrokeForCharCode = function (charCode) { | ||
return this._charCodeKeyCodeMap[charCode] || null; | ||
}; | ||
if (currentModifierState !== toModifierState) { | ||
throw new Error( | ||
'internal error, expected modifier state: ' + toModifierState + | ||
', got: ' + currentModifierState | ||
); | ||
} | ||
}, | ||
/** | ||
* @param {EventTarget} target | ||
* @private | ||
*/ | ||
Keyboard.prototype.targetCanReceiveTextInput = function (target) { | ||
if (!target) { | ||
return false; | ||
} | ||
enumerable: false, | ||
writable: true | ||
}); | ||
switch (target.nodeName && target.nodeName.toLowerCase()) { | ||
case "input": | ||
var type = target.type; | ||
return !(type === "hidden" || type === "radio" || type === "checkbox"); | ||
index$$$__Object$defineProperty(Keyboard.prototype, "keystrokeForAction", { | ||
value: function(action) { | ||
var keyCode = null; | ||
var modifiers = 0; | ||
case "textarea": | ||
return true; | ||
var parts = action.split('+'); | ||
var lastPart = parts.pop(); | ||
default: | ||
return false; | ||
} | ||
}; | ||
parts.forEach(function(part) { | ||
switch (part.toUpperCase()) { | ||
case 'CTRL': modifiers |= index$$CTRL; break; | ||
case 'META': modifiers |= index$$META; break; | ||
case 'ALT': modifiers |= index$$ALT; break; | ||
case 'SHIFT': modifiers |= index$$SHIFT; break; | ||
default: | ||
throw new Error('in "' + action + '", invalid modifier: ' + part); | ||
break; | ||
} | ||
}); | ||
var US_ENGLISH_CHARCODE_KEYCODE_MAP = { | ||
32: new Keystroke(0, 32), // <space> | ||
33: new Keystroke(SHIFT, 49), // ! | ||
34: new Keystroke(SHIFT, 222), // " | ||
35: new Keystroke(SHIFT, 51), // # | ||
36: new Keystroke(SHIFT, 52), // $ | ||
37: new Keystroke(SHIFT, 53), // % | ||
38: new Keystroke(SHIFT, 55), // & | ||
39: new Keystroke(0, 222), // ' | ||
40: new Keystroke(SHIFT, 57), // ( | ||
41: new Keystroke(SHIFT, 48), // ) | ||
42: new Keystroke(SHIFT, 56), // * | ||
43: new Keystroke(SHIFT, 187), // + | ||
44: new Keystroke(0, 188), // , | ||
45: new Keystroke(0, 189), // - | ||
46: new Keystroke(0, 190), // . | ||
47: new Keystroke(0, 191), // / | ||
48: new Keystroke(0, 48), // 0 | ||
49: new Keystroke(0, 49), // 1 | ||
50: new Keystroke(0, 50), // 2 | ||
51: new Keystroke(0, 51), // 3 | ||
52: new Keystroke(0, 52), // 4 | ||
53: new Keystroke(0, 53), // 5 | ||
54: new Keystroke(0, 54), // 6 | ||
55: new Keystroke(0, 55), // 7 | ||
56: new Keystroke(0, 56), // 8 | ||
57: new Keystroke(0, 57), // 9 | ||
58: new Keystroke(SHIFT, 186), // : | ||
59: new Keystroke(0, 186), // ; | ||
60: new Keystroke(SHIFT, 188), // < | ||
61: new Keystroke(0, 187), // = | ||
62: new Keystroke(SHIFT, 190), // > | ||
63: new Keystroke(SHIFT, 191), // ? | ||
64: new Keystroke(SHIFT, 50), // @ | ||
65: new Keystroke(SHIFT, 65), // A | ||
66: new Keystroke(SHIFT, 66), // B | ||
67: new Keystroke(SHIFT, 67), // C | ||
68: new Keystroke(SHIFT, 68), // D | ||
69: new Keystroke(SHIFT, 69), // E | ||
70: new Keystroke(SHIFT, 70), // F | ||
71: new Keystroke(SHIFT, 71), // G | ||
72: new Keystroke(SHIFT, 72), // H | ||
73: new Keystroke(SHIFT, 73), // I | ||
74: new Keystroke(SHIFT, 74), // J | ||
75: new Keystroke(SHIFT, 75), // K | ||
76: new Keystroke(SHIFT, 76), // L | ||
77: new Keystroke(SHIFT, 77), // M | ||
78: new Keystroke(SHIFT, 78), // N | ||
79: new Keystroke(SHIFT, 79), // O | ||
80: new Keystroke(SHIFT, 80), // P | ||
81: new Keystroke(SHIFT, 81), // Q | ||
82: new Keystroke(SHIFT, 82), // R | ||
83: new Keystroke(SHIFT, 83), // S | ||
84: new Keystroke(SHIFT, 84), // T | ||
85: new Keystroke(SHIFT, 85), // U | ||
86: new Keystroke(SHIFT, 86), // V | ||
87: new Keystroke(SHIFT, 87), // W | ||
88: new Keystroke(SHIFT, 88), // X | ||
89: new Keystroke(SHIFT, 89), // Y | ||
90: new Keystroke(SHIFT, 90), // Z | ||
91: new Keystroke(0, 219), // [ | ||
92: new Keystroke(0, 220), // \ | ||
93: new Keystroke(0, 221), // ] | ||
96: new Keystroke(0, 192), // ` | ||
97: new Keystroke(0, 65), // a | ||
98: new Keystroke(0, 66), // b | ||
99: new Keystroke(0, 67), // c | ||
100: new Keystroke(0, 68), // d | ||
101: new Keystroke(0, 69), // e | ||
102: new Keystroke(0, 70), // f | ||
103: new Keystroke(0, 71), // g | ||
104: new Keystroke(0, 72), // h | ||
105: new Keystroke(0, 73), // i | ||
106: new Keystroke(0, 74), // j | ||
107: new Keystroke(0, 75), // k | ||
108: new Keystroke(0, 76), // l | ||
109: new Keystroke(0, 77), // m | ||
110: new Keystroke(0, 78), // n | ||
111: new Keystroke(0, 79), // o | ||
112: new Keystroke(0, 80), // p | ||
113: new Keystroke(0, 81), // q | ||
114: new Keystroke(0, 82), // r | ||
115: new Keystroke(0, 83), // s | ||
116: new Keystroke(0, 84), // t | ||
117: new Keystroke(0, 85), // u | ||
118: new Keystroke(0, 86), // v | ||
119: new Keystroke(0, 87), // w | ||
120: new Keystroke(0, 88), // x | ||
121: new Keystroke(0, 89), // y | ||
122: new Keystroke(0, 90), // z | ||
123: new Keystroke(SHIFT, 219), // { | ||
124: new Keystroke(SHIFT, 220), // | | ||
125: new Keystroke(SHIFT, 221), // } | ||
126: new Keystroke(SHIFT, 192) // ~ | ||
}; | ||
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); | ||
} | ||
var US_ENGLISH_ACTION_KEYCODE_MAP = { | ||
BACKSPACE: 8, | ||
TAB: 9, | ||
ENTER: 13, | ||
SHIFT: 16, | ||
CTRL: 17, | ||
ALT: 18, | ||
PAUSE: 19, | ||
CAPSLOCK: 20, | ||
ESCAPE: 27, | ||
PAGEUP: 33, | ||
PAGEDOWN: 34, | ||
END: 35, | ||
HOME: 36, | ||
LEFT: 37, | ||
UP: 38, | ||
RIGHT: 39, | ||
DOWN: 40, | ||
INSERT: 45, | ||
DELETE: 46, | ||
META: 91, | ||
F1: 112, | ||
F2: 113, | ||
F3: 114, | ||
F4: 115, | ||
F5: 116, | ||
F6: 117, | ||
F7: 118, | ||
F8: 119, | ||
F9: 120, | ||
F10: 121, | ||
F11: 122, | ||
F12: 123 | ||
}; | ||
return new index$$Keystroke(modifiers, keyCode); | ||
}, | ||
/** | ||
* Gets a keyboard instance configured as a U.S. English keyboard would be. | ||
* | ||
* @return {Keyboard} | ||
*/ | ||
Keyboard.US_ENGLISH = new Keyboard(US_ENGLISH_CHARCODE_KEYCODE_MAP, US_ENGLISH_ACTION_KEYCODE_MAP); | ||
enumerable: false, | ||
writable: true | ||
}); | ||
index$$$__Object$defineProperty(Keyboard.prototype, "keystrokeForCharCode", { | ||
value: function(charCode) { | ||
return this._charCodeKeyCodeMap[charCode] || null; | ||
}, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
index$$$__Object$defineProperty(Keyboard.prototype, "targetCanReceiveTextInput", { | ||
value: function(target) { | ||
if (!target) { return false; } | ||
switch (target.nodeName && target.nodeName.toLowerCase()) { | ||
case 'input': | ||
var type = target.type; | ||
return !(type === 'hidden' || type === 'radio' || type === 'checkbox'); | ||
case 'textarea': | ||
return true; | ||
default: | ||
return false; | ||
} | ||
}, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
return Keyboard; | ||
}(); | ||
var index$$US_ENGLISH_CHARCODE_KEYCODE_MAP = { | ||
32: new index$$Keystroke(0, 32), // <space> | ||
33: new index$$Keystroke(index$$SHIFT, 49), // ! | ||
34: new index$$Keystroke(index$$SHIFT, 222), // " | ||
35: new index$$Keystroke(index$$SHIFT, 51), // # | ||
36: new index$$Keystroke(index$$SHIFT, 52), // $ | ||
37: new index$$Keystroke(index$$SHIFT, 53), // % | ||
38: new index$$Keystroke(index$$SHIFT, 55), // & | ||
39: new index$$Keystroke(0, 222), // ' | ||
40: new index$$Keystroke(index$$SHIFT, 57), // ( | ||
41: new index$$Keystroke(index$$SHIFT, 48), // ) | ||
42: new index$$Keystroke(index$$SHIFT, 56), // * | ||
43: new index$$Keystroke(index$$SHIFT, 187), // + | ||
44: new index$$Keystroke(0, 188), // , | ||
45: new index$$Keystroke(0, 189), // - | ||
46: new index$$Keystroke(0, 190), // . | ||
47: new index$$Keystroke(0, 191), // / | ||
48: new index$$Keystroke(0, 48), // 0 | ||
49: new index$$Keystroke(0, 49), // 1 | ||
50: new index$$Keystroke(0, 50), // 2 | ||
51: new index$$Keystroke(0, 51), // 3 | ||
52: new index$$Keystroke(0, 52), // 4 | ||
53: new index$$Keystroke(0, 53), // 5 | ||
54: new index$$Keystroke(0, 54), // 6 | ||
55: new index$$Keystroke(0, 55), // 7 | ||
56: new index$$Keystroke(0, 56), // 8 | ||
57: new index$$Keystroke(0, 57), // 9 | ||
58: new index$$Keystroke(index$$SHIFT, 186), // : | ||
59: new index$$Keystroke(0, 186), // ; | ||
60: new index$$Keystroke(index$$SHIFT, 188), // < | ||
61: new index$$Keystroke(0, 187), // = | ||
62: new index$$Keystroke(index$$SHIFT, 190), // > | ||
63: new index$$Keystroke(index$$SHIFT, 191), // ? | ||
64: new index$$Keystroke(index$$SHIFT, 50), // @ | ||
65: new index$$Keystroke(index$$SHIFT, 65), // A | ||
66: new index$$Keystroke(index$$SHIFT, 66), // B | ||
67: new index$$Keystroke(index$$SHIFT, 67), // C | ||
68: new index$$Keystroke(index$$SHIFT, 68), // D | ||
69: new index$$Keystroke(index$$SHIFT, 69), // E | ||
70: new index$$Keystroke(index$$SHIFT, 70), // F | ||
71: new index$$Keystroke(index$$SHIFT, 71), // G | ||
72: new index$$Keystroke(index$$SHIFT, 72), // H | ||
73: new index$$Keystroke(index$$SHIFT, 73), // I | ||
74: new index$$Keystroke(index$$SHIFT, 74), // J | ||
75: new index$$Keystroke(index$$SHIFT, 75), // K | ||
76: new index$$Keystroke(index$$SHIFT, 76), // L | ||
77: new index$$Keystroke(index$$SHIFT, 77), // M | ||
78: new index$$Keystroke(index$$SHIFT, 78), // N | ||
79: new index$$Keystroke(index$$SHIFT, 79), // O | ||
80: new index$$Keystroke(index$$SHIFT, 80), // P | ||
81: new index$$Keystroke(index$$SHIFT, 81), // Q | ||
82: new index$$Keystroke(index$$SHIFT, 82), // R | ||
83: new index$$Keystroke(index$$SHIFT, 83), // S | ||
84: new index$$Keystroke(index$$SHIFT, 84), // T | ||
85: new index$$Keystroke(index$$SHIFT, 85), // U | ||
86: new index$$Keystroke(index$$SHIFT, 86), // V | ||
87: new index$$Keystroke(index$$SHIFT, 87), // W | ||
88: new index$$Keystroke(index$$SHIFT, 88), // X | ||
89: new index$$Keystroke(index$$SHIFT, 89), // Y | ||
90: new index$$Keystroke(index$$SHIFT, 90), // Z | ||
91: new index$$Keystroke(0, 219), // [ | ||
92: new index$$Keystroke(0, 220), // \ | ||
93: new index$$Keystroke(0, 221), // ] | ||
96: new index$$Keystroke(0, 192), // ` | ||
97: new index$$Keystroke(0, 65), // a | ||
98: new index$$Keystroke(0, 66), // b | ||
99: new index$$Keystroke(0, 67), // c | ||
100: new index$$Keystroke(0, 68), // d | ||
101: new index$$Keystroke(0, 69), // e | ||
102: new index$$Keystroke(0, 70), // f | ||
103: new index$$Keystroke(0, 71), // g | ||
104: new index$$Keystroke(0, 72), // h | ||
105: new index$$Keystroke(0, 73), // i | ||
106: new index$$Keystroke(0, 74), // j | ||
107: new index$$Keystroke(0, 75), // k | ||
108: new index$$Keystroke(0, 76), // l | ||
109: new index$$Keystroke(0, 77), // m | ||
110: new index$$Keystroke(0, 78), // n | ||
111: new index$$Keystroke(0, 79), // o | ||
112: new index$$Keystroke(0, 80), // p | ||
113: new index$$Keystroke(0, 81), // q | ||
114: new index$$Keystroke(0, 82), // r | ||
115: new index$$Keystroke(0, 83), // s | ||
116: new index$$Keystroke(0, 84), // t | ||
117: new index$$Keystroke(0, 85), // u | ||
118: new index$$Keystroke(0, 86), // v | ||
119: new index$$Keystroke(0, 87), // w | ||
120: new index$$Keystroke(0, 88), // x | ||
121: new index$$Keystroke(0, 89), // y | ||
122: new index$$Keystroke(0, 90), // z | ||
123: new index$$Keystroke(index$$SHIFT, 219), // { | ||
124: new index$$Keystroke(index$$SHIFT, 220), // | | ||
125: new index$$Keystroke(index$$SHIFT, 221), // } | ||
126: new index$$Keystroke(index$$SHIFT, 192) // ~ | ||
}; | ||
var index$$US_ENGLISH_ACTION_KEYCODE_MAP = { | ||
BACKSPACE: 8, | ||
TAB: 9, | ||
ENTER: 13, | ||
SHIFT: 16, | ||
CTRL: 17, | ||
ALT: 18, | ||
PAUSE: 19, | ||
CAPSLOCK: 20, | ||
ESCAPE: 27, | ||
PAGEUP: 33, | ||
PAGEDOWN: 34, | ||
END: 35, | ||
HOME: 36, | ||
LEFT: 37, | ||
UP: 38, | ||
RIGHT: 39, | ||
DOWN: 40, | ||
INSERT: 45, | ||
DELETE: 46, | ||
META: 91, | ||
F1: 112, | ||
F2: 113, | ||
F3: 114, | ||
F4: 115, | ||
F5: 116, | ||
F6: 117, | ||
F7: 118, | ||
F8: 119, | ||
F9: 120, | ||
F10: 121, | ||
F11: 122, | ||
F12: 123 | ||
}; | ||
/** | ||
* Gets a keyboard instance configured as a U.S. English keyboard would be. | ||
* | ||
* @return {Keyboard} | ||
*/ | ||
index$$Keyboard.US_ENGLISH = new index$$Keyboard( | ||
index$$US_ENGLISH_CHARCODE_KEYCODE_MAP, | ||
index$$US_ENGLISH_ACTION_KEYCODE_MAP | ||
); | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports.Keyboard = index$$Keyboard; | ||
exports.Keystroke = index$$Keystroke; | ||
} else if (typeof define !== 'undefined' && define.amd) { | ||
define(function() { | ||
return { Keyboard: index$$Keyboard, Keystroke: index$$Keystroke }; | ||
}); | ||
} else if (typeof window !== 'undefined') { | ||
window.keysim = { Keyboard: index$$Keyboard, Keystroke: index$$Keystroke }; | ||
} else { | ||
this.keysim = { Keyboard: index$$Keyboard, Keystroke: index$$Keystroke }; | ||
} | ||
}).call(this); | ||
//# sourceMappingURL=keysim.js.map | ||
exports.Keystroke = Keystroke; | ||
exports.Keyboard = Keyboard; | ||
}); |
{ | ||
"name": "keysim", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Keyboard simulator for JavaScript.", | ||
"main": "dist/keysim.js", | ||
"jsnext:main": "lib/index.js", | ||
"jsnext:main": "lib/keysim.js", | ||
"directories": { | ||
@@ -15,2 +15,3 @@ "test": "test" | ||
"scripts": { | ||
"build": "gobble build dist -f", | ||
"test": "mocha -R spec --recursive test" | ||
@@ -20,3 +21,3 @@ }, | ||
"dist/keysim.js", | ||
"lib/index.js" | ||
"lib/keysim.js" | ||
], | ||
@@ -33,8 +34,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"broccoli-es6-module-transpiler": "^0.2.3", | ||
"broccoli-esnext": "^0.3.0", | ||
"ember-cli": "0.1.1", | ||
"jsdom": "1.0.1", | ||
"mocha": "^1.21.4" | ||
"6to5": "^2.5.0", | ||
"esperanto": "^0.5.7", | ||
"gobble": "^0.7.1", | ||
"jsdom": "2.0.0", | ||
"mocha": "^2.1.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
973
38829
1