Socket
Socket
Sign inDemoInstall

hotkeys-js

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hotkeys-js - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

2

bower.json
{
"name": "hotkeys-js",
"main": "src/hotkeys.js",
"version": "1.0.0",
"version": "1.0.1",
"homepage": "https://github.com/jaywcjlove/hotkeys",

@@ -6,0 +6,0 @@ "authors": [

@@ -1,1 +0,1 @@

(function(root,factory){var hotkeys=factory(root);if(typeof define==="function"&&define.amd){define("hotkeys",function(){return hotkeys})}else if(typeof exports==="object"){module.exports=hotkeys}else{var previousKey=root.hotkeys;hotkeys.noConflict=function(){var k=root.hotkeys;root.hotkeys=previousKey;return k};root.hotkeys=hotkeys}})(this,function(root,undefined){var _api,_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,"-":189,"=":187,";":186,"'":222,"[":219,"]":221,"\\":220},_scope="all",_modifier={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,command:91},modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey"},_mods={16:false,18:false,17:false,91:false},code=function(x){return _keyMap[x]||x.toUpperCase().charCodeAt(0)},_handlers={};for(k=1;k<20;k++){_keyMap["f"+k]=111+k}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 dispatch(event){var key=event.keyCode,modifiersMatch,scope,handler;if(key===93||key===224)key=91;if(key in _mods){_mods[key]=true;for(var k in _modifier)if(_modifier[k]===key)hotkeys[k]=true;return}for(var e in _mods)_mods[e]=event[modifierMap[e]];if(!(key in _handlers))return;scope=getScope();for(i=0;i<_handlers[key].length;i++){handler=_handlers[key][i];if(handler.scope===scope||handler.scope==="all"){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){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}}}}}addEvent(document,"keydown",function(event){dispatch(event)});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}function getKeys(key){var keys;key=key.replace(/\s/g,"");keys=key.split(",");if(keys[keys.length-1]==="")keys[keys.length-2]+=",";return keys}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=code(key);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};for(var a in _api)hotkeys[a]=_api[a];return hotkeys});
if(!Array.indexOf){Array.prototype.indexOf=function(a){for(var b=0;b<this.length;b++)if(this[b]===a)return b;return-1}}(function(a,b){var c=b(a);if(typeof define==="function"&&define.amd){define("hotkeys",function(){return c})}else if(typeof exports==="object"){module.exports=c}else{var d=a.hotkeys;c.noConflict=function(){var b=a.hotkeys;a.hotkeys=d;return b};a.hotkeys=c}})(this,function(a,b){var c,d={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,"-":189,"=":187,";":186,"'":222,"[":219,"]":221,"\\":220},e="all",f={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,command:91},g=[],h={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey"},j={16:false,18:false,17:false,91:false},l=function(a){return d[a]||a.toUpperCase().charCodeAt(0)},m={};for(k=1;k<20;k++){d["f"+k]=111+k}function n(a){e=a||"all"}function o(){return e||"all"}function p(a,b,c){if(a.addEventListener){a.addEventListener(b,c,false)}else if(a.attachEvent){a.attachEvent("on"+b,function(){c(window.event)})}}function q(a){if(typeof a==="string"){a=l(a)}return g.indexOf(a)!==-1}function r(a){return g.slice(0)}function s(a){var b=a.keyCode,c,d,e;if(g.indexOf(b)===-1)g.push(b);if(b===93||b===224)b=91;if(b in j){j[b]=true;for(var k in f)if(f[k]===b)y[k]=true;return}for(var l in j)j[l]=a[h[l]];if(!y.filter.call(this,a))return;if(!(b in m))return;d=o();for(i=0;i<m[b].length;i++){e=m[b][i];if(e.scope===d||e.scope==="all"){c=e.mods.length>0;for(var n in j){if(!j[n]&&e.mods.indexOf(+n)>-1||j[n]&&e.mods.indexOf(+n)===-1)c=false}if(e.mods.length===0&&!j[16]&&!j[18]&&!j[17]&&!j[91]||c){if(e.method(a,e)===false){if(a.preventDefault)a.preventDefault();else a.returnValue=false;if(a.stopPropagation)a.stopPropagation();if(a.cancelBubble)a.cancelBubble=true}}}}}function t(a,c){var d=x(a),e,f=[],g;for(var h=0;h<d.length;h++){e=d[h].split("+");if(e.length>1)f=w(e);a=e[e.length-1];a=l(a);if(c===b)c=o();if(!m[a])return;for(var i=0;i<m[a].length;i++){g=m[a][i];if(g.scope===c&&u(g.mods,f)){m[a][i]={}}}}}function u(a,b){if(a.length!==b.length)return false;for(var c=0;c<a.length;c++){if(a[c]!==b[c])return false}return true}function v(a){var b=(a.target||a.srcElement).tagName;return!(b==="INPUT"||b==="SELECT"||b==="TEXTAREA")}function w(a){var b=a.slice(0,a.length-1);for(var c=0;c<b.length;c++)b[c]=f[b[c]];return b}function x(a){var b;a=a.replace(/\s/g,"");b=a.split(",");if(b[b.length-1]==="")b[b.length-2]+=",";return b}p(document,"keydown",function(a){s(a)});p(document,"keyup",function(a){var b=a.keyCode,c=g.indexOf(b);if(c>=0)g.splice(c,1)});function y(a,c,d){var e=x(a),f=[],g=0;if(d===b){d=c;c="all"}for(;g<e.length;g++){a=e[g].split("+");f=[];if(a.length>1){f=w(a);a=[a[a.length-1]]}a=a[0];a=l(a);if(!(a in m))m[a]=[];m[a].push({shortcut:e[g],scope:c,method:d,key:e[g],mods:f})}}c={setScope:n,getScope:o,getPressedKeyCodes:r,isPressed:q,filter:v,unbind:t};for(var z in c)y[z]=c[z];return y});
{
"name": "hotkeys-js",
"version": "1.0.0",
"version": "1.0.1",
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -11,5 +11,9 @@ # 设置快捷键

Run `bower info hotkeysjs` to list the available versions.
安装 `bower install hotkeysjs`
下载 `bower install hotkeysjs`
### npm
下载 `npm install hotkeys-js`
## 定义快捷键

@@ -19,3 +23,10 @@

// 定义a快捷键
hotkeys('a', function(){ alert('你按下了 a!') });
hotkeys('a', function(event){
//event.srcElement: input
//event.target: input
if(event.target === "input"){
alert('你在输入框中按下了 a!')
}
alert('你按下了 a!')
});

@@ -27,3 +38,2 @@ // 返回false将停止活动,并阻止默认浏览器事件

hotkeys('⌘+r, ctrl+r', function(){ });
```

@@ -44,3 +54,3 @@

## 修时键判断
## 修饰键判断
可以对下面的修饰键判断 `shift` `alt` `option` `ctrl` `control` `command`

@@ -56,4 +66,8 @@

// 一个快捷键,有可能干的活儿不一样哦
hotkeys('o, enter', 'issues', function(){ /* 干点活儿 */ });
hotkeys('o, enter', 'files', function(){ /* 另一种活儿 */ });
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){
console.log('干点活儿');
});
hotkeys('o, enter', 'files', function(){
console.log('另一种活儿');
});

@@ -64,2 +78,38 @@ // 设定范围scope

## 解除绑定
`hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键
`hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files钟的一组快捷键
## 键判断
判断摁下的键是否为某个键
```js
hotkeys('a', function(){
console.log(hotkeys.isPressed("A")); //=> true
console.log(hotkeys.isPressed(65)); //=> true
});
```
## 获取摁下键值
获取摁下绑定键的键值
```js
hotkeys('command+ctrl+shift+a,f', function(){
console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] 或者 [70]
})
```
## 过滤
`INPUT` `SELECT` `TEXTAREA` 默认不处理。
`key.filter` 返回 `true` 快捷键设置才会起作用,`flase` 快捷键设置失效。
```javascript
key.filter = function(event){
return true;
}
```
## 兼容模式

@@ -66,0 +116,0 @@

@@ -0,1 +1,9 @@

//IE对indexOf方法的支持
if(!Array.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++) if(this[i]===obj) return i;
return -1;
};
}
;(function(root, factory) {

@@ -22,3 +30,3 @@ var hotkeys = factory(root);

var _api,//对外API
_keyMap = {//特殊键
_keyMap = {//特殊键
backspace: 8, tab: 9, clear: 12,

@@ -43,2 +51,3 @@ enter: 13, 'return': 13,

},
_downKeys=[],//记录摁下的绑定键
modifierMap = {

@@ -72,5 +81,18 @@ 16:'shiftKey',

}
//判断摁下的键是否为某个键,返回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,modifiersMatch,scope,handler;
//搜集绑定的键
if(_downKeys.indexOf(key)===-1) _downKeys.push(key);
//Gecko(Friefox)的command键值224,在Webkit(Chrome)中保持一致

@@ -87,2 +109,4 @@ //Webkit左右command键值不一样

for(var e in _mods) _mods[e] = event[modifierMap[e]];
//表单控件控件过滤 默认表单控件不触发快捷键
if(!hotkeys.filter.call(this,event)) return;
// key 不在_handlers中返回

@@ -114,5 +138,48 @@ if (!(key in _handlers)) return;

}
addEvent(document, 'keydown', function(event) {
dispatch(event);
});
//解除绑定某个范围的快捷键
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 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;
}
//表单控件控件判断 返回 Boolean
function filter(event){
var tagName = (event.target || event.srcElement).tagName;
//忽略这些标签情况下快捷键无效
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA');
}
//修饰键转换成对应的键码

@@ -132,2 +199,12 @@ function getMods (key) {

}
//在全局document上设置快捷键
addEvent(document, 'keydown', function(event) {
dispatch(event);
});
//清除修改
addEvent(document, 'keyup',function(event){
var key = event.keyCode,
i = _downKeys.indexOf(key);
if(i>=0) _downKeys.splice(i,1);
});
//主体hotkeys函数

@@ -160,3 +237,7 @@ function hotkeys(key,scope,method){

setScope:setScope,
getScope:getScope
getScope:getScope,
getPressedKeyCodes:getPressedKeyCodes,
isPressed:isPressed,
filter:filter,
unbind:unbind
};

@@ -163,0 +244,0 @@ for (var a in _api) hotkeys[a] = _api[a];

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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