hotkeys-js
Advanced tools
Comparing version 1.0.9 to 1.0.10
{ | ||
"name": "hotkeys-js", | ||
"version": "1.0.5", | ||
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.", | ||
@@ -5,0 +4,0 @@ "main": "dist/hotkeys.js", |
/*! | ||
* hotkeys-js v1.0.8 | ||
* hotkeys-js v1.0.10 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
@@ -10,281 +10,305 @@ * | ||
*/ | ||
;(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} else { | ||
root.hotkeys = factory(); | ||
} | ||
}(this, function() { | ||
//IE对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; | ||
}; | ||
} | ||
var _api,//对外API | ||
isff = navigator.userAgent.toLowerCase().indexOf('firefox') > 0, | ||
_keyMap = {//特殊键 | ||
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, | ||
',': 188, '.': 190, '/': 191, | ||
'`': 192, '-': isff?173:189, '=': isff?61:187, | ||
';': isff?59:186, '\'': 222, | ||
'[': 219, ']': 221, '\\': 220 | ||
}, | ||
_scope = 'all',//默认热键范围 | ||
_modifier = {//修饰键 | ||
'⇧': 16, shift: 16, | ||
'⌥': 18, alt: 18, option: 18, | ||
'⌃': 17, ctrl: 17, control: 17, | ||
'⌘': isff?224:91, command: isff?224:91 | ||
}, | ||
_downKeys=[],//记录摁下的绑定键 | ||
modifierMap = { | ||
16:'shiftKey', | ||
18:'altKey', | ||
17:'ctrlKey' | ||
}, | ||
_mods = { 16: false, 18: false, 17: false}, | ||
//返回键码 | ||
code = function(x){ | ||
return _keyMap[x] || x.toUpperCase().charCodeAt(0); | ||
}, | ||
_handlers={}; | ||
// F1~F12 特殊键 | ||
for(var k=1;k<20;k++) { | ||
_keyMap['f'+k] = 111+k; | ||
} | ||
// 兼容Firefox处理 | ||
modifierMap[isff?224:91] = 'metaKey'; | ||
_mods[isff?224:91] = false; | ||
//设置获取当前范围(默认为'所有') | ||
function setScope(scope){ _scope = scope || 'all';} | ||
function getScope(){ return _scope || 'all';} | ||
//绑定事件 | ||
function addEvent(object, event, method) { | ||
if (object.addEventListener){ | ||
object.addEventListener(event, method, false); | ||
}else if(object.attachEvent){ | ||
object.attachEvent('on'+event, function(){ method(window.event); }); | ||
(function(f) { | ||
if (typeof exports === "object" && typeof module !== "undefined") { | ||
module.exports = f(); | ||
} else if (typeof define === "function" && define.amd) { | ||
define([], f); | ||
} else { | ||
var g; | ||
if (typeof window !== "undefined") { | ||
g = window; | ||
} else if (typeof global !== "undefined") { | ||
g = global; | ||
} else if (typeof self !== "undefined") { | ||
g = self; | ||
} else { | ||
g = this; | ||
} | ||
g.hotkeys = f(); | ||
} | ||
} | ||
//判断摁下的键是否为某个键,返回true或者false | ||
function isPressed(keyCode) { | ||
if(typeof(keyCode) === 'string'){ | ||
keyCode = code(keyCode);//转换成键码 | ||
})(function() { | ||
var define, module, exports; | ||
//IE对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; | ||
}; | ||
} | ||
return _downKeys.indexOf(keyCode) !==-1; | ||
} | ||
//获取摁下绑定键的键值 | ||
function getPressedKeyCodes (argument) { return _downKeys.slice(0);} | ||
//处理keydown事件 | ||
function dispatch (event) { | ||
var key = event.keyCode,scope,asterisk = _handlers['*']; | ||
//搜集绑定的键 | ||
if(_downKeys.indexOf(key)===-1) _downKeys.push(key); | ||
//Gecko(Friefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
//Webkit左右command键值不一样 | ||
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; | ||
if(!asterisk) return; | ||
var _api, //对外API | ||
isff = navigator.userAgent.toLowerCase().indexOf("firefox") > 0, _keyMap = { | ||
//特殊键 | ||
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, | ||
",": 188, | ||
".": 190, | ||
"/": 191, | ||
"`": 192, | ||
"-": isff ? 173 : 189, | ||
"=": isff ? 61 : 187, | ||
";": isff ? 59 : 186, | ||
"'": 222, | ||
"[": 219, | ||
"]": 221, | ||
"\\": 220 | ||
}, _scope = "all", //默认热键范围 | ||
_modifier = { | ||
//修饰键 | ||
"⇧": 16, | ||
shift: 16, | ||
"⌥": 18, | ||
alt: 18, | ||
option: 18, | ||
"⌃": 17, | ||
ctrl: 17, | ||
control: 17, | ||
"⌘": isff ? 224 : 91, | ||
command: isff ? 224 : 91 | ||
}, _downKeys = [], //记录摁下的绑定键 | ||
modifierMap = { | ||
16: "shiftKey", | ||
18: "altKey", | ||
17: "ctrlKey" | ||
}, _mods = { | ||
16: false, | ||
18: false, | ||
17: false | ||
}, //返回键码 | ||
code = function(x) { | ||
return _keyMap[x] || x.toUpperCase().charCodeAt(0); | ||
}, _handlers = {}; | ||
// F1~F12 特殊键 | ||
for (var k = 1; k < 20; k++) { | ||
_keyMap["f" + k] = 111 + k; | ||
} | ||
//将modifierMap里面的修饰键绑定到event中 | ||
for(var e in _mods) _mods[e] = event[modifierMap[e]]; | ||
//表单控件控件过滤 默认表单控件不触发快捷键 | ||
if(!hotkeys.filter.call(this,event)) return; | ||
//获取范围 默认为all | ||
scope = getScope(); | ||
//对任何按键做处理 | ||
if(asterisk) for (i = 0; i < asterisk.length; i++) { | ||
if(asterisk[i].scope === scope) eventHandler(event,asterisk[i],scope); | ||
// 兼容Firefox处理 | ||
modifierMap[isff ? 224 : 91] = "metaKey"; | ||
_mods[isff ? 224 : 91] = false; | ||
//设置获取当前范围(默认为'所有') | ||
function setScope(scope) { | ||
_scope = scope || "all"; | ||
} | ||
// key 不在_handlers中返回 | ||
if (!(key in _handlers)) return; | ||
for(var i = 0; i < _handlers[key].length; i++) { | ||
//找到处理内容 | ||
eventHandler(event,_handlers[key][i],scope); | ||
function getScope() { | ||
return _scope || "all"; | ||
} | ||
} | ||
function eventHandler(event,handler,scope){ | ||
var modifiersMatch; | ||
//看它是否在当前范围 | ||
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; | ||
//绑定事件 | ||
function addEvent(object, event, method) { | ||
if (object.addEventListener) { | ||
object.addEventListener(event, method, false); | ||
} else if (object.attachEvent) { | ||
object.attachEvent("on" + event, function() { | ||
method(window.event); | ||
}); | ||
} | ||
// 调用处理程序,如果是修饰键不做处理 | ||
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(); | ||
if(event.cancelBubble) event.cancelBubble = true; | ||
} | ||
//判断摁下的键是否为某个键,返回true或者false | ||
function isPressed(keyCode) { | ||
if (typeof keyCode === "string") { | ||
keyCode = code(keyCode); | ||
} | ||
return _downKeys.indexOf(keyCode) !== -1; | ||
} | ||
//获取摁下绑定键的键值 | ||
function getPressedKeyCodes(argument) { | ||
return _downKeys.slice(0); | ||
} | ||
//处理keydown事件 | ||
function dispatch(event) { | ||
var key = event.keyCode, scope, asterisk = _handlers["*"]; | ||
//搜集绑定的键 | ||
if (_downKeys.indexOf(key) === -1) _downKeys.push(key); | ||
//Gecko(Friefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
//Webkit左右command键值不一样 | ||
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; | ||
if (!asterisk) return; | ||
} | ||
//将modifierMap里面的修饰键绑定到event中 | ||
for (var e in _mods) _mods[e] = event[modifierMap[e]]; | ||
//表单控件控件过滤 默认表单控件不触发快捷键 | ||
if (!hotkeys.filter.call(this, event)) return; | ||
//获取范围 默认为all | ||
scope = getScope(); | ||
//对任何按键做处理 | ||
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; | ||
for (var i = 0; i < _handlers[key].length; i++) { | ||
//找到处理内容 | ||
eventHandler(event, _handlers[key][i], scope); | ||
} | ||
} | ||
function eventHandler(event, handler, scope) { | ||
var modifiersMatch; | ||
//看它是否在当前范围 | ||
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.method(event, handler) === false) { | ||
if (event.preventDefault) event.preventDefault(); else event.returnValue = false; | ||
if (event.stopPropagation) event.stopPropagation(); | ||
if (event.cancelBubble) event.cancelBubble = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//解除绑定某个范围的快捷键 | ||
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); | ||
//获取除修饰键外的键值key | ||
key = keys[keys.length - 1]; | ||
key = code(key); | ||
//判断是否传入范围,没有就获取范围 | ||
if(scope === undefined) scope = getScope(); | ||
//如何key不在 _handlers 中返回不做处理 | ||
if (!_handlers[key]) return; | ||
//清空 handlers 中数据, | ||
//让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的 | ||
for (var r = 0; r < _handlers[key].length; r++) { | ||
obj = _handlers[key][r]; | ||
//判断是否在范围内并且键值相同 | ||
if (obj.scope === scope && compareArray(obj.mods, mods)) { | ||
_handlers[key][r] = {}; | ||
//解除绑定某个范围的快捷键 | ||
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); | ||
//获取除修饰键外的键值key | ||
key = keys[keys.length - 1]; | ||
key = code(key); | ||
//判断是否传入范围,没有就获取范围 | ||
if (scope === undefined) scope = getScope(); | ||
//如何key不在 _handlers 中返回不做处理 | ||
if (!_handlers[key]) return; | ||
//清空 handlers 中数据, | ||
//让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的 | ||
for (var r = 0; r < _handlers[key].length; r++) { | ||
obj = _handlers[key][r]; | ||
//判断是否在范围内并且键值相同 | ||
if (obj.scope === scope && compareArray(obj.mods, mods)) { | ||
_handlers[key][r] = {}; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//循环删除handlers中的所有 scope(范围) | ||
function deleteScope(scope){ | ||
var key, handlers, i; | ||
for (key in _handlers) { | ||
handlers = _handlers[key]; | ||
for (i = 0; i < handlers.length; ) { | ||
if (handlers[i].scope === scope) handlers.splice(i, 1); | ||
else i++; | ||
//循环删除handlers中的所有 scope(范围) | ||
function deleteScope(scope) { | ||
var key, handlers, i; | ||
for (key in _handlers) { | ||
handlers = _handlers[key]; | ||
for (i = 0; i < handlers.length; ) { | ||
if (handlers[i].scope === scope) handlers.splice(i, 1); else i++; | ||
} | ||
} | ||
} | ||
} | ||
//比较修饰键的数组 | ||
function compareArray(a1, a2) { | ||
if (a1.length !== a2.length) return false; | ||
for (var i = 0; i < a1.length; i++) { | ||
if (a1[i] !== a2[i]) return false; | ||
//比较修饰键的数组 | ||
function compareArray(a1, a2) { | ||
if (a1.length !== a2.length) return false; | ||
for (var i = 0; i < a1.length; i++) { | ||
if (a1[i] !== a2[i]) return false; | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
//表单控件控件判断 返回 Boolean | ||
function filter(event){ | ||
var tagName = (event.target || event.srcElement).tagName; | ||
//忽略这些标签情况下快捷键无效 | ||
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA'); | ||
} | ||
//修饰键转换成对应的键码 | ||
function getMods (key) { | ||
var mods = key.slice(0, key.length - 1); | ||
for (var i = 0; i < mods.length; i++) mods[i] = _modifier[mods[i]]; | ||
return mods; | ||
} | ||
//处理传的key字符串转换成数组 | ||
function getKeys(key) { | ||
var keys; | ||
key = key.replace(/\s/g, '');//匹配任何空白字符,包括空格、制表符、换页符等等 | ||
keys = key.split(','); | ||
if ((keys[keys.length - 1]) === '') keys[keys.length - 2] += ','; | ||
return keys; | ||
} | ||
//在全局document上设置快捷键 | ||
addEvent(document, 'keydown', function(event) { | ||
dispatch(event); | ||
}); | ||
addEvent(document, 'keyup',function(event){ | ||
clearModifier(event); | ||
}); | ||
//清除修饰键 | ||
function clearModifier(event){ | ||
var key = event.keyCode, | ||
i = _downKeys.indexOf(key); | ||
if(i>=0) _downKeys.splice(i,1); | ||
//修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
if(key === 93 || key === 224) key = 91; | ||
if(key in _mods) { | ||
_mods[key] = false; | ||
for(var k in _modifier) if(_modifier[k] === key) hotkeys[k] = false; | ||
//表单控件控件判断 返回 Boolean | ||
function filter(event) { | ||
var tagName = (event.target || event.srcElement).tagName; | ||
//忽略这些标签情况下快捷键无效 | ||
return !(tagName === "INPUT" || tagName === "SELECT" || tagName === "TEXTAREA"); | ||
} | ||
} | ||
//主体hotkeys函数 | ||
function hotkeys(key,scope,method){ | ||
var keys = getKeys(key), mods=[],i=0; | ||
//对为设定范围的判断 | ||
if (method === undefined) { | ||
method = scope; | ||
scope = 'all'; | ||
//修饰键转换成对应的键码 | ||
function getMods(key) { | ||
var mods = key.slice(0, key.length - 1); | ||
for (var i = 0; i < mods.length; i++) mods[i] = _modifier[mods[i]]; | ||
return mods; | ||
} | ||
//对于每个快捷键处理 | ||
for(;i < keys.length; i++){ | ||
key = keys[i].split('+'); | ||
mods = []; | ||
//如果是组合快捷键取得组合快捷键 | ||
if (key.length > 1){ | ||
mods = getMods(key); | ||
key = [key[key.length-1]]; | ||
//处理传的key字符串转换成数组 | ||
function getKeys(key) { | ||
var keys; | ||
key = key.replace(/\s/g, ""); | ||
//匹配任何空白字符,包括空格、制表符、换页符等等 | ||
keys = key.split(","); | ||
if (keys[keys.length - 1] === "") keys[keys.length - 2] += ","; | ||
return keys; | ||
} | ||
//在全局document上设置快捷键 | ||
addEvent(document, "keydown", function(event) { | ||
dispatch(event); | ||
}); | ||
addEvent(document, "keyup", function(event) { | ||
clearModifier(event); | ||
}); | ||
//清除修饰键 | ||
function clearModifier(event) { | ||
var key = event.keyCode, i = _downKeys.indexOf(key); | ||
if (i >= 0) _downKeys.splice(i, 1); | ||
//修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key in _mods) { | ||
_mods[key] = false; | ||
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
//转换成键码 | ||
key = key[0]; | ||
key = key === '*' ? '*' : code(key); | ||
//判断key是否在_handlers中,不在就赋一个空数组 | ||
if (!(key in _handlers)) _handlers[key] = []; | ||
_handlers[key].push({shortcut: keys[i], scope: scope, method: method, key: keys[i], mods: mods}); | ||
} | ||
} | ||
_api = { | ||
setScope:setScope, | ||
getScope:getScope, | ||
deleteScope:deleteScope, | ||
getPressedKeyCodes:getPressedKeyCodes, | ||
isPressed:isPressed, | ||
filter:filter, | ||
unbind:unbind | ||
}; | ||
for (var a in _api) hotkeys[a] = _api[a]; | ||
var _hotkeys = window.hotkeys | ||
hotkeys.noConflict = function( deep ) { | ||
if ( deep && window.hotkeys === hotkeys ) { | ||
window.hotkeys = _hotkeys; | ||
//主体hotkeys函数 | ||
function hotkeys(key, scope, method) { | ||
var keys = getKeys(key), mods = [], i = 0; | ||
//对为设定范围的判断 | ||
if (method === undefined) { | ||
method = scope; | ||
scope = "all"; | ||
} | ||
//对于每个快捷键处理 | ||
for (;i < keys.length; i++) { | ||
key = keys[i].split("+"); | ||
mods = []; | ||
//如果是组合快捷键取得组合快捷键 | ||
if (key.length > 1) { | ||
mods = getMods(key); | ||
key = [ key[key.length - 1] ]; | ||
} | ||
//转换成键码 | ||
key = key[0]; | ||
key = key === "*" ? "*" : code(key); | ||
//判断key是否在_handlers中,不在就赋一个空数组 | ||
if (!(key in _handlers)) _handlers[key] = []; | ||
_handlers[key].push({ | ||
shortcut: keys[i], | ||
scope: scope, | ||
method: method, | ||
key: keys[i], | ||
mods: mods | ||
}); | ||
} | ||
} | ||
_api = { | ||
setScope: setScope, | ||
getScope: getScope, | ||
deleteScope: deleteScope, | ||
getPressedKeyCodes: getPressedKeyCodes, | ||
isPressed: isPressed, | ||
filter: filter, | ||
unbind: unbind | ||
}; | ||
for (var a in _api) hotkeys[a] = _api[a]; | ||
var _hotkeys = window.hotkeys; | ||
hotkeys.noConflict = function(deep) { | ||
if (deep && window.hotkeys === hotkeys) { | ||
window.hotkeys = _hotkeys; | ||
} | ||
return hotkeys; | ||
}; | ||
window.hotkeys = hotkeys; | ||
return hotkeys; | ||
}; | ||
window.hotkeys = hotkeys; | ||
return hotkeys; | ||
})); | ||
}); |
@@ -1,1 +0,3 @@ | ||
/*! hotkeys-js v1.0.8 | (c) 2016 | https://github.com/jaywcjlove/hotkeys.git */!function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():e.hotkeys=n()}(this,function(){function e(e){m=e||"all"}function n(){return m||"all"}function t(e,n,t){e.addEventListener?e.addEventListener(n,t,!1):e.attachEvent&&e.attachEvent("on"+n,function(){t(window.event)})}function o(e){return"string"==typeof e&&(e=b(e)),k.indexOf(e)!==-1}function r(e){return k.slice(0)}function i(e){var t,o=e.keyCode,r=C["*"];if(k.indexOf(o)===-1&&k.push(o),93!==o&&224!==o||(o=91),o in E){E[o]=!0;for(var i in w)w[i]===o&&(h[i]=!0);if(!r)return}for(var a in E)E[a]=e[x[a]];if(h.filter.call(this,e)){if(t=n(),r)for(c=0;c<r.length;c++)r[c].scope===t&&f(e,r[c],t);if(o in C)for(var c=0;c<C[o].length;c++)f(e,C[o][c],t)}}function f(e,n,t){var o;if(n.scope===t||"all"===n.scope){o=n.mods.length>0;for(var r in E)(!E[r]&&n.mods.indexOf(+r)>-1||E[r]&&n.mods.indexOf(+r)===-1)&&(o=!1);(0!==n.mods.length||E[16]||E[18]||E[17]||E[91])&&!o&&"*"!==n.shortcut||n.method(e,n)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function a(e,t){for(var o,r,i=d(e),f=[],a=0;a<i.length;a++){if(o=i[a].split("+"),o.length>1&&(f=s(o)),e=o[o.length-1],e=b(e),void 0===t&&(t=n()),!C[e])return;for(var c=0;c<C[e].length;c++)r=C[e][c],r.scope===t&&l(r.mods,f)&&(C[e][c]={})}}function c(e){var n,t,o;for(n in C)for(t=C[n],o=0;o<t.length;)t[o].scope===e?t.splice(o,1):o++}function l(e,n){if(e.length!==n.length)return!1;for(var t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}function u(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)}function s(e){for(var n=e.slice(0,e.length-1),t=0;t<n.length;t++)n[t]=w[n[t]];return n}function d(e){var n;return e=e.replace(/\s/g,""),n=e.split(","),""===n[n.length-1]&&(n[n.length-2]+=","),n}function p(e){var n=e.keyCode,t=k.indexOf(n);if(t>=0&&k.splice(t,1),93!==n&&224!==n||(n=91),n in E){E[n]=!1;for(var o in w)w[o]===n&&(h[o]=!1)}}function h(e,n,t){var o=d(e),r=[],i=0;for(void 0===t&&(t=n,n="all");i<o.length;i++)e=o[i].split("+"),r=[],e.length>1&&(r=s(e),e=[e[e.length-1]]),e=e[0],e="*"===e?"*":b(e),e in C||(C[e]=[]),C[e].push({shortcut:o[i],scope:n,method:t,key:o[i],mods:r})}Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;n++)if(this[n]===e)return n;return-1});for(var g,v=navigator.userAgent.toLowerCase().indexOf("firefox")>0,y={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,",":188,".":190,"/":191,"`":192,"-":v?173:189,"=":v?61:187,";":v?59:186,"'":222,"[":219,"]":221,"\\":220},m="all",w={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":v?224:91,command:v?224:91},k=[],x={16:"shiftKey",18:"altKey",17:"ctrlKey"},E={16:!1,18:!1,17:!1},b=function(e){return y[e]||e.toUpperCase().charCodeAt(0)},C={},O=1;O<20;O++)y["f"+O]=111+O;x[v?224:91]="metaKey",E[v?224:91]=!1,t(document,"keydown",function(e){i(e)}),t(document,"keyup",function(e){p(e)}),g={setScope:e,getScope:n,deleteScope:c,getPressedKeyCodes:r,isPressed:o,filter:u,unbind:a};for(var A in g)h[A]=g[A];var K=window.hotkeys;return h.noConflict=function(e){return e&&window.hotkeys===h&&(window.hotkeys=K),h},window.hotkeys=h,h}); | ||
/*! hotkeys-js v1.0.10 | (c) 2016 | 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{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.hotkeys=e()}}(function(){function e(e){m=e||"all"}function n(){return m||"all"}function t(e,n,t){e.addEventListener?e.addEventListener(n,t,!1):e.attachEvent&&e.attachEvent("on"+n,function(){t(window.event)})}function o(e){return"string"==typeof e&&(e=E(e)),k.indexOf(e)!==-1}function r(e){return k.slice(0)}function i(e){var t,o=e.keyCode,r=C["*"];if(k.indexOf(o)===-1&&k.push(o),93!==o&&224!==o||(o=91),o in b){b[o]=!0;for(var i in w)w[i]===o&&(h[i]=!0);if(!r)return}for(var l in b)b[l]=e[x[l]];if(h.filter.call(this,e)){if(t=n(),r)for(a=0;a<r.length;a++)r[a].scope===t&&f(e,r[a],t);if(o in C)for(var a=0;a<C[o].length;a++)f(e,C[o][a],t)}}function f(e,n,t){var o;if(n.scope===t||"all"===n.scope){o=n.mods.length>0;for(var r in b)(!b[r]&&n.mods.indexOf(+r)>-1||b[r]&&n.mods.indexOf(+r)===-1)&&(o=!1);(0!==n.mods.length||b[16]||b[18]||b[17]||b[91])&&!o&&"*"!==n.shortcut||n.method(e,n)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function l(e,t){for(var o,r,i=d(e),f=[],l=0;l<i.length;l++){if(o=i[l].split("+"),o.length>1&&(f=s(o)),e=o[o.length-1],e=E(e),void 0===t&&(t=n()),!C[e])return;for(var a=0;a<C[e].length;a++)r=C[e][a],r.scope===t&&u(r.mods,f)&&(C[e][a]={})}}function a(e){var n,t,o;for(n in C)for(t=C[n],o=0;o<t.length;)t[o].scope===e?t.splice(o,1):o++}function u(e,n){if(e.length!==n.length)return!1;for(var t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}function c(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)}function s(e){for(var n=e.slice(0,e.length-1),t=0;t<n.length;t++)n[t]=w[n[t]];return n}function d(e){var n;return e=e.replace(/\s/g,""),n=e.split(","),""===n[n.length-1]&&(n[n.length-2]+=","),n}function p(e){var n=e.keyCode,t=k.indexOf(n);if(t>=0&&k.splice(t,1),93!==n&&224!==n||(n=91),n in b){b[n]=!1;for(var o in w)w[o]===n&&(h[o]=!1)}}function h(e,n,t){var o=d(e),r=[],i=0;for(void 0===t&&(t=n,n="all");i<o.length;i++)e=o[i].split("+"),r=[],e.length>1&&(r=s(e),e=[e[e.length-1]]),e=e[0],e="*"===e?"*":E(e),e in C||(C[e]=[]),C[e].push({shortcut:o[i],scope:n,method:t,key:o[i],mods:r})}Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(var n=0;n<this.length;n++)if(this[n]===e)return n;return-1});for(var g,v=navigator.userAgent.toLowerCase().indexOf("firefox")>0,y={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,",":188,".":190,"/":191,"`":192,"-":v?173:189,"=":v?61:187,";":v?59:186,"'":222,"[":219,"]":221,"\\":220},m="all",w={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":v?224:91,command:v?224:91},k=[],x={16:"shiftKey",18:"altKey",17:"ctrlKey"},b={16:!1,18:!1,17:!1},E=function(e){return y[e]||e.toUpperCase().charCodeAt(0)},C={},O=1;O<20;O++)y["f"+O]=111+O;x[v?224:91]="metaKey",b[v?224:91]=!1,t(document,"keydown",function(e){i(e)}),t(document,"keyup",function(e){p(e)}),g={setScope:e,getScope:n,deleteScope:a,getPressedKeyCodes:r,isPressed:o,filter:c,unbind:l};for(var A in g)h[A]=g[A];var K=window.hotkeys;return h.noConflict=function(e){return e&&window.hotkeys===h&&(window.hotkeys=K),h},window.hotkeys=h,h}); | ||
{ | ||
"name": "hotkeys-js", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.", | ||
"main": "dist/hotkeys.js", | ||
"main": "dist/hotkeys.min.js", | ||
"scripts": { | ||
"build:min": "umd hotkeys src/hotkeys.js | uglifyjs -mc | bannerjs -o > dist/hotkeys.min.js", | ||
"build:dist": "umd hotkeys src/hotkeys.js | bannerjs -m | uglifyjs -b beautify=true --comments 'all' > dist/hotkeys.js ", | ||
"build": "npm run build:min && npm run build:dist", | ||
"watch": "onchange 'src/hotkeys.js' -v -- npm run build " | ||
}, | ||
"keywords": [ | ||
@@ -21,12 +27,11 @@ "hotkey", | ||
"devDependencies": { | ||
"bannerjs": "^1.0.4", | ||
"gulp": "^3.9.0", | ||
"gulp-banner": "^0.1.3", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-sourcemap": "^1.0.1", | ||
"gulp-uglify": "^1.5.1", | ||
"gulp-umd": "^0.2.0", | ||
"gulp-util": "^3.0.7", | ||
"gulp-watch": "^4.3.5" | ||
"bannerjs": "^1.0.5", | ||
"browserify": "^13.1.1", | ||
"onchange": "^3.2.0", | ||
"ssr": "^1.1.1", | ||
"tape": "^4.6.3", | ||
"tape-dom": "0.0.12", | ||
"uglify-js": "^2.7.5", | ||
"umd": "^3.0.1" | ||
} | ||
} |
@@ -37,2 +37,16 @@ # 设置快捷键 | ||
## 使用 | ||
```js | ||
import hotkeys from 'hotkeys-js'; | ||
hotkeys('shift+a,alt+d, w', function(e){ | ||
console.log('干点活儿',e); | ||
if(hotkeys.shift) console.log('大哥你摁下了 shift 键!'); | ||
if(hotkeys.ctrl) console.log('大哥你摁下了 ctrl 键!'); | ||
if(hotkeys.alt) console.log('大哥你摁下了 alt 键!'); | ||
}); | ||
``` | ||
## 定义快捷键 | ||
@@ -39,0 +53,0 @@ |
@@ -268,2 +268,2 @@ //IE对indexOf方法的支持 | ||
window.hotkeys = hotkeys; | ||
return hotkeys; |
37742
8
599
211
9