diff --git a/README-zh.md b/README-zh.md index 01f3270..6853740 100644 --- a/README-zh.md +++ b/README-zh.md @@ -186,6 +186,8 @@ hotkeys('ctrl-y, ctrl-a', {splitKey: '-'}, function(e){ - `element` - `keyup` - `keydown` +- `splitKey` (默认值 `+`) +- `capture` ```js hotkeys('o, enter', { @@ -194,9 +196,70 @@ hotkeys('o, enter', { }, function(){ console.log('do something else'); }); + +hotkeys('ctrl-+', { splitKey: '-' }, function(e) { + console.log('you pressed ctrl and +'); +}); + +hotkeys('+', { splitKey: '-' }, function(e){ + console.log('you pressed +'); +}) +``` + +**keyup** + +**key down** 和 **key up** 将都执行回调事件。 + +```js +hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => { + if(evn.type === 'keydown') { + console.log('keydown:', evn.type, handler, handler.key); + } + if(evn.type === 'keyup') { + console.log('keyup:', evn.type, handler, handler.key); + } +}); +``` + +## API 参考 + +### 星号 * + +通过修饰符号判断 + +```js +hotkeys('*', function() { + if (hotkeys.shift) { + console.log('shift is pressed!'); + } + + if (hotkeys.ctrl) { + console.log('ctrl is pressed!'); + } + + if (hotkeys.alt) { + console.log('alt is pressed!'); + } + + if (hotkeys.option) { + console.log('option is pressed!'); + } + + if (hotkeys.control) { + console.log('control is pressed!'); + } + + if (hotkeys.cmd) { + console.log('cmd is pressed!'); + } + + if (hotkeys.command) { + console.log('command is pressed!'); + } +}); ``` -## 切换快捷键 +### 切换快捷键 如果在单页面在不同的区域,相同的快捷键,干不同的事儿,之间来回切换。O(∩_∩)O ! @@ -218,7 +281,7 @@ hotkeys('ctrl+o, enter', 'scope2', function(){ hotkeys.setScope('scope1'); // 默认所有事儿都干哦 ``` -## 标记快捷键范围 +### 标记快捷键范围 **删除** 区域范围标记 @@ -238,20 +301,19 @@ hotkeys.getScope(); hotkeys.setScope('scope1'); ``` -## trigger +### trigger ```js hotkeys.trigger('ctrl+o') hotkeys.trigger('ctrl+o', 'scope2') ``` -## 解除绑定 +### 解除绑定 `hotkeys.unbind()` 解除绑定的所有快捷键 `hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键 `hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files的区域范围中的一组快捷键 - ```js // 解除绑定 'a' 程序函数 hotkeys.unbind('a'); @@ -272,14 +334,20 @@ hotkeys.unbind('a', example); hotkeys('a', 'issues', example); hotkeys.unbind('a', 'issues', example); ``` -```js + 可以通过传入对象解除绑定的快捷键 + +```js hotkeys.unbind({ key: 'ctrl-e,ctrl-u', scope: 'issues', spitKey: '-' }) +``` + 传入数组可同时解除多个scope下绑定的快捷键 + +```js hotkeys.unbind([ { key: 'a, ctrl+r', @@ -293,7 +361,7 @@ hotkeys.unbind([ ]) ``` -## 键判断 +### isPressed 判断摁下的键是否为某个键 @@ -305,7 +373,7 @@ hotkeys('a', function(){ }); ``` -## 获取摁下键值 +### getPressedKeyCodes 获取摁下绑定键的键值 `hotkeys.getPressedKeyCodes()` @@ -315,22 +383,32 @@ hotkeys('command+ctrl+shift+a,f', function(){ }) ``` -## keyup +### getPressedKeyString -**key down** 和 **key up** 将都执行回调事件。 +获取所有注册代码的列表 ```js -hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => { - if(evn.type === 'keydown') { - console.log('keydown:', evn.type, handler, handler.key); - } - if(evn.type === 'keyup') { - console.log('keyup:', evn.type, handler, handler.key); - } -}); +hotkeys('command+ctrl+shift+a,f', function() { + console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] +}) +``` + + +### getAllKeyCodes + +Get a list of all registration codes. + +```js +hotkeys('command+ctrl+shift+a,f', function() { + console.log(hotkeys.getAllKeyCodes()); + // [ + // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] }, + // { scope: 'all', shortcut: 'f', mods: [], keys: [42] } + // ] +}) ``` -## 过滤 +### filter `INPUT` `SELECT` `TEXTAREA` 默认不处理。 `hotkeys.filter` 返回 `true` 快捷键设置才会起作用,`false` 快捷键设置失效。 diff --git a/README.md b/README.md index 63e7f65..a0e447b 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ hotkeys('*','wcj', function(event){ hotkeys('o, enter', { scope: 'wcj', element: document.getElementById('wrapper'), -}, function(){ +}, function() { console.log('do something else'); }); @@ -237,10 +237,10 @@ Use the `hotkeys.setScope` method to set scope. There can only be one active sco ```js // Define shortcuts with a scope -hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){ +hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() { console.log('do something'); }); -hotkeys('o, enter', 'files', function(){ +hotkeys('o, enter', 'files', function() { console.log('do something else'); }); @@ -315,6 +315,8 @@ hotkeys('a', function() { ### trigger +trigger shortcut key event + ```js hotkeys.trigger('ctrl+o'); hotkeys.trigger('ctrl+o', 'scope2'); @@ -325,22 +327,35 @@ hotkeys.trigger('ctrl+o', 'scope2'); Returns an array of key codes currently pressed. ```js -hotkeys('command+ctrl+shift+a,f', function(){ +hotkeys('command+ctrl+shift+a,f', function() { console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70] }) ``` - -### getPressedKeyStrings +### getPressedKeyString Returns an array of key codes currently pressed. ```js -hotkeys('command+ctrl+shift+a,f', function(){ +hotkeys('command+ctrl+shift+a,f', function() { console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] }) ``` +### getAllKeyCodes + +Get a list of all registration codes. + +```js +hotkeys('command+ctrl+shift+a,f', function() { + console.log(hotkeys.getAllKeyCodes()); + // [ + // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] }, + // { scope: 'all', shortcut: 'f', mods: [], keys: [42] } + // ] +}) +``` + ### filter By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements. `Hotkeys.filter` to return to the `true` shortcut keys set to play a role, `false` shortcut keys set up failure. diff --git a/index.d.ts b/index.d.ts index c5d1fb4..298839b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -37,14 +37,71 @@ export interface Hotkeys { modifier: Record; modifierMap: Record; - getAllKeyCodes(): Omit; - + /** + * Use the `hotkeys.setScope` method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active. + * + * ```js + * // Define shortcuts with a scope + * hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() { + * console.log('do something'); + * }); + * hotkeys('o, enter', 'files', function() { + * console.log('do something else'); + * }); + * + * // Set the scope (only 'all' and 'issues' shortcuts will be honored) + * hotkeys.setScope('issues'); // default scope is 'all' + * ``` + */ setScope(scopeName: string): void; + /** + * Use the `hotkeys.getScope` method to get scope. + * + * ```js + * hotkeys.getScope(); + * ``` + */ getScope(): string; + /** + * Use the `hotkeys.deleteScope` method to delete a scope. This will also remove all associated hotkeys with it. + * + * ```js + * hotkeys.deleteScope('issues'); + * ``` + * You can use second argument, if need set new scope after deleting. + * + * ```js + * hotkeys.deleteScope('issues', 'newScopeName'); + * ``` + */ deleteScope(scopeName: string, newScopeName?: string): void; + /** + * Relinquish HotKeys’s control of the `hotkeys` variable. + * + * ```js + * var k = hotkeys.noConflict(); + * k('a', function() { + * console.log("do something") + * }); + * + * hotkeys() + * // -->Uncaught TypeError: hotkeys is not a function(anonymous function) + * // @ VM2170:2InjectedScript._evaluateOn + * // @ VM2165:883InjectedScript._evaluateAndWrap + * // @ VM2165:816InjectedScript.evaluate @ VM2165:682 + * ``` + */ noConflict(): Hotkeys; + /** + * trigger shortcut key event + * + * ```js + * hotkeys.trigger('ctrl+o'); + * hotkeys.trigger('ctrl+o', 'scope2'); + * ``` + */ trigger(shortcut: string, scope?: string): void; unbind(key?: string): void; @@ -52,11 +109,70 @@ export interface Hotkeys { unbind(key: string, scopeName: string, method: KeyHandler): void; unbind(key: string, method: KeyHandler): void; + /** For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed. */ isPressed(keyCode: number): boolean; + /** For example, `hotkeys.isPressed('m')` is true if the `M` key is currently pressed. */ isPressed(keyCode: string): boolean; + /** + * Returns an array of key codes currently pressed. + * + * ```js + * hotkeys('command+ctrl+shift+a,f', function() { + * console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70] + * }) + * ``` + */ getPressedKeyCodes(): number[]; + /** + * Returns an array of key codes currently pressed. + * + * ```js + * hotkeys('command+ctrl+shift+a,f', function() { + * console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] + * }) + * ``` + */ getPressedKeyString(): string[]; + /** + * Get a list of all registration codes. + * + * ```js + * hotkeys('command+ctrl+shift+a,f', function() { + * console.log(hotkeys.getAllKeyCodes()); + * // [ + * // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] }, + * // { scope: 'all', shortcut: 'f', mods: [], keys: [42] } + * // ] + * }) + * ``` + * + */ + getAllKeyCodes(): Omit; + /** + * By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements. + * `Hotkeys.filter` to return to the `true` shortcut keys set to play a role, + * `false` shortcut keys set up failure. + * + * ```js + * hotkeys.filter = function(event){ + * return true; + * } + * //How to add the filter to edit labels.
+ * //"contentEditable" Older browsers that do not support drops + * hotkeys.filter = function(event) { + * var target = event.target || event.srcElement; + * var tagName = target.tagName; + * return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA'); + * } + * + * hotkeys.filter = function(event){ + * var tagName = (event.target || event.srcElement).tagName; + * hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other'); + * return true; + * } + * ``` + */ filter(event: KeyboardEvent): boolean; } // https://github.com/eiriklv/react-masonry-component/issues/57