New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@pluginjs/keyboard

Package Overview
Dependencies
Maintainers
2
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pluginjs/keyboard - npm Package Compare versions

Comparing version 0.7.9 to 0.7.10

283

dist/keyboard.common.js
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var SimpleEmitter = _interopDefault(require('@pluginjs/simple-emitter'));
var utils = require('@pluginjs/utils');
/* eslint object-property-newline: 'off' */
const MAP_BY_CODE = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'caps_lock',
27: 'esc',
32: 'space',
33: 'page_up',
34: 'page_down',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'insert',
46: 'delete',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
65: 'a',
66: 'b',
67: 'c',
68: 'd',
69: 'e',
70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
77: 'm',
78: 'n',
79: 'o',
80: 'p',
81: 'q',
82: 'r',
83: 's',
84: 't',
85: 'u',
86: 'v',
87: 'w',
88: 'x',
89: 'y',
90: 'z',
91: 'command',
112: 'f1',
113: 'f2',
114: 'f3',
115: 'f4',
116: 'f5',
117: 'f6',
118: 'f7',
119: 'f8',
120: 'f9',
121: 'f10',
122: 'f11',
123: 'f12',
144: 'num_lock'
};
const MAP_BY_NAME = {};
for (const key in MAP_BY_CODE) {
if (Object.prototype.hasOwnProperty.call(MAP_BY_CODE, key)) {
MAP_BY_NAME[MAP_BY_CODE[key]] = Number(key);
}
}
const MODIFIERS = {
16: 'shift',
17: 'ctrl',
18: 'alt',
91: 'command'
};
class Keyboard {
constructor(element) {
this.element = element || window.document;
this.emitter = new SimpleEmitter();
this.initialize();
this.registerEvent();
}
initialize() {
this.status = {};
utils.each(MODIFIERS, (keyCode, keyName) => {
this.status[keyName] = false;
this.emitter.on("".concat(keyCode, "down"), () => {
if (this.status[keyName]) {
return;
}
this.status[keyName] = true;
});
this.emitter.on("".concat(keyCode, "up"), () => {
if (!this.status[keyName]) {
return;
}
this.status[keyName] = false;
});
});
}
registerEvent() {
const handler = e => this.handler(e);
this.element.addEventListener('keydown', handler);
this.element.addEventListener('keyup', handler);
}
handler(e) {
/* eslint consistent-return: "off" */
let keyCode = e.keyCode;
const action = e.type === 'keydown' ? 'down' : 'up';
let prefix = '';
if (keyCode === 93 || keyCode === 224) {
keyCode = 91;
} // if (!this.filter(e)) return;
if (keyCode in MODIFIERS) {
const result = this.emitter.emit(keyCode + action);
if (result === false) {
return false;
}
}
utils.each(this.status, (keyName, status) => {
if (status) {
prefix += keyName;
}
});
const eventName = prefix + keyCode + action;
if (!(eventName in this.emitter.listeners)) {
return;
}
const result = this.emitter.emit(eventName);
if (result === false) {
e.preventDefault();
e.stopPropagation();
}
return;
}
on(action, key, func) {
return this.dispatch(true, action, key, func);
}
off(action, key, func) {
return this.dispatch(false, action, key, func);
}
dispatch(toggle, action, key, func) {
const keys = this.parseKeys(this.processKey(key));
keys.forEach(key => {
const modifiers = key.modifiers;
const keyCode = key.keyCode;
let prefix = '';
if (modifiers !== null) {
for (let i = 0; i < modifiers.length; i++) {
prefix += MODIFIERS[modifiers[i]];
}
}
if (toggle) {
this.emitter.on(prefix + keyCode + action, func);
} else {
this.emitter.off(prefix + keyCode + action, func);
}
});
return this;
}
parseKeys(keys) {
const newKeys = [];
keys.map(key => {
const newKey = {};
let modifiers = null;
key = key.split('+');
const length = key.length;
if (length > 1) {
modifiers = this.processModifiers(key);
key = [key[length - 1]];
}
key = this.getKeyCode(key[0]);
newKey.modifiers = modifiers;
newKey.keyCode = key;
newKeys.push(newKey);
return key;
});
return newKeys;
}
processKey(key) {
key = key.toLowerCase().replace(/\s/g, '');
const keys = key.split(',');
if (keys[keys.length - 1] === '') {
keys[keys.length - 2] += ',';
}
return keys;
}
processModifiers(key) {
const modifiers = key.slice(0, key.length - 1);
for (let i = 0; i < modifiers.length; i++) {
modifiers[i] = MAP_BY_NAME[modifiers[i]];
}
modifiers.sort();
return modifiers;
}
distribute(action, key, func) {
return func === null || typeof func === 'undefined' ? this.off(action, key, func) : this.on(action, key, func);
}
getKeyName(keyCode) {
return MAP_BY_CODE[keyCode];
}
getKeyCode(keyName) {
return MAP_BY_NAME[keyName];
}
up(key, func) {
return this.distribute('up', key, func);
}
down(key, func) {
return this.distribute('down', key, func);
}
static of() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return new this(...args);
}
}
const keyboard = function keyboard() {
return Keyboard.of(...arguments);
};
module.exports = keyboard;

4

dist/keyboard.common.min.js
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var SimpleEmitter=_interopDefault(require("@pluginjs/simple-emitter")),utils=require("@pluginjs/utils");const MAP_BY_CODE={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",20:"caps_lock",27:"esc",32:"space",33:"page_up",34:"page_down",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"command",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",144:"num_lock"},MAP_BY_NAME={};for(const t in MAP_BY_CODE)Object.prototype.hasOwnProperty.call(MAP_BY_CODE,t)&&(MAP_BY_NAME[MAP_BY_CODE[t]]=Number(t));const MODIFIERS={16:"shift",17:"ctrl",18:"alt",91:"command"};class Keyboard{constructor(t){this.element=t||window.document,this.emitter=new SimpleEmitter,this.initialize(),this.registerEvent()}initialize(){this.status={},utils.each(MODIFIERS,(t,e)=>{this.status[e]=!1,this.emitter.on("".concat(t,"down"),()=>{this.status[e]||(this.status[e]=!0)}),this.emitter.on("".concat(t,"up"),()=>{this.status[e]&&(this.status[e]=!1)})})}registerEvent(){const t=t=>this.handler(t);this.element.addEventListener("keydown",t),this.element.addEventListener("keyup",t)}handler(t){let e=t.keyCode;const s="keydown"===t.type?"down":"up";let i="";if(93!==e&&224!==e||(e=91),e in MODIFIERS){if(!1===this.emitter.emit(e+s))return!1}utils.each(this.status,(t,e)=>{e&&(i+=t)});const r=i+e+s;r in this.emitter.listeners&&!1===this.emitter.emit(r)&&(t.preventDefault(),t.stopPropagation())}on(t,e,s){return this.dispatch(!0,t,e,s)}off(t,e,s){return this.dispatch(!1,t,e,s)}dispatch(t,e,s,i){return this.parseKeys(this.processKey(s)).forEach(s=>{const r=s.modifiers,n=s.keyCode;let o="";if(null!==r)for(let t=0;t<r.length;t++)o+=MODIFIERS[r[t]];t?this.emitter.on(o+n+e,i):this.emitter.off(o+n+e,i)}),this}parseKeys(t){const e=[];return t.map(t=>{const s={};let i=null;const r=(t=t.split("+")).length;return r>1&&(i=this.processModifiers(t),t=[t[r-1]]),t=this.getKeyCode(t[0]),s.modifiers=i,s.keyCode=t,e.push(s),t}),e}processKey(t){const e=(t=t.toLowerCase().replace(/\s/g,"")).split(",");return""===e[e.length-1]&&(e[e.length-2]+=","),e}processModifiers(t){const e=t.slice(0,t.length-1);for(let t=0;t<e.length;t++)e[t]=MAP_BY_NAME[e[t]];return e.sort(),e}distribute(t,e,s){return null==s?this.off(t,e,s):this.on(t,e,s)}getKeyName(t){return MAP_BY_CODE[t]}getKeyCode(t){return MAP_BY_NAME[t]}up(t,e){return this.distribute("up",t,e)}down(t,e){return this.distribute("down",t,e)}static of(){for(var t=arguments.length,e=new Array(t),s=0;s<t;s++)e[s]=arguments[s];return new this(...e)}}const keyboard=function(){return Keyboard.of(...arguments)};module.exports=keyboard;
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
import SimpleEmitter from '@pluginjs/simple-emitter';
import { each } from '@pluginjs/utils';
/* eslint object-property-newline: 'off' */
const MAP_BY_CODE = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'caps_lock',
27: 'esc',
32: 'space',
33: 'page_up',
34: 'page_down',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'insert',
46: 'delete',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
65: 'a',
66: 'b',
67: 'c',
68: 'd',
69: 'e',
70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
77: 'm',
78: 'n',
79: 'o',
80: 'p',
81: 'q',
82: 'r',
83: 's',
84: 't',
85: 'u',
86: 'v',
87: 'w',
88: 'x',
89: 'y',
90: 'z',
91: 'command',
112: 'f1',
113: 'f2',
114: 'f3',
115: 'f4',
116: 'f5',
117: 'f6',
118: 'f7',
119: 'f8',
120: 'f9',
121: 'f10',
122: 'f11',
123: 'f12',
144: 'num_lock'
};
const MAP_BY_NAME = {};
for (const key in MAP_BY_CODE) {
if (Object.prototype.hasOwnProperty.call(MAP_BY_CODE, key)) {
MAP_BY_NAME[MAP_BY_CODE[key]] = Number(key);
}
}
const MODIFIERS = {
16: 'shift',
17: 'ctrl',
18: 'alt',
91: 'command'
};
class Keyboard {
constructor(element) {
this.element = element || window.document;
this.emitter = new SimpleEmitter();
this.initialize();
this.registerEvent();
}
initialize() {
this.status = {};
each(MODIFIERS, (keyCode, keyName) => {
this.status[keyName] = false;
this.emitter.on("".concat(keyCode, "down"), () => {
if (this.status[keyName]) {
return;
}
this.status[keyName] = true;
});
this.emitter.on("".concat(keyCode, "up"), () => {
if (!this.status[keyName]) {
return;
}
this.status[keyName] = false;
});
});
}
registerEvent() {
const handler = e => this.handler(e);
this.element.addEventListener('keydown', handler);
this.element.addEventListener('keyup', handler);
}
handler(e) {
/* eslint consistent-return: "off" */
let keyCode = e.keyCode;
const action = e.type === 'keydown' ? 'down' : 'up';
let prefix = '';
if (keyCode === 93 || keyCode === 224) {
keyCode = 91;
} // if (!this.filter(e)) return;
if (keyCode in MODIFIERS) {
const result = this.emitter.emit(keyCode + action);
if (result === false) {
return false;
}
}
each(this.status, (keyName, status) => {
if (status) {
prefix += keyName;
}
});
const eventName = prefix + keyCode + action;
if (!(eventName in this.emitter.listeners)) {
return;
}
const result = this.emitter.emit(eventName);
if (result === false) {
e.preventDefault();
e.stopPropagation();
}
return;
}
on(action, key, func) {
return this.dispatch(true, action, key, func);
}
off(action, key, func) {
return this.dispatch(false, action, key, func);
}
dispatch(toggle, action, key, func) {
const keys = this.parseKeys(this.processKey(key));
keys.forEach(key => {
const modifiers = key.modifiers;
const keyCode = key.keyCode;
let prefix = '';
if (modifiers !== null) {
for (let i = 0; i < modifiers.length; i++) {
prefix += MODIFIERS[modifiers[i]];
}
}
if (toggle) {
this.emitter.on(prefix + keyCode + action, func);
} else {
this.emitter.off(prefix + keyCode + action, func);
}
});
return this;
}
parseKeys(keys) {
const newKeys = [];
keys.map(key => {
const newKey = {};
let modifiers = null;
key = key.split('+');
const length = key.length;
if (length > 1) {
modifiers = this.processModifiers(key);
key = [key[length - 1]];
}
key = this.getKeyCode(key[0]);
newKey.modifiers = modifiers;
newKey.keyCode = key;
newKeys.push(newKey);
return key;
});
return newKeys;
}
processKey(key) {
key = key.toLowerCase().replace(/\s/g, '');
const keys = key.split(',');
if (keys[keys.length - 1] === '') {
keys[keys.length - 2] += ',';
}
return keys;
}
processModifiers(key) {
const modifiers = key.slice(0, key.length - 1);
for (let i = 0; i < modifiers.length; i++) {
modifiers[i] = MAP_BY_NAME[modifiers[i]];
}
modifiers.sort();
return modifiers;
}
distribute(action, key, func) {
return func === null || typeof func === 'undefined' ? this.off(action, key, func) : this.on(action, key, func);
}
getKeyName(keyCode) {
return MAP_BY_CODE[keyCode];
}
getKeyCode(keyName) {
return MAP_BY_NAME[keyName];
}
up(key, func) {
return this.distribute('up', key, func);
}
down(key, func) {
return this.distribute('down', key, func);
}
static of() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return new this(...args);
}
}
const keyboard = function keyboard() {
return Keyboard.of(...arguments);
};
export default keyboard;
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
import t from"@pluginjs/simple-emitter";import{each as e}from"@pluginjs/utils";const s={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",20:"caps_lock",27:"esc",32:"space",33:"page_up",34:"page_down",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"command",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",144:"num_lock"},i={};for(const t in s)Object.prototype.hasOwnProperty.call(s,t)&&(i[s[t]]=Number(t));const r={16:"shift",17:"ctrl",18:"alt",91:"command"};export default function(){return class{constructor(e){this.element=e||window.document,this.emitter=new t,this.initialize(),this.registerEvent()}initialize(){this.status={},e(r,(t,e)=>{this.status[e]=!1,this.emitter.on("".concat(t,"down"),()=>{this.status[e]||(this.status[e]=!0)}),this.emitter.on("".concat(t,"up"),()=>{this.status[e]&&(this.status[e]=!1)})})}registerEvent(){const t=t=>this.handler(t);this.element.addEventListener("keydown",t),this.element.addEventListener("keyup",t)}handler(t){let s=t.keyCode;const i="keydown"===t.type?"down":"up";let n="";if(93!==s&&224!==s||(s=91),s in r&&!1===this.emitter.emit(s+i))return!1;e(this.status,(t,e)=>{e&&(n+=t)});const o=n+s+i;o in this.emitter.listeners&&!1===this.emitter.emit(o)&&(t.preventDefault(),t.stopPropagation())}on(t,e,s){return this.dispatch(!0,t,e,s)}off(t,e,s){return this.dispatch(!1,t,e,s)}dispatch(t,e,s,i){return this.parseKeys(this.processKey(s)).forEach(s=>{const n=s.modifiers,o=s.keyCode;let h="";if(null!==n)for(let t=0;t<n.length;t++)h+=r[n[t]];t?this.emitter.on(h+o+e,i):this.emitter.off(h+o+e,i)}),this}parseKeys(t){const e=[];return t.map(t=>{const s={};let i=null;const r=(t=t.split("+")).length;return r>1&&(i=this.processModifiers(t),t=[t[r-1]]),t=this.getKeyCode(t[0]),s.modifiers=i,s.keyCode=t,e.push(s),t}),e}processKey(t){const e=(t=t.toLowerCase().replace(/\s/g,"")).split(",");return""===e[e.length-1]&&(e[e.length-2]+=","),e}processModifiers(t){const e=t.slice(0,t.length-1);for(let t=0;t<e.length;t++)e[t]=i[e[t]];return e.sort(),e}distribute(t,e,s){return null==s?this.off(t,e,s):this.on(t,e,s)}getKeyName(t){return s[t]}getKeyCode(t){return i[t]}up(t,e){return this.distribute("up",t,e)}down(t,e){return this.distribute("down",t,e)}static of(){for(var t=arguments.length,e=new Array(t),s=0;s<t;s++)e[s]=arguments[s];return new this(...e)}}.of(...arguments)}
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@pluginjs/simple-emitter'), require('@pluginjs/utils')) :
typeof define === 'function' && define.amd ? define(['@pluginjs/simple-emitter', '@pluginjs/utils'], factory) :
(global = global || self, global['@pluginjs/keyboard'] = factory(global['@pluginjs/simple-emitter'], global['@pluginjs/utils']));
}(this, function (SimpleEmitter, utils) { 'use strict';
SimpleEmitter = SimpleEmitter && SimpleEmitter.hasOwnProperty('default') ? SimpleEmitter['default'] : SimpleEmitter;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
/* eslint object-property-newline: 'off' */
var MAP_BY_CODE = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'caps_lock',
27: 'esc',
32: 'space',
33: 'page_up',
34: 'page_down',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'insert',
46: 'delete',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
65: 'a',
66: 'b',
67: 'c',
68: 'd',
69: 'e',
70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
77: 'm',
78: 'n',
79: 'o',
80: 'p',
81: 'q',
82: 'r',
83: 's',
84: 't',
85: 'u',
86: 'v',
87: 'w',
88: 'x',
89: 'y',
90: 'z',
91: 'command',
112: 'f1',
113: 'f2',
114: 'f3',
115: 'f4',
116: 'f5',
117: 'f6',
118: 'f7',
119: 'f8',
120: 'f9',
121: 'f10',
122: 'f11',
123: 'f12',
144: 'num_lock'
};
var MAP_BY_NAME = {};
for (var key in MAP_BY_CODE) {
if (Object.prototype.hasOwnProperty.call(MAP_BY_CODE, key)) {
MAP_BY_NAME[MAP_BY_CODE[key]] = Number(key);
}
}
var MODIFIERS = {
16: 'shift',
17: 'ctrl',
18: 'alt',
91: 'command'
};
var Keyboard =
/*#__PURE__*/
function () {
function Keyboard(element) {
_classCallCheck(this, Keyboard);
this.element = element || window.document;
this.emitter = new SimpleEmitter();
this.initialize();
this.registerEvent();
}
_createClass(Keyboard, [{
key: "initialize",
value: function initialize() {
var _this = this;
this.status = {};
utils.each(MODIFIERS, function (keyCode, keyName) {
_this.status[keyName] = false;
_this.emitter.on("".concat(keyCode, "down"), function () {
if (_this.status[keyName]) {
return;
}
_this.status[keyName] = true;
});
_this.emitter.on("".concat(keyCode, "up"), function () {
if (!_this.status[keyName]) {
return;
}
_this.status[keyName] = false;
});
});
}
}, {
key: "registerEvent",
value: function registerEvent() {
var _this2 = this;
var handler = function handler(e) {
return _this2.handler(e);
};
this.element.addEventListener('keydown', handler);
this.element.addEventListener('keyup', handler);
}
}, {
key: "handler",
value: function handler(e) {
/* eslint consistent-return: "off" */
var keyCode = e.keyCode;
var action = e.type === 'keydown' ? 'down' : 'up';
var prefix = '';
if (keyCode === 93 || keyCode === 224) {
keyCode = 91;
} // if (!this.filter(e)) return;
if (keyCode in MODIFIERS) {
var _result = this.emitter.emit(keyCode + action);
if (_result === false) {
return false;
}
}
utils.each(this.status, function (keyName, status) {
if (status) {
prefix += keyName;
}
});
var eventName = prefix + keyCode + action;
if (!(eventName in this.emitter.listeners)) {
return;
}
var result = this.emitter.emit(eventName);
if (result === false) {
e.preventDefault();
e.stopPropagation();
}
return;
}
}, {
key: "on",
value: function on(action, key, func) {
return this.dispatch(true, action, key, func);
}
}, {
key: "off",
value: function off(action, key, func) {
return this.dispatch(false, action, key, func);
}
}, {
key: "dispatch",
value: function dispatch(toggle, action, key, func) {
var _this3 = this;
var keys = this.parseKeys(this.processKey(key));
keys.forEach(function (key) {
var modifiers = key.modifiers;
var keyCode = key.keyCode;
var prefix = '';
if (modifiers !== null) {
for (var i = 0; i < modifiers.length; i++) {
prefix += MODIFIERS[modifiers[i]];
}
}
if (toggle) {
_this3.emitter.on(prefix + keyCode + action, func);
} else {
_this3.emitter.off(prefix + keyCode + action, func);
}
});
return this;
}
}, {
key: "parseKeys",
value: function parseKeys(keys) {
var _this4 = this;
var newKeys = [];
keys.map(function (key) {
var newKey = {};
var modifiers = null;
key = key.split('+');
var length = key.length;
if (length > 1) {
modifiers = _this4.processModifiers(key);
key = [key[length - 1]];
}
key = _this4.getKeyCode(key[0]);
newKey.modifiers = modifiers;
newKey.keyCode = key;
newKeys.push(newKey);
return key;
});
return newKeys;
}
}, {
key: "processKey",
value: function processKey(key) {
key = key.toLowerCase().replace(/\s/g, '');
var keys = key.split(',');
if (keys[keys.length - 1] === '') {
keys[keys.length - 2] += ',';
}
return keys;
}
}, {
key: "processModifiers",
value: function processModifiers(key) {
var modifiers = key.slice(0, key.length - 1);
for (var i = 0; i < modifiers.length; i++) {
modifiers[i] = MAP_BY_NAME[modifiers[i]];
}
modifiers.sort();
return modifiers;
}
}, {
key: "distribute",
value: function distribute(action, key, func) {
return func === null || typeof func === 'undefined' ? this.off(action, key, func) : this.on(action, key, func);
}
}, {
key: "getKeyName",
value: function getKeyName(keyCode) {
return MAP_BY_CODE[keyCode];
}
}, {
key: "getKeyCode",
value: function getKeyCode(keyName) {
return MAP_BY_NAME[keyName];
}
}, {
key: "up",
value: function up(key, func) {
return this.distribute('up', key, func);
}
}, {
key: "down",
value: function down(key, func) {
return this.distribute('down', key, func);
}
}], [{
key: "of",
value: function of() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _construct(this, args);
}
}]);
return Keyboard;
}();
var keyboard = function keyboard() {
return Keyboard.of.apply(Keyboard, arguments);
};
return keyboard;
}));
/*!
* @pluginjs/keyboard v0.7.9 (https://pluginjs.com)
* @pluginjs/keyboard v0.7.10 (https://pluginjs.com)
* Copyright 2019 Creation Studio Limited
* Released under the GPL-3.0 License.
*/
undefined
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@pluginjs/simple-emitter"),require("@pluginjs/utils")):"function"==typeof define&&define.amd?define(["@pluginjs/simple-emitter","@pluginjs/utils"],t):(e=e||self)["@pluginjs/keyboard"]=t(e["@pluginjs/simple-emitter"],e["@pluginjs/utils"])}(this,function(e,t){"use strict";function n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function r(e,t){return(r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t,n){return(i=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}()?Reflect.construct:function(e,t,n){var i=[null];i.push.apply(i,t);var u=new(Function.bind.apply(e,i));return n&&r(u,n.prototype),u}).apply(null,arguments)}e=e&&e.hasOwnProperty("default")?e.default:e;var u={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",20:"caps_lock",27:"esc",32:"space",33:"page_up",34:"page_down",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"insert",46:"delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",91:"command",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",144:"num_lock"},o={};for(var s in u)Object.prototype.hasOwnProperty.call(u,s)&&(o[u[s]]=Number(s));var a={16:"shift",17:"ctrl",18:"alt",91:"command"},f=function(){function r(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),this.element=t||window.document,this.emitter=new e,this.initialize(),this.registerEvent()}var s,f,c;return s=r,c=[{key:"of",value:function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return i(this,t)}}],(f=[{key:"initialize",value:function(){var e=this;this.status={},t.each(a,function(t,n){e.status[n]=!1,e.emitter.on("".concat(t,"down"),function(){e.status[n]||(e.status[n]=!0)}),e.emitter.on("".concat(t,"up"),function(){e.status[n]&&(e.status[n]=!1)})})}},{key:"registerEvent",value:function(){var e=this,t=function(t){return e.handler(t)};this.element.addEventListener("keydown",t),this.element.addEventListener("keyup",t)}},{key:"handler",value:function(e){var n=e.keyCode,r="keydown"===e.type?"down":"up",i="";if((93!==n&&224!==n||(n=91),n in a)&&!1===this.emitter.emit(n+r))return!1;t.each(this.status,function(e,t){t&&(i+=e)});var u=i+n+r;u in this.emitter.listeners&&(!1===this.emitter.emit(u)&&(e.preventDefault(),e.stopPropagation()))}},{key:"on",value:function(e,t,n){return this.dispatch(!0,e,t,n)}},{key:"off",value:function(e,t,n){return this.dispatch(!1,e,t,n)}},{key:"dispatch",value:function(e,t,n,r){var i=this;return this.parseKeys(this.processKey(n)).forEach(function(n){var u=n.modifiers,o=n.keyCode,s="";if(null!==u)for(var f=0;f<u.length;f++)s+=a[u[f]];e?i.emitter.on(s+o+t,r):i.emitter.off(s+o+t,r)}),this}},{key:"parseKeys",value:function(e){var t=this,n=[];return e.map(function(e){var r={},i=null,u=(e=e.split("+")).length;return u>1&&(i=t.processModifiers(e),e=[e[u-1]]),e=t.getKeyCode(e[0]),r.modifiers=i,r.keyCode=e,n.push(r),e}),n}},{key:"processKey",value:function(e){var t=(e=e.toLowerCase().replace(/\s/g,"")).split(",");return""===t[t.length-1]&&(t[t.length-2]+=","),t}},{key:"processModifiers",value:function(e){for(var t=e.slice(0,e.length-1),n=0;n<t.length;n++)t[n]=o[t[n]];return t.sort(),t}},{key:"distribute",value:function(e,t,n){return null==n?this.off(e,t,n):this.on(e,t,n)}},{key:"getKeyName",value:function(e){return u[e]}},{key:"getKeyCode",value:function(e){return o[e]}},{key:"up",value:function(e,t){return this.distribute("up",e,t)}},{key:"down",value:function(e,t){return this.distribute("down",e,t)}}])&&n(s.prototype,f),c&&n(s,c),r}();return function(){return f.of.apply(f,arguments)}});

@@ -14,3 +14,3 @@ {

},
"version": "0.7.9",
"version": "0.7.10",
"category": "core",

@@ -37,10 +37,10 @@ "main": "dist/keyboard.common.js",

"dependencies": {
"@pluginjs/dom": "^0.7.9",
"@pluginjs/simple-emitter": "^0.7.8",
"@pluginjs/utils": "^0.7.9"
"@pluginjs/dom": "^0.7.10",
"@pluginjs/simple-emitter": "^0.7.9",
"@pluginjs/utils": "^0.7.10"
},
"devDependencies": {
"@babel/core": "^7.4.4",
"@pluginjs/browserslist-config": "^1.2.5",
"@pluginjs/cli": "^0.7.8",
"@pluginjs/browserslist-config": "^1.2.6",
"@pluginjs/cli": "^0.7.9",
"babel-jest": "*",

@@ -71,3 +71,4 @@ "jest": "*",

],
"title": "Plugin"
"title": "Plugin",
"gitHead": "9ae759d6378f7bd8b952e5c1951dec91a101966e"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc