Comparing version 0.1.0 to 0.1.1
import Component from "@egjs/component"; | ||
export interface KeyControllerEvent { | ||
inputEvent: KeyboardEvent; | ||
key: string; | ||
keyCode: number; | ||
ctrlKey: boolean; | ||
altKey: boolean; | ||
shiftKey: boolean; | ||
metaKey: boolean; | ||
} | ||
export declare class KeyController extends Component { | ||
constructor(); | ||
keydown(comb: string, callback: (e: KeyboardEvent) => void): this; | ||
keyup(comb: string, callback: (e: KeyboardEvent) => void): this; | ||
constructor(container?: Window | Document | HTMLElement); | ||
keydown(comb: string, callback: (e: KeyControllerEvent) => void): this; | ||
keydown(callback: (e: KeyControllerEvent) => void): this; | ||
keyup(comb: string, callback: (e: KeyControllerEvent) => void): this; | ||
keyup(callback: (e: KeyControllerEvent) => void): this; | ||
private triggerEvent; | ||
@@ -7,0 +18,0 @@ private keydownEvent; |
@@ -7,3 +7,3 @@ /* | ||
repository: git+https://github.com/daybrush/keycon.git | ||
version: 0.1.0 | ||
version: 0.1.1 | ||
*/ | ||
@@ -242,2 +242,39 @@ import Component from '@egjs/component'; | ||
/* | ||
Copyright (c) 2018 Daybrush | ||
@name: @daybrush/utils | ||
license: MIT | ||
author: Daybrush | ||
repository: https://github.com/daybrush/utils | ||
@version 0.7.0 | ||
*/ | ||
/** | ||
* get string "string" | ||
* @memberof Consts | ||
* @example | ||
import {STRING} from "@daybrush/utils"; | ||
console.log(STRING); // "string" | ||
*/ | ||
var STRING = "string"; | ||
/** | ||
* Check the type that the value is string. | ||
* @memberof Utils | ||
* @param {string} value - Value to check the type | ||
* @return {} true if the type is correct, false otherwise | ||
* @example | ||
import {isString} from "@daybrush/utils"; | ||
console.log(isString("1234")); // true | ||
console.log(isString(undefined)); // false | ||
console.log(isString(1)); // false | ||
console.log(isString(null)); // false | ||
*/ | ||
function isString(value) { | ||
return typeof value === STRING; | ||
} | ||
var codeData = { | ||
@@ -264,3 +301,7 @@ "+": "plus", | ||
function KeyController() { | ||
function KeyController(container) { | ||
if (container === void 0) { | ||
container = window; | ||
} | ||
var _this = _super.call(this) || this; | ||
@@ -276,4 +317,4 @@ | ||
window.addEventListener("keydown", _this.keydownEvent); | ||
window.addEventListener("keyup", _this.keyupEvent); | ||
container.addEventListener("keydown", _this.keydownEvent); | ||
container.addEventListener("keyup", _this.keyupEvent); | ||
return _this; | ||
@@ -285,7 +326,15 @@ } | ||
__proto.keydown = function (comb, callback) { | ||
return this.on("keydown." + comb, callback); | ||
if (isString(comb)) { | ||
return this.on("keydown." + comb, callback); | ||
} else { | ||
return this.on("keydown", callback); | ||
} | ||
}; | ||
__proto.keyup = function (comb, callback) { | ||
return this.on("keyup." + comb, callback); | ||
if (typeof comb === "string") { | ||
return this.on("keyup." + comb, callback); | ||
} else { | ||
return this.on("keyup", callback); | ||
} | ||
}; | ||
@@ -295,4 +344,6 @@ | ||
var key = getKey(e.keyCode); | ||
this.trigger(type + "." + key, { | ||
var param = { | ||
key: key, | ||
inputEvent: e, | ||
keyCode: e.keyCode, | ||
ctrlKey: e.ctrlKey, | ||
@@ -302,3 +353,5 @@ altKey: e.altKey, | ||
metaKey: e.metaKey | ||
}); | ||
}; | ||
this.trigger(type, param); | ||
this.trigger(type + "." + key, param); | ||
}; | ||
@@ -305,0 +358,0 @@ |
@@ -7,5 +7,5 @@ /* | ||
repository: git+https://github.com/daybrush/keycon.git | ||
version: 0.1.0 | ||
version: 0.1.1 | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@egjs/component")):"function"==typeof define&&define.amd?define(["@egjs/component"],t):(e=e||self).keycon=t(e.eg.Component)}(this,function(e){"use strict";var o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};var t,n=(function(e,t){function n(e){if(e&&"object"==typeof e){var t=e.which||e.keyCode||e.charCode;t&&(e=t)}if("number"==typeof e)return i[e];var n,o=String(e);return(n=r[o.toLowerCase()])?n:(n=a[o.toLowerCase()])||(1===o.length?o.charCodeAt(0):void 0)}n.isEventKey=function(e,t){if(e&&"object"==typeof e){var n=e.which||e.keyCode||e.charCode;if(null==n)return!1;if("string"==typeof t){var o;if(o=r[t.toLowerCase()])return o===n;if(o=a[t.toLowerCase()])return o===n}else if("number"==typeof t)return t===n;return!1}};var r=(t=e.exports=n).code=t.codes={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,"pause/break":19,"caps lock":20,esc:27,space:32,"page up":33,"page down":34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,delete:46,command:91,"left command":91,"right command":93,"numpad *":106,"numpad +":107,"numpad -":109,"numpad .":110,"numpad /":111,"num lock":144,"scroll lock":145,"my computer":182,"my calculator":183,";":186,"=":187,",":188,"-":189,".":190,"/":191,"`":192,"[":219,"\\":220,"]":221,"'":222},a=t.aliases={windows:91,"⇧":16,"⌥":18,"⌃":17,"⌘":91,ctl:17,control:17,option:18,pause:19,break:19,caps:20,return:13,escape:27,spc:32,spacebar:32,pgup:33,pgdn:34,ins:45,del:46,cmd:91};for(o=97;o<123;o++)r[String.fromCharCode(o)]=o-32;for(var o=48;o<58;o++)r[o-48]=o;for(o=1;o<13;o++)r["f"+o]=o+111;for(o=0;o<10;o++)r["numpad "+o]=o+96;var i=t.names=t.title={};for(o in r)i[r[o]]=o;for(var c in a)r[c]=a[c]}(t={exports:{}},t.exports),t.exports),r=(n.code,n.codes,n.aliases,n.names),a=(n.title,{"+":"plus","left command":"meta","right command":"meta"});var i=function(e){function t(){var t=e.call(this)||this;return t.keydownEvent=function(e){t.triggerEvent("keydown",e)},t.keyupEvent=function(e){t.triggerEvent("keyup",e)},window.addEventListener("keydown",t.keydownEvent),window.addEventListener("keyup",t.keyupEvent),t}!function(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(t,e);var n=t.prototype;return n.keydown=function(e,t){return this.on("keydown."+e,t)},n.keyup=function(e,t){return this.on("keyup."+e,t)},n.triggerEvent=function(e,t){var n=function(e){var t=r[e]||"";for(var n in a)t=t.replace(n,a[n]);return t.replace(/\s/g,"")}(t.keyCode);this.trigger(e+"."+n,{inputEvent:t,ctrlKey:t.ctrlKey,altKey:t.altKey,shiftKey:t.shiftKey,metaKey:t.metaKey})},t}(e);return function(){return new i}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@egjs/component"),require("@daybrush/utils")):"function"==typeof define&&define.amd?define(["@egjs/component","@daybrush/utils"],t):(e=e||self).keycon=t(e.eg.Component,e.utils)}(this,function(e,r){"use strict";var o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};var t,n=(function(e,t){function n(e){if(e&&"object"==typeof e){var t=e.which||e.keyCode||e.charCode;t&&(e=t)}if("number"==typeof e)return a[e];var n,r=String(e);return(n=o[r.toLowerCase()])?n:(n=i[r.toLowerCase()])||(1===r.length?r.charCodeAt(0):void 0)}n.isEventKey=function(e,t){if(e&&"object"==typeof e){var n=e.which||e.keyCode||e.charCode;if(null==n)return!1;if("string"==typeof t){var r;if(r=o[t.toLowerCase()])return r===n;if(r=i[t.toLowerCase()])return r===n}else if("number"==typeof t)return t===n;return!1}};var o=(t=e.exports=n).code=t.codes={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,"pause/break":19,"caps lock":20,esc:27,space:32,"page up":33,"page down":34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,delete:46,command:91,"left command":91,"right command":93,"numpad *":106,"numpad +":107,"numpad -":109,"numpad .":110,"numpad /":111,"num lock":144,"scroll lock":145,"my computer":182,"my calculator":183,";":186,"=":187,",":188,"-":189,".":190,"/":191,"`":192,"[":219,"\\":220,"]":221,"'":222},i=t.aliases={windows:91,"⇧":16,"⌥":18,"⌃":17,"⌘":91,ctl:17,control:17,option:18,pause:19,break:19,caps:20,return:13,escape:27,spc:32,spacebar:32,pgup:33,pgdn:34,ins:45,del:46,cmd:91};for(r=97;r<123;r++)o[String.fromCharCode(r)]=r-32;for(var r=48;r<58;r++)o[r-48]=r;for(r=1;r<13;r++)o["f"+r]=r+111;for(r=0;r<10;r++)o["numpad "+r]=r+96;var a=t.names=t.title={};for(r in o)a[o[r]]=r;for(var u in i)o[u]=i[u]}(t={exports:{}},t.exports),t.exports),i=(n.code,n.codes,n.aliases,n.names),a=(n.title,{"+":"plus","left command":"meta","right command":"meta"});var u=function(n){function e(e){void 0===e&&(e=window);var t=n.call(this)||this;return t.keydownEvent=function(e){t.triggerEvent("keydown",e)},t.keyupEvent=function(e){t.triggerEvent("keyup",e)},e.addEventListener("keydown",t.keydownEvent),e.addEventListener("keyup",t.keyupEvent),t}!function(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(e,n);var t=e.prototype;return t.keydown=function(e,t){return r.isString(e)?this.on("keydown."+e,t):this.on("keydown",t)},t.keyup=function(e,t){return"string"==typeof e?this.on("keyup."+e,t):this.on("keyup",t)},t.triggerEvent=function(e,t){var n=function(e){var t=i[e]||"";for(var n in a)t=t.replace(n,a[n]);return t.replace(/\s/g,"")}(t.keyCode),r={key:n,inputEvent:t,keyCode:t.keyCode,ctrlKey:t.ctrlKey,altKey:t.altKey,shiftKey:t.shiftKey,metaKey:t.metaKey};this.trigger(e,r),this.trigger(e+"."+n,r)},e}(e);return function(){return new u}}); | ||
//# sourceMappingURL=keycon.js.map |
@@ -7,3 +7,3 @@ /* | ||
repository: git+https://github.com/daybrush/keycon.git | ||
version: 0.1.0 | ||
version: 0.1.1 | ||
*/ | ||
@@ -537,2 +537,39 @@ (function (global, factory) { | ||
/* | ||
Copyright (c) 2018 Daybrush | ||
@name: @daybrush/utils | ||
license: MIT | ||
author: Daybrush | ||
repository: https://github.com/daybrush/utils | ||
@version 0.7.0 | ||
*/ | ||
/** | ||
* get string "string" | ||
* @memberof Consts | ||
* @example | ||
import {STRING} from "@daybrush/utils"; | ||
console.log(STRING); // "string" | ||
*/ | ||
var STRING = "string"; | ||
/** | ||
* Check the type that the value is string. | ||
* @memberof Utils | ||
* @param {string} value - Value to check the type | ||
* @return {} true if the type is correct, false otherwise | ||
* @example | ||
import {isString} from "@daybrush/utils"; | ||
console.log(isString("1234")); // true | ||
console.log(isString(undefined)); // false | ||
console.log(isString(1)); // false | ||
console.log(isString(null)); // false | ||
*/ | ||
function isString(value) { | ||
return typeof value === STRING; | ||
} | ||
var codeData = { | ||
@@ -559,3 +596,7 @@ "+": "plus", | ||
function KeyController() { | ||
function KeyController(container) { | ||
if (container === void 0) { | ||
container = window; | ||
} | ||
var _this = _super.call(this) || this; | ||
@@ -571,4 +612,4 @@ | ||
window.addEventListener("keydown", _this.keydownEvent); | ||
window.addEventListener("keyup", _this.keyupEvent); | ||
container.addEventListener("keydown", _this.keydownEvent); | ||
container.addEventListener("keyup", _this.keyupEvent); | ||
return _this; | ||
@@ -580,7 +621,15 @@ } | ||
__proto.keydown = function (comb, callback) { | ||
return this.on("keydown." + comb, callback); | ||
if (isString(comb)) { | ||
return this.on("keydown." + comb, callback); | ||
} else { | ||
return this.on("keydown", callback); | ||
} | ||
}; | ||
__proto.keyup = function (comb, callback) { | ||
return this.on("keyup." + comb, callback); | ||
if (typeof comb === "string") { | ||
return this.on("keyup." + comb, callback); | ||
} else { | ||
return this.on("keyup", callback); | ||
} | ||
}; | ||
@@ -590,4 +639,6 @@ | ||
var key = getKey(e.keyCode); | ||
this.trigger(type + "." + key, { | ||
var param = { | ||
key: key, | ||
inputEvent: e, | ||
keyCode: e.keyCode, | ||
ctrlKey: e.ctrlKey, | ||
@@ -597,3 +648,5 @@ altKey: e.altKey, | ||
metaKey: e.metaKey | ||
}); | ||
}; | ||
this.trigger(type, param); | ||
this.trigger(type + "." + key, param); | ||
}; | ||
@@ -600,0 +653,0 @@ |
@@ -7,5 +7,5 @@ /* | ||
repository: git+https://github.com/daybrush/keycon.git | ||
version: 0.1.0 | ||
version: 0.1.1 | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).keycon=t()}(this,function(){"use strict";var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function f(e){return void 0===e}var e=function(){var e=function(){function e(){this._eventHandler={},this.options={}}var t=e.prototype;return t.trigger=function(e,t){void 0===t&&(t={});var n=this._eventHandler[e]||[];if(!(0<n.length))return!0;n=n.concat(),t.eventType=e;var r=!1,o=[t],i=0;t.stop=function(){r=!0},t.currentTarget=this;for(var a=arguments.length,f=new Array(2<a?a-2:0),u=2;u<a;u++)f[u-2]=arguments[u];for(1<=f.length&&(o=o.concat(f)),i=0;n[i];i++)n[i].apply(this,o);return!r},t.once=function(o,i){if("object"==typeof o&&f(i)){var e,t=o;for(e in t)this.once(e,t[e]);return this}if("string"==typeof o&&"function"==typeof i){var a=this;this.on(o,function e(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];i.apply(a,n),a.off(o,e)})}return this},t.hasOn=function(e){return!!this._eventHandler[e]},t.on=function(e,t){if("object"==typeof e&&f(t)){var n,r=e;for(n in r)this.on(n,r[n]);return this}if("string"==typeof e&&"function"==typeof t){var o=this._eventHandler[e];f(o)&&(this._eventHandler[e]=[],o=this._eventHandler[e]),o.push(t)}return this},t.off=function(e,t){if(f(e))return this._eventHandler={},this;if(f(t)){if("string"==typeof e)return this._eventHandler[e]=void 0,this;var n,r=e;for(n in r)this.off(n,r[n]);return this}var o,i,a=this._eventHandler[e];if(a)for(o=0;void 0!==(i=a[o]);o++)if(i===t){a=a.splice(o,1);break}return this},e}();return e.VERSION="2.1.2",e}();var t,n=(function(e,t){function n(e){if(e&&"object"==typeof e){var t=e.which||e.keyCode||e.charCode;t&&(e=t)}if("number"==typeof e)return a[e];var n,r=String(e);return(n=o[r.toLowerCase()])?n:(n=i[r.toLowerCase()])||(1===r.length?r.charCodeAt(0):void 0)}n.isEventKey=function(e,t){if(e&&"object"==typeof e){var n=e.which||e.keyCode||e.charCode;if(null==n)return!1;if("string"==typeof t){var r;if(r=o[t.toLowerCase()])return r===n;if(r=i[t.toLowerCase()])return r===n}else if("number"==typeof t)return t===n;return!1}};var o=(t=e.exports=n).code=t.codes={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,"pause/break":19,"caps lock":20,esc:27,space:32,"page up":33,"page down":34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,delete:46,command:91,"left command":91,"right command":93,"numpad *":106,"numpad +":107,"numpad -":109,"numpad .":110,"numpad /":111,"num lock":144,"scroll lock":145,"my computer":182,"my calculator":183,";":186,"=":187,",":188,"-":189,".":190,"/":191,"`":192,"[":219,"\\":220,"]":221,"'":222},i=t.aliases={windows:91,"⇧":16,"⌥":18,"⌃":17,"⌘":91,ctl:17,control:17,option:18,pause:19,break:19,caps:20,return:13,escape:27,spc:32,spacebar:32,pgup:33,pgdn:34,ins:45,del:46,cmd:91};for(r=97;r<123;r++)o[String.fromCharCode(r)]=r-32;for(var r=48;r<58;r++)o[r-48]=r;for(r=1;r<13;r++)o["f"+r]=r+111;for(r=0;r<10;r++)o["numpad "+r]=r+96;var a=t.names=t.title={};for(r in o)a[o[r]]=r;for(var f in i)o[f]=i[f]}(t={exports:{}},t.exports),t.exports),o=(n.code,n.codes,n.aliases,n.names),i=(n.title,{"+":"plus","left command":"meta","right command":"meta"});var a=function(e){function t(){var t=e.call(this)||this;return t.keydownEvent=function(e){t.triggerEvent("keydown",e)},t.keyupEvent=function(e){t.triggerEvent("keyup",e)},window.addEventListener("keydown",t.keydownEvent),window.addEventListener("keyup",t.keyupEvent),t}!function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(t,e);var n=t.prototype;return n.keydown=function(e,t){return this.on("keydown."+e,t)},n.keyup=function(e,t){return this.on("keyup."+e,t)},n.triggerEvent=function(e,t){var n=function(e){var t=o[e]||"";for(var n in i)t=t.replace(n,i[n]);return t.replace(/\s/g,"")}(t.keyCode);this.trigger(e+"."+n,{inputEvent:t,ctrlKey:t.ctrlKey,altKey:t.altKey,shiftKey:t.shiftKey,metaKey:t.metaKey})},t}(e);return function(){return new a}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).keycon=t()}(this,function(){"use strict";var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};function f(e){return void 0===e}var e=function(){var e=function(){function e(){this._eventHandler={},this.options={}}var t=e.prototype;return t.trigger=function(e,t){void 0===t&&(t={});var n=this._eventHandler[e]||[];if(!(0<n.length))return!0;n=n.concat(),t.eventType=e;var r=!1,o=[t],i=0;t.stop=function(){r=!0},t.currentTarget=this;for(var a=arguments.length,f=new Array(2<a?a-2:0),u=2;u<a;u++)f[u-2]=arguments[u];for(1<=f.length&&(o=o.concat(f)),i=0;n[i];i++)n[i].apply(this,o);return!r},t.once=function(o,i){if("object"==typeof o&&f(i)){var e,t=o;for(e in t)this.once(e,t[e]);return this}if("string"==typeof o&&"function"==typeof i){var a=this;this.on(o,function e(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];i.apply(a,n),a.off(o,e)})}return this},t.hasOn=function(e){return!!this._eventHandler[e]},t.on=function(e,t){if("object"==typeof e&&f(t)){var n,r=e;for(n in r)this.on(n,r[n]);return this}if("string"==typeof e&&"function"==typeof t){var o=this._eventHandler[e];f(o)&&(this._eventHandler[e]=[],o=this._eventHandler[e]),o.push(t)}return this},t.off=function(e,t){if(f(e))return this._eventHandler={},this;if(f(t)){if("string"==typeof e)return this._eventHandler[e]=void 0,this;var n,r=e;for(n in r)this.off(n,r[n]);return this}var o,i,a=this._eventHandler[e];if(a)for(o=0;void 0!==(i=a[o]);o++)if(i===t){a=a.splice(o,1);break}return this},e}();return e.VERSION="2.1.2",e}();var t,n=(function(e,t){function n(e){if(e&&"object"==typeof e){var t=e.which||e.keyCode||e.charCode;t&&(e=t)}if("number"==typeof e)return a[e];var n,r=String(e);return(n=o[r.toLowerCase()])?n:(n=i[r.toLowerCase()])||(1===r.length?r.charCodeAt(0):void 0)}n.isEventKey=function(e,t){if(e&&"object"==typeof e){var n=e.which||e.keyCode||e.charCode;if(null==n)return!1;if("string"==typeof t){var r;if(r=o[t.toLowerCase()])return r===n;if(r=i[t.toLowerCase()])return r===n}else if("number"==typeof t)return t===n;return!1}};var o=(t=e.exports=n).code=t.codes={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,"pause/break":19,"caps lock":20,esc:27,space:32,"page up":33,"page down":34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,delete:46,command:91,"left command":91,"right command":93,"numpad *":106,"numpad +":107,"numpad -":109,"numpad .":110,"numpad /":111,"num lock":144,"scroll lock":145,"my computer":182,"my calculator":183,";":186,"=":187,",":188,"-":189,".":190,"/":191,"`":192,"[":219,"\\":220,"]":221,"'":222},i=t.aliases={windows:91,"⇧":16,"⌥":18,"⌃":17,"⌘":91,ctl:17,control:17,option:18,pause:19,break:19,caps:20,return:13,escape:27,spc:32,spacebar:32,pgup:33,pgdn:34,ins:45,del:46,cmd:91};for(r=97;r<123;r++)o[String.fromCharCode(r)]=r-32;for(var r=48;r<58;r++)o[r-48]=r;for(r=1;r<13;r++)o["f"+r]=r+111;for(r=0;r<10;r++)o["numpad "+r]=r+96;var a=t.names=t.title={};for(r in o)a[o[r]]=r;for(var f in i)o[f]=i[f]}(t={exports:{}},t.exports),t.exports),o=(n.code,n.codes,n.aliases,n.names);n.title;var i={"+":"plus","left command":"meta","right command":"meta"};var a=function(n){function e(e){void 0===e&&(e=window);var t=n.call(this)||this;return t.keydownEvent=function(e){t.triggerEvent("keydown",e)},t.keyupEvent=function(e){t.triggerEvent("keyup",e)},e.addEventListener("keydown",t.keydownEvent),e.addEventListener("keyup",t.keyupEvent),t}!function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(e,n);var t=e.prototype;return t.keydown=function(e,t){return function(e){return"string"==typeof e}(e)?this.on("keydown."+e,t):this.on("keydown",t)},t.keyup=function(e,t){return"string"==typeof e?this.on("keyup."+e,t):this.on("keyup",t)},t.triggerEvent=function(e,t){var n=function(e){var t=o[e]||"";for(var n in i)t=t.replace(n,i[n]);return t.replace(/\s/g,"")}(t.keyCode),r={key:n,inputEvent:t,keyCode:t.keyCode,ctrlKey:t.ctrlKey,altKey:t.altKey,shiftKey:t.shiftKey,metaKey:t.metaKey};this.trigger(e,r),this.trigger(e+"."+n,r)},e}(e);return function(){return new a}}); | ||
//# sourceMappingURL=keycon.pkgd.min.js.map |
{ | ||
"name": "keycon", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Keyboard Controller", | ||
@@ -5,0 +5,0 @@ "main": "./dist/keycon.js", |
@@ -11,2 +11,3 @@ import builder from "@daybrush/builder"; | ||
external: { | ||
"@daybrush/utils": "utils", | ||
"@egjs/component": "eg.Component", | ||
@@ -23,2 +24,3 @@ }, | ||
external: { | ||
"@daybrush/utils": "utils", | ||
"@egjs/component": "eg.Component", | ||
@@ -25,0 +27,0 @@ }, |
import Component from "@egjs/component"; | ||
import { names } from "keycode"; | ||
import { isString } from "@daybrush/utils"; | ||
@@ -18,21 +19,48 @@ const codeData = { | ||
} | ||
export interface KeyControllerEvent { | ||
inputEvent: KeyboardEvent; | ||
key: string; | ||
keyCode: number; | ||
ctrlKey: boolean; | ||
altKey: boolean; | ||
shiftKey: boolean; | ||
metaKey: boolean; | ||
} | ||
export class KeyController extends Component { | ||
constructor() { | ||
constructor(container: Window | Document | HTMLElement = window) { | ||
super(); | ||
window.addEventListener("keydown", this.keydownEvent); | ||
window.addEventListener("keyup", this.keyupEvent); | ||
container.addEventListener("keydown", this.keydownEvent); | ||
container.addEventListener("keyup", this.keyupEvent); | ||
} | ||
public keydown(comb: string, callback: (e: KeyboardEvent) => void) { | ||
return this.on(`keydown.${comb}`, callback); | ||
public keydown(comb: string, callback: (e: KeyControllerEvent) => void): this; | ||
public keydown(callback: (e: KeyControllerEvent) => void): this; | ||
public keydown( | ||
comb: string | ((e: KeyControllerEvent) => void), | ||
callback?: (e: KeyControllerEvent) => void, | ||
) { | ||
if (isString(comb)) { | ||
return this.on(`keydown.${comb}`, callback); | ||
} else { | ||
return this.on(`keydown`, callback); | ||
} | ||
} | ||
public keyup(comb: string, callback: (e: KeyboardEvent) => void) { | ||
return this.on(`keyup.${comb}`, callback); | ||
public keyup(comb: string, callback: (e: KeyControllerEvent) => void): this; | ||
public keyup(callback: (e: KeyControllerEvent) => void): this; | ||
public keyup( | ||
comb: string | ((e: KeyControllerEvent) => void), | ||
callback?: (e: KeyControllerEvent) => void, | ||
) { | ||
if (typeof comb === "string") { | ||
return this.on(`keyup.${comb}`, callback); | ||
} else { | ||
return this.on(`keyup`, callback); | ||
} | ||
} | ||
private triggerEvent(type: "keydown" | "keyup", e: KeyboardEvent) { | ||
const key = getKey(e.keyCode); | ||
this.trigger(`${type}.${key}`, { | ||
const param: KeyControllerEvent = { | ||
key, | ||
inputEvent: e, | ||
keyCode: e.keyCode, | ||
ctrlKey: e.ctrlKey, | ||
@@ -42,3 +70,5 @@ altKey: e.altKey, | ||
metaKey: e.metaKey, | ||
}); | ||
}; | ||
this.trigger(type, param); | ||
this.trigger(`${type}.${key}`, param); | ||
} | ||
@@ -45,0 +75,0 @@ private keydownEvent = (e: KeyboardEvent) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177562
1127