Support INSERT key. released v2.0.9

2.x v2.0.9
jaywcjlove 7 years ago
parent 516287ac8f
commit d1a4647830

120
dist/hotkeys.js vendored

@ -28,14 +28,14 @@
})(function() {
var define, module, exports;
//IE对indexOf方法的支持
if (!Array.prototype.indexOf) {
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj) {
for (var i = 0; i < this.length; i++) if (this[i] === obj) return i;
return -1;
};
}
// IE对lastIndexOf方法的支持
if (!Array.prototype.lastIndexOf) {
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function(obj) {
for (var i = this.length - 1; i >= 0; i--) if (this[i] === obj) return i;
return -1;
@ -58,6 +58,8 @@
down: 40,
del: 46,
delete: 46,
ins: 45,
insert: 45,
home: 36,
end: 35,
pageup: 33,
@ -100,26 +102,26 @@
17: false
}, _handlers = {};
// F1~F12 特殊键
for (var k = 1; k < 20; k++) {
for (var k = 1; k < 20; k++) {
_keyMap["f" + k] = 111 + k;
}
// 兼容Firefox处理
modifierMap[isff ? 224 : 91] = "metaKey";
modifierMap[isff ? 224 : 91] = "metaKey";
_mods[isff ? 224 : 91] = false;
// 返回键码
function code(x) {
function code(x) {
return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
}
// 设置获取当前范围(默认为'所有'
function setScope(scope) {
function setScope(scope) {
_scope = scope || "all";
}
// 获取当前范围
function getScope() {
function getScope() {
return _scope || "all";
}
// 绑定事件
function addEvent(object, event, method) {
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
@ -129,61 +131,62 @@
}
}
// 判断摁下的键是否为某个键返回true或者false
function isPressed(keyCode) {
function isPressed(keyCode) {
if (typeof keyCode === "string") {
keyCode = code(keyCode);
}
// 转换成键码
}
return _downKeys.indexOf(keyCode) !== -1;
}
// 获取摁下绑定键的键值
function getPressedKeyCodes() {
function getPressedKeyCodes() {
return _downKeys.slice(0);
}
// 处理keydown事件
function dispatch(event) {
function dispatch(event) {
var key = event.keyCode || event.which || event.charCode, scope, asterisk = _handlers["*"];
// 搜集绑定的键
if (_downKeys.indexOf(key) === -1) _downKeys.push(key);
if (_downKeys.indexOf(key) === -1) _downKeys.push(key);
// Gecko(Firefox)的command键值224在Webkit(Chrome)中保持一致
// Webkit左右command键值不一样
if (key === 93 || key === 224) key = 91;
if (key === 93 || key === 224) key = 91;
if (key in _mods) {
_mods[key] = true;
// 将特殊字符的key注册到 hotkeys 上
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = true;
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = true;
if (!asterisk) return;
}
// 将modifierMap里面的修饰键绑定到event中
for (var e in _mods) _mods[e] = event[modifierMap[e]];
for (var e in _mods) _mods[e] = event[modifierMap[e]];
// 表单控件过滤 默认表单控件不触发快捷键
if (!hotkeys.filter.call(this, event)) return;
if (!hotkeys.filter.call(this, event)) return;
// 获取范围 默认为all
scope = getScope();
scope = getScope();
// 对任何快捷键都需要做的处理
if (asterisk) {
if (asterisk) {
for (i = 0; i < asterisk.length; i++) {
if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope);
}
}
// key 不在_handlers中返回
if (!(key in _handlers)) return;
if (!(key in _handlers)) return;
for (var i = 0; i < _handlers[key].length; i++) {
// 找到处理内容
eventHandler(event, _handlers[key][i], scope);
}
}
// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
function eventHandler(event, handler, scope) {
var modifiersMatch;
// 看它是否在当前范围
if (handler.scope === scope || handler.scope === "all") {
if (handler.scope === scope || handler.scope === "all") {
//检查是否匹配修饰符如果有返回true
modifiersMatch = handler.mods.length > 0;
for (var y in _mods) {
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) modifiersMatch = false;
}
// 调用处理程序,如果是修饰键不做处理
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === "*") {
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === "*") {
if (handler.method(event, handler) === false) {
if (event.preventDefault) event.preventDefault(); else event.returnValue = false;
if (event.stopPropagation) event.stopPropagation();
@ -193,34 +196,34 @@
}
}
// 解除绑定某个范围的快捷键
function unbind(key, scope) {
function unbind(key, scope) {
var multipleKeys = getKeys(key), keys, mods = [], obj;
for (var i = 0; i < multipleKeys.length; i++) {
// 将组合快捷键拆分为数组
keys = multipleKeys[i].split("+");
// 记录每个组合键中的修饰键的键码 返回数组
if (keys.length > 1) mods = getMods(keys);
if (keys.length > 1) mods = getMods(keys);
// 获取除修饰键外的键值key
key = keys[keys.length - 1];
key = keys[keys.length - 1];
key = key === "*" ? "*" : code(key);
// 判断是否传入范围,没有就获取范围
if (!scope) scope = getScope();
if (!scope) scope = getScope();
// 如何key不在 _handlers 中返回不做处理
if (!_handlers[key]) return;
if (!_handlers[key]) return;
// 清空 handlers 中数据,
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
for (var r = 0; r < _handlers[key].length; r++) {
for (var r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r];
// 判断是否在范围内并且键值相同
if (obj.scope === scope && compareArray(obj.mods, mods)) _handlers[key][r] = {};
if (obj.scope === scope && compareArray(obj.mods, mods)) _handlers[key][r] = {};
}
}
}
// 循环删除handlers中的所有 scope(范围)
function deleteScope(scope, newScope) {
function deleteScope(scope, newScope) {
var key, handlers, i;
// 没有指定scope获取scope
if (!scope) scope = getScope();
if (!scope) scope = getScope();
for (key in _handlers) {
handlers = _handlers[key];
for (i = 0; i < handlers.length; ) {
@ -228,10 +231,10 @@
}
}
// 如果scope被删除将scope重置为all
if (getScope() === scope) setScope(newScope || "all");
if (getScope() === scope) setScope(newScope || "all");
}
//比较修饰键的数组
function compareArray(a1, a2) {
function compareArray(a1, a2) {
var arr1 = a1.length >= a2.length ? a1 : a2;
var arr2 = a1.length >= a2.length ? a2 : a1;
for (var i = 0; i < arr1.length; i++) {
@ -240,28 +243,28 @@
return true;
}
// 表单控件控件判断 返回 Boolean
function filter(event) {
function filter(event) {
var tagName = (event.target || event.srcElement).tagName;
// 忽略这些标签情况下快捷键无效
return !(tagName === "INPUT" || tagName === "SELECT" || tagName === "TEXTAREA");
return !(tagName === "INPUT" || tagName === "SELECT" || tagName === "TEXTAREA");
}
// 修饰键转换成对应的键码
function getMods(key) {
function getMods(key) {
var mods = key.slice(0, key.length - 1);
for (var i = 0; i < mods.length; i++) mods[i] = _modifier[mods[i].toLowerCase()];
return mods;
}
// 处理传的key字符串转换成数组
function getKeys(key) {
function getKeys(key) {
if (!key) key = "";
var keys, index;
key = key.replace(/\s/g, "");
// 匹配任何空白字符,包括空格、制表符、换页符等等
keys = key.split(",");
// 同时设置多个快捷键,以','分割
index = keys.lastIndexOf("");
// 匹配任何空白字符,包括空格、制表符、换页符等等
keys = key.split(",");
// 同时设置多个快捷键,以','分割
index = keys.lastIndexOf("");
// 快捷键可能包含',',需特殊处理
for (;index >= 0; ) {
for (;index >= 0; ) {
keys[index - 1] += ",";
keys.splice(index, 1);
index = keys.lastIndexOf("");
@ -269,7 +272,7 @@
return keys;
}
// 在全局document上设置快捷键
if (typeof document !== "undefined") {
if (typeof document !== "undefined") {
addEvent(document, "keydown", function(event) {
dispatch(event);
});
@ -278,40 +281,41 @@
});
}
// 清除修饰键
function clearModifier(event) {
function clearModifier(event) {
var key = event.keyCode || event.which || event.charCode, i = _downKeys.indexOf(key);
// 从列表中清除按压过的键
if (i >= 0) _downKeys.splice(i, 1);
if (i >= 0) _downKeys.splice(i, 1);
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
if (key === 93 || key === 224) key = 91;
if (key === 93 || key === 224) key = 91;
if (key in _mods) {
_mods[key] = false;
// 将修饰键重置为false
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
}
}
// 主体hotkeys函数
function hotkeys(key, scope, method) {
function hotkeys(key, scope, method) {
var keys = getKeys(key), // 需要处理的快捷键列表
mods = [], i = 0;
// 对为设定范围的判断
if (method === undefined) {
if (method === undefined) {
method = scope;
scope = "all";
}
// scope默认为all所有范围都有效
}
// 对于每个快捷键进行处理
for (;i < keys.length; i++) {
for (;i < keys.length; i++) {
key = keys[i].split("+");
// 按键列表
mods = [];
// 按键列表
mods = [];
// 如果是组合快捷键取得组合快捷键
if (key.length > 1) mods = getMods(key);
if (key.length > 1) mods = getMods(key);
// 将非修饰键转化为键码
key = key[key.length - 1];
key = key[key.length - 1];
key = key === "*" ? "*" : code(key);
// *表示匹配所有快捷键
// *表示匹配所有快捷键
// 判断key是否在_handlers中不在就赋一个空数组
if (!(key in _handlers)) _handlers[key] = [];
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({
shortcut: keys[i],
scope: scope,

@ -1,3 +1,3 @@
/*! hotkeys-js v2.0.8 | MIT (c) 2018 kenny wang <wowohoo@qq.com> | https://github.com/jaywcjlove/hotkeys.git */
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).hotkeys=e()}}(function(){function e(e){return p[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)}function n(e){h=e||"all"}function t(){return h||"all"}function o(e,n,t){e.addEventListener?e.addEventListener(n,t,!1):e.attachEvent&&e.attachEvent("on"+n,function(){t(window.event)})}function r(e){var n,o=e.keyCode||e.which||e.charCode,r=m["*"];if(-1===y.indexOf(o)&&y.push(o),93!==o&&224!==o||(o=91),o in w){w[o]=!0;for(var f in g)g[f]===o&&(c[f]=!0);if(!r)return}for(var a in w)w[a]=e[v[a]];if(c.filter.call(this,e)){if(n=t(),r)for(l=0;l<r.length;l++)r[l].scope===n&&i(e,r[l],n);if(o in m)for(var l=0;l<m[o].length;l++)i(e,m[o][l],n)}}function i(e,n,t){var o;if(n.scope===t||"all"===n.scope){o=n.mods.length>0;for(var r in w)(!w[r]&&n.mods.indexOf(+r)>-1||w[r]&&-1===n.mods.indexOf(+r))&&(o=!1);(0!==n.mods.length||w[16]||w[18]||w[17]||w[91])&&!o&&"*"!==n.shortcut||!1===n.method(e,n)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function f(e,n){for(var t=e.length>=n.length?e:n,o=e.length>=n.length?n:e,r=0;r<t.length;r++)if(-1===o.indexOf(t[r]))return!1;return!0}function a(e){for(var n=e.slice(0,e.length-1),t=0;t<n.length;t++)n[t]=g[n[t].toLowerCase()];return n}function l(e){e||(e="");var n,t;for(t=(n=(e=e.replace(/\s/g,"")).split(",")).lastIndexOf("");t>=0;)n[t-1]+=",",n.splice(t,1),t=n.lastIndexOf("");return n}function d(e){var n=e.keyCode||e.which||e.charCode,t=y.indexOf(n);if(t>=0&&y.splice(t,1),93!==n&&224!==n||(n=91),n in w){w[n]=!1;for(var o in g)g[o]===n&&(c[o]=!1)}}function c(n,t,o){var r=l(n),i=[],f=0;for(void 0===o&&(o=t,t="all");f<r.length;f++)i=[],(n=r[f].split("+")).length>1&&(i=a(n)),(n="*"===(n=n[n.length-1])?"*":e(n))in m||(m[n]=[]),m[n].push({shortcut:r[f],scope:t,method:o,key:r[f],mods:i})}Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;n++)if(this[n]===e)return n;return-1}),Array.prototype.lastIndexOf||(Array.prototype.lastIndexOf=function(e){for(var n=this.length-1;n>=0;n--)if(this[n]===e)return n;return-1});for(var s,u="undefined"!=typeof navigator&&navigator.userAgent.toLowerCase().indexOf("firefox")>0,p={backspace:8,tab:9,clear:12,enter:13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,home:36,end:35,pageup:33,pagedown:34,"⇪":20,capslock:20,",":188,".":190,"/":191,"`":192,"-":u?173:189,"=":u?61:187,";":u?59:186,"'":222,"[":219,"]":221,"\\":220},h="all",g={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":u?224:91,cmd:u?224:91,command:u?224:91},y=[],v={16:"shiftKey",18:"altKey",17:"ctrlKey"},w={16:!1,18:!1,17:!1},m={},x=1;x<20;x++)p["f"+x]=111+x;v[u?224:91]="metaKey",w[u?224:91]=!1,"undefined"!=typeof document&&(o(document,"keydown",function(e){r(e)}),o(document,"keyup",function(e){d(e)})),s={setScope:n,getScope:t,deleteScope:function(e,o){var r,i,f;e||(e=t());for(r in m)for(i=m[r],f=0;f<i.length;)i[f].scope===e?i.splice(f,1):f++;t()===e&&n(o||"all")},getPressedKeyCodes:function(){return y.slice(0)},isPressed:function(n){return"string"==typeof n&&(n=e(n)),-1!==y.indexOf(n)},filter:function(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)},unbind:function(n,o){for(var r,i,d=l(n),c=[],s=0;s<d.length;s++){if((r=d[s].split("+")).length>1&&(c=a(r)),n=r[r.length-1],n="*"===n?"*":e(n),o||(o=t()),!m[n])return;for(var u=0;u<m[n].length;u++)(i=m[n][u]).scope===o&&f(i.mods,c)&&(m[n][u]={})}}};for(var k in s)c[k]=s[k];if("undefined"!=typeof window){var O=window.hotkeys;c.noConflict=function(e){return e&&window.hotkeys===c&&(window.hotkeys=O),c},window.hotkeys=c}return window.hotkeys=c,c});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).hotkeys=e()}}(function(){Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;n++)if(this[n]===e)return n;return-1}),Array.prototype.lastIndexOf||(Array.prototype.lastIndexOf=function(e){for(var n=this.length-1;n>=0;n--)if(this[n]===e)return n;return-1});for(var e,n="undefined"!=typeof navigator&&navigator.userAgent.toLowerCase().indexOf("firefox")>0,t={backspace:8,tab:9,clear:12,enter:13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,"⇪":20,capslock:20,",":188,".":190,"/":191,"`":192,"-":n?173:189,"=":n?61:187,";":n?59:186,"'":222,"[":219,"]":221,"\\":220},o="all",r={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":n?224:91,cmd:n?224:91,command:n?224:91},i=[],f={16:"shiftKey",18:"altKey",17:"ctrlKey"},a={16:!1,18:!1,17:!1},l={},d=1;d<20;d++)t["f"+d]=111+d;function s(e){return t[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)}function c(e){o=e||"all"}function u(){return o||"all"}function p(e,n,t){e.addEventListener?e.addEventListener(n,t,!1):e.attachEvent&&e.attachEvent("on"+n,function(){t(window.event)})}function h(e,n,t){var o;if(n.scope===t||"all"===n.scope){for(var r in o=n.mods.length>0,a)(!a[r]&&n.mods.indexOf(+r)>-1||a[r]&&-1===n.mods.indexOf(+r))&&(o=!1);(0!==n.mods.length||a[16]||a[18]||a[17]||a[91])&&!o&&"*"!==n.shortcut||!1===n.method(e,n)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function g(e,n){for(var t=e.length>=n.length?e:n,o=e.length>=n.length?n:e,r=0;r<t.length;r++)if(-1===o.indexOf(t[r]))return!1;return!0}function y(e){for(var n=e.slice(0,e.length-1),t=0;t<n.length;t++)n[t]=r[n[t].toLowerCase()];return n}function v(e){var n,t;for(e||(e=""),t=(n=(e=e.replace(/\s/g,"")).split(",")).lastIndexOf("");t>=0;)n[t-1]+=",",n.splice(t,1),t=n.lastIndexOf("");return n}function w(e,n,t){var o=v(e),r=[],i=0;for(void 0===t&&(t=n,n="all");i<o.length;i++)r=[],(e=o[i].split("+")).length>1&&(r=y(e)),(e="*"===(e=e[e.length-1])?"*":s(e))in l||(l[e]=[]),l[e].push({shortcut:o[i],scope:n,method:t,key:o[i],mods:r})}for(var m in f[n?224:91]="metaKey",a[n?224:91]=!1,"undefined"!=typeof document&&(p(document,"keydown",function(e){!function(e){var n,t=e.keyCode||e.which||e.charCode,o=l["*"];if(-1===i.indexOf(t)&&i.push(t),93!==t&&224!==t||(t=91),t in a){for(var d in a[t]=!0,r)r[d]===t&&(w[d]=!0);if(!o)return}for(var s in a)a[s]=e[f[s]];if(w.filter.call(this,e)){if(n=u(),o)for(c=0;c<o.length;c++)o[c].scope===n&&h(e,o[c],n);if(t in l)for(var c=0;c<l[t].length;c++)h(e,l[t][c],n)}}(e)}),p(document,"keyup",function(e){!function(e){var n=e.keyCode||e.which||e.charCode,t=i.indexOf(n);t>=0&&i.splice(t,1);93!==n&&224!==n||(n=91);if(n in a)for(var o in a[n]=!1,r)r[o]===n&&(w[o]=!1)}(e)})),e={setScope:c,getScope:u,deleteScope:function(e,n){var t,o,r;for(t in e||(e=u()),l)for(o=l[t],r=0;r<o.length;)o[r].scope===e?o.splice(r,1):r++;u()===e&&c(n||"all")},getPressedKeyCodes:function(){return i.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=s(e)),-1!==i.indexOf(e)},filter:function(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)},unbind:function(e,n){for(var t,o,r=v(e),i=[],f=0;f<r.length;f++){if((t=r[f].split("+")).length>1&&(i=y(t)),e="*"===(e=t[t.length-1])?"*":s(e),n||(n=u()),!l[e])return;for(var a=0;a<l[e].length;a++)(o=l[e][a]).scope===n&&g(o.mods,i)&&(l[e][a]={})}}})w[m]=e[m];if("undefined"!=typeof window){var x=window.hotkeys;w.noConflict=function(e){return e&&window.hotkeys===w&&(window.hotkeys=x),w},window.hotkeys=w}return window.hotkeys=w,w});

@ -40,4 +40,4 @@
},
"license": "MIT",
"dependencies": {}
}
}

@ -301,5 +301,5 @@ tape('hotkeys 单元测试', function (t) {
__triggerKeyboardEvent(document.body, 65,{
ctrlKey:true
});
});

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save