diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 1567c205..4f9e2f83 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -285,7 +285,10 @@ function clearModifier(event) { function unbind(keysInfo) { // unbind(), unbind all keys if (typeof keysInfo === 'undefined') { - Object.keys(_handlers).forEach(key => delete _handlers[key]); + Object.keys(_handlers).forEach(key => { + Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info)); + delete _handlers[key]; + }); removeKeyEvent(null); } else if (Array.isArray(keysInfo)) { // support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}]) diff --git a/dist/hotkeys.common.min.js b/dist/hotkeys.common.min.js index 9f7150e0..ce404681 100644 --- a/dist/hotkeys.common.min.js +++ b/dist/hotkeys.common.min.js @@ -1,2 +1,2 @@ /*! hotkeys-js v3.13.4 | MIT © 2024 kenny wong https://jaywcjlove.github.io/hotkeys-js */ -"use strict";const isff="undefined"!=typeof navigator&&0_keyMap[e.toLowerCase()]||_modifier[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),getKey=t=>Object.keys(_keyMap).find(e=>_keyMap[e]===t),getModifier=t=>Object.keys(_modifier).find(e=>_modifier[e]===t);function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function getPressedKeyString(){return _downKeys.map(e=>getKey(e)||getModifier(e)||String.fromCharCode(e))}function getAllKeyCodes(){const s=[];return Object.keys(_handlers).forEach(e=>{_handlers[e].forEach(e=>{var{key:e,scope:t,mods:n,shortcut:o}=e;s.push({scope:t,shortcut:o,mods:n,keys:e.split("+").map(e=>code(e))})})}),s}function filter(e){var e=e.target||e.srcElement,t=e["tagName"];let n=!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1;return n}function isPressed(e){return"string"==typeof e&&(e=code(e)),!!~_downKeys.indexOf(e)}function deleteScope(e,t){var n;let o;e=e||getScope();for(const s in _handlers)if(Object.prototype.hasOwnProperty.call(_handlers,s))for(n=_handlers[s],o=0;o{e=e.element;return removeKeyEvent(e)}):o++;getScope()===e&&setScope(t||"all")}function clearModifier(e){let t=e.keyCode||e.which||e.charCode;var n=_downKeys.indexOf(t);if(n<0||_downKeys.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&_downKeys.splice(0,_downKeys.length),(t=93!==t&&224!==t?t:91)in _mods){_mods[t]=!1;for(const o in _modifier)_modifier[o]===t&&(hotkeys[o]=!1)}}function unbind(n){if(void 0===n)Object.keys(_handlers).forEach(e=>delete _handlers[e]),removeKeyEvent(null);else if(Array.isArray(n))n.forEach(e=>{e.key&&eachUnbind(e)});else if("object"==typeof n)n.key&&eachUnbind(n);else if("string"==typeof n){for(var o=arguments.length,s=Array(1{let{key:t,scope:r,method:d,splitKey:i="+"}=e;getKeys(t).forEach(e=>{var e=e.split(i),t=e.length,n=e[t-1],n="*"===n?"*":code(n);if(_handlers[n]){r=r||getScope();const o=1{var t=(!d||e.method===d)&&e.scope===r&&compareArray(e.mods,o);return t&&s.push(e.element),!t}),s.forEach(e=>removeKeyEvent(e))}})};function eventHandler(t,n,o,e){if(n.element===e){let e;if(n.scope===o||"all"===n.scope){e=0{var t=modifierMap[e];n[e]&&!~_downKeys.indexOf(t)?_downKeys.push(t):!n[e]&&~_downKeys.indexOf(t)?_downKeys.splice(_downKeys.indexOf(t),1):"metaKey"!==e||!n[e]||3!==_downKeys.length||n.ctrlKey||n.shiftKey||n.altKey||(_downKeys=_downKeys.slice(_downKeys.indexOf(t)))}),e in _mods){_mods[e]=!0;for(const y in _modifier)_modifier[y]===e&&(hotkeys[y]=!0);if(!o)return}for(const p in _mods)Object.prototype.hasOwnProperty.call(_mods,p)&&(_mods[p]=n[modifierMap[p]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~_downKeys.indexOf(17)||_downKeys.push(17),~_downKeys.indexOf(18)||_downKeys.push(18),_mods[17]=!0,_mods[18]=!0);var s=getScope();if(o)for(let e=0;e{_downKeys=[]},winListendFocus={listener:t,capture:p},addEvent(window,"focus",t,p)))}function trigger(t){let n=1{_handlers[e].filter(e=>e.scope===n&&e.shortcut===t).forEach(e=>{e&&e.method&&e.method()})})}function removeKeyEvent(t){var e,n,o,s=Object.values(_handlers).flat();s.findIndex(e=>{e=e.element;return e===t})<0&&({keydownListener:o,keyupListenr:n,capture:e}=elementEventMap.get(t)||{},o)&&n&&(removeEvent(t,"keyup",n,e),removeEvent(t,"keydown",o,e),elementEventMap.delete(t)),0{var{keydownListener:t,keyupListenr:n,capture:o}=elementEventMap.get(e)||{};t&&n&&(removeEvent(e,"keyup",n,o),removeEvent(e,"keydown",t,o),elementEventMap.delete(e))}),elementEventMap.clear(),Object.keys(_handlers).forEach(e=>delete _handlers[e]),winListendFocus&&({listener:n,capture:o}=winListendFocus,removeEvent(window,"focus",n,o),winListendFocus=null))}const _api={getPressedKeyString:getPressedKeyString,setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,getAllKeyCodes:getAllKeyCodes,isPressed:isPressed,filter:filter,trigger:trigger,unbind:unbind,keyMap:_keyMap,modifier:_modifier,modifierMap:modifierMap};for(const J0 in _api)Object.prototype.hasOwnProperty.call(_api,J0)&&(hotkeys[J0]=_api[J0]);if("undefined"!=typeof window){const K0=window.hotkeys;hotkeys.noConflict=e=>(e&&window.hotkeys===hotkeys&&(window.hotkeys=K0),hotkeys),window.hotkeys=hotkeys}module.exports=hotkeys; \ No newline at end of file +"use strict";const isff="undefined"!=typeof navigator&&0_keyMap[e.toLowerCase()]||_modifier[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),getKey=t=>Object.keys(_keyMap).find(e=>_keyMap[e]===t),getModifier=t=>Object.keys(_modifier).find(e=>_modifier[e]===t);function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function getPressedKeyString(){return _downKeys.map(e=>getKey(e)||getModifier(e)||String.fromCharCode(e))}function getAllKeyCodes(){const s=[];return Object.keys(_handlers).forEach(e=>{_handlers[e].forEach(e=>{var{key:e,scope:t,mods:n,shortcut:o}=e;s.push({scope:t,shortcut:o,mods:n,keys:e.split("+").map(e=>code(e))})})}),s}function filter(e){var e=e.target||e.srcElement,t=e["tagName"];let n=!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1;return n}function isPressed(e){return"string"==typeof e&&(e=code(e)),!!~_downKeys.indexOf(e)}function deleteScope(e,t){var n;let o;e=e||getScope();for(const s in _handlers)if(Object.prototype.hasOwnProperty.call(_handlers,s))for(n=_handlers[s],o=0;o{e=e.element;return removeKeyEvent(e)}):o++;getScope()===e&&setScope(t||"all")}function clearModifier(e){let t=e.keyCode||e.which||e.charCode;var n=_downKeys.indexOf(t);if(n<0||_downKeys.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&_downKeys.splice(0,_downKeys.length),(t=93!==t&&224!==t?t:91)in _mods){_mods[t]=!1;for(const o in _modifier)_modifier[o]===t&&(hotkeys[o]=!1)}}function unbind(n){if(void 0===n)Object.keys(_handlers).forEach(e=>{Array.isArray(_handlers[e])&&_handlers[e].forEach(e=>eachUnbind(e)),delete _handlers[e]}),removeKeyEvent(null);else if(Array.isArray(n))n.forEach(e=>{e.key&&eachUnbind(e)});else if("object"==typeof n)n.key&&eachUnbind(n);else if("string"==typeof n){for(var o=arguments.length,s=Array(1{let{key:t,scope:r,method:d,splitKey:i="+"}=e;getKeys(t).forEach(e=>{var e=e.split(i),t=e.length,n=e[t-1],n="*"===n?"*":code(n);if(_handlers[n]){r=r||getScope();const o=1{var t=(!d||e.method===d)&&e.scope===r&&compareArray(e.mods,o);return t&&s.push(e.element),!t}),s.forEach(e=>removeKeyEvent(e))}})};function eventHandler(t,n,o,e){if(n.element===e){let e;if(n.scope===o||"all"===n.scope){e=0{var t=modifierMap[e];n[e]&&!~_downKeys.indexOf(t)?_downKeys.push(t):!n[e]&&~_downKeys.indexOf(t)?_downKeys.splice(_downKeys.indexOf(t),1):"metaKey"!==e||!n[e]||3!==_downKeys.length||n.ctrlKey||n.shiftKey||n.altKey||(_downKeys=_downKeys.slice(_downKeys.indexOf(t)))}),e in _mods){_mods[e]=!0;for(const y in _modifier)_modifier[y]===e&&(hotkeys[y]=!0);if(!o)return}for(const p in _mods)Object.prototype.hasOwnProperty.call(_mods,p)&&(_mods[p]=n[modifierMap[p]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~_downKeys.indexOf(17)||_downKeys.push(17),~_downKeys.indexOf(18)||_downKeys.push(18),_mods[17]=!0,_mods[18]=!0);var s=getScope();if(o)for(let e=0;e{_downKeys=[]},winListendFocus={listener:t,capture:p},addEvent(window,"focus",t,p)))}function trigger(t){let n=1{_handlers[e].filter(e=>e.scope===n&&e.shortcut===t).forEach(e=>{e&&e.method&&e.method()})})}function removeKeyEvent(t){var e,n,o,s=Object.values(_handlers).flat();s.findIndex(e=>{e=e.element;return e===t})<0&&({keydownListener:o,keyupListenr:n,capture:e}=elementEventMap.get(t)||{},o)&&n&&(removeEvent(t,"keyup",n,e),removeEvent(t,"keydown",o,e),elementEventMap.delete(t)),0{var{keydownListener:t,keyupListenr:n,capture:o}=elementEventMap.get(e)||{};t&&n&&(removeEvent(e,"keyup",n,o),removeEvent(e,"keydown",t,o),elementEventMap.delete(e))}),elementEventMap.clear(),Object.keys(_handlers).forEach(e=>delete _handlers[e]),winListendFocus&&({listener:n,capture:o}=winListendFocus,removeEvent(window,"focus",n,o),winListendFocus=null))}const _api={getPressedKeyString:getPressedKeyString,setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,getAllKeyCodes:getAllKeyCodes,isPressed:isPressed,filter:filter,trigger:trigger,unbind:unbind,keyMap:_keyMap,modifier:_modifier,modifierMap:modifierMap};for(const K0 in _api)Object.prototype.hasOwnProperty.call(_api,K0)&&(hotkeys[K0]=_api[K0]);if("undefined"!=typeof window){const L0=window.hotkeys;hotkeys.noConflict=e=>(e&&window.hotkeys===hotkeys&&(window.hotkeys=L0),hotkeys),window.hotkeys=hotkeys}module.exports=hotkeys; \ No newline at end of file diff --git a/dist/hotkeys.esm.js b/dist/hotkeys.esm.js index 50a0287f..872347c0 100644 --- a/dist/hotkeys.esm.js +++ b/dist/hotkeys.esm.js @@ -283,7 +283,10 @@ function clearModifier(event) { function unbind(keysInfo) { // unbind(), unbind all keys if (typeof keysInfo === 'undefined') { - Object.keys(_handlers).forEach(key => delete _handlers[key]); + Object.keys(_handlers).forEach(key => { + Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info)); + delete _handlers[key]; + }); removeKeyEvent(null); } else if (Array.isArray(keysInfo)) { // support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}]) diff --git a/dist/hotkeys.js b/dist/hotkeys.js index fcac8247..9cc4228a 100644 --- a/dist/hotkeys.js +++ b/dist/hotkeys.js @@ -289,7 +289,10 @@ function unbind(keysInfo) { // unbind(), unbind all keys if (typeof keysInfo === 'undefined') { - Object.keys(_handlers).forEach(key => delete _handlers[key]); + Object.keys(_handlers).forEach(key => { + Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info)); + delete _handlers[key]; + }); removeKeyEvent(null); } else if (Array.isArray(keysInfo)) { // support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}]) diff --git a/dist/hotkeys.min.js b/dist/hotkeys.min.js index 16e9dc33..defa033b 100644 --- a/dist/hotkeys.min.js +++ b/dist/hotkeys.min.js @@ -1,2 +1,2 @@ /*! hotkeys-js v3.13.4 | MIT © 2024 kenny wong https://jaywcjlove.github.io/hotkeys-js */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0o[e.toLowerCase()]||k[e.toLowerCase()]||e.toUpperCase().charCodeAt(0);function l(e){t=e||"all"}function E(){return t||"all"}function j(n){if(void 0===n)Object.keys(v).forEach(e=>delete v[e]),c(null);else if(Array.isArray(n))n.forEach(e=>{e.key&&s(e)});else if("object"==typeof n)n.key&&s(n);else if("string"==typeof n){for(var o=arguments.length,r=Array(1{let{key:t,scope:i,method:l,splitKey:s="+"}=e;m(t).forEach(e=>{var e=e.split(s),t=e.length,n=e[t-1],n="*"===n?"*":K(n);if(v[n]){i=i||E();const o=1{var t=(!l||e.method===l)&&e.scope===i&&function(e,t){var n=e.lengthc(e))}})};function x(t,n,o,e){if(n.element===e){let e;if(n.scope===o||"all"===n.scope){e=0{var t=u[e];n[e]&&!~w.indexOf(t)?w.push(t):!n[e]&&~w.indexOf(t)?w.splice(w.indexOf(t),1):"metaKey"!==e||!n[e]||3!==w.length||n.ctrlKey||n.shiftKey||n.altKey||(w=w.slice(w.indexOf(t)))}),e in g){g[e]=!0;for(const d in k)k[d]===e&&(L[d]=!0);if(!o)return}for(const p in g)Object.prototype.hasOwnProperty.call(g,p)&&(g[p]=n[u[p]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~w.indexOf(17)||w.push(17),~w.indexOf(18)||w.push(18),g[17]=!0,g[18]=!0);var r=E();if(o)for(let e=0;e{w=[]},O={listener:t,capture:p},y(window,"focus",t,p)))}function c(t){var e,n,o,r=Object.values(v).flat();r.findIndex(e=>{e=e.element;return e===t})<0&&({keydownListener:o,keyupListenr:n,capture:e}=b.get(t)||{},o)&&n&&(i(t,"keyup",n,e),i(t,"keydown",o,e),b.delete(t)),0{var{keydownListener:t,keyupListenr:n,capture:o}=b.get(e)||{};t&&n&&(i(e,"keyup",n,o),i(e,"keydown",t,o),b.delete(e))}),b.clear(),Object.keys(v).forEach(e=>delete v[e]),O&&({listener:n,capture:o}=O,i(window,"focus",n,o),O=null))}var n={getPressedKeyString:function(){return w.map(e=>{return n=e,Object.keys(o).find(e=>o[e]===n)||(t=e,Object.keys(k).find(e=>k[e]===t))||String.fromCharCode(e);var t,n})},setScope:l,getScope:E,deleteScope:function(e,t){var n;let o;e=e||E();for(const r in v)if(Object.prototype.hasOwnProperty.call(v,r))for(n=v[r],o=0;o{e=e.element;return c(e)}):o++;E()===e&&l(t||"all")},getPressedKeyCodes:function(){return w.slice(0)},getAllKeyCodes:function(){const r=[];return Object.keys(v).forEach(e=>{v[e].forEach(e=>{var{key:e,scope:t,mods:n,shortcut:o}=e;r.push({scope:t,shortcut:o,mods:n,keys:e.split("+").map(e=>K(e))})})}),r},isPressed:function(e){return"string"==typeof e&&(e=K(e)),!!~w.indexOf(e)},filter:function(e){var t=(e=e.target||e.srcElement)["tagName"];let n=!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1;return n},trigger:function(t){let n=1{v[e].filter(e=>e.scope===n&&e.shortcut===t).forEach(e=>{e&&e.method&&e.method()})})},unbind:j,keyMap:o,modifier:k,modifierMap:u};for(const r in n)Object.prototype.hasOwnProperty.call(n,r)&&(L[r]=n[r]);if("undefined"!=typeof window){const a=window.hotkeys;L.noConflict=e=>(e&&window.hotkeys===L&&(window.hotkeys=a),L),window.hotkeys=L}return L}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0o[e.toLowerCase()]||k[e.toLowerCase()]||e.toUpperCase().charCodeAt(0);function l(e){t=e||"all"}function K(){return t||"all"}function j(n){if(void 0===n)Object.keys(v).forEach(e=>{Array.isArray(v[e])&&v[e].forEach(e=>s(e)),delete v[e]}),c(null);else if(Array.isArray(n))n.forEach(e=>{e.key&&s(e)});else if("object"==typeof n)n.key&&s(n);else if("string"==typeof n){for(var o=arguments.length,r=Array(1{let{key:t,scope:i,method:l,splitKey:s="+"}=e;m(t).forEach(e=>{var e=e.split(s),t=e.length,n=e[t-1],n="*"===n?"*":E(n);if(v[n]){i=i||K();const o=1{var t=(!l||e.method===l)&&e.scope===i&&function(e,t){var n=e.lengthc(e))}})};function x(t,n,o,e){if(n.element===e){let e;if(n.scope===o||"all"===n.scope){e=0{var t=u[e];n[e]&&!~w.indexOf(t)?w.push(t):!n[e]&&~w.indexOf(t)?w.splice(w.indexOf(t),1):"metaKey"!==e||!n[e]||3!==w.length||n.ctrlKey||n.shiftKey||n.altKey||(w=w.slice(w.indexOf(t)))}),e in g){g[e]=!0;for(const d in k)k[d]===e&&(L[d]=!0);if(!o)return}for(const p in g)Object.prototype.hasOwnProperty.call(g,p)&&(g[p]=n[u[p]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~w.indexOf(17)||w.push(17),~w.indexOf(18)||w.push(18),g[17]=!0,g[18]=!0);var r=K();if(o)for(let e=0;e{w=[]},O={listener:t,capture:p},y(window,"focus",t,p)))}function c(t){var e,n,o,r=Object.values(v).flat();r.findIndex(e=>{e=e.element;return e===t})<0&&({keydownListener:o,keyupListenr:n,capture:e}=b.get(t)||{},o)&&n&&(i(t,"keyup",n,e),i(t,"keydown",o,e),b.delete(t)),0{var{keydownListener:t,keyupListenr:n,capture:o}=b.get(e)||{};t&&n&&(i(e,"keyup",n,o),i(e,"keydown",t,o),b.delete(e))}),b.clear(),Object.keys(v).forEach(e=>delete v[e]),O&&({listener:n,capture:o}=O,i(window,"focus",n,o),O=null))}var n={getPressedKeyString:function(){return w.map(e=>{return n=e,Object.keys(o).find(e=>o[e]===n)||(t=e,Object.keys(k).find(e=>k[e]===t))||String.fromCharCode(e);var t,n})},setScope:l,getScope:K,deleteScope:function(e,t){var n;let o;e=e||K();for(const r in v)if(Object.prototype.hasOwnProperty.call(v,r))for(n=v[r],o=0;o{e=e.element;return c(e)}):o++;K()===e&&l(t||"all")},getPressedKeyCodes:function(){return w.slice(0)},getAllKeyCodes:function(){const r=[];return Object.keys(v).forEach(e=>{v[e].forEach(e=>{var{key:e,scope:t,mods:n,shortcut:o}=e;r.push({scope:t,shortcut:o,mods:n,keys:e.split("+").map(e=>E(e))})})}),r},isPressed:function(e){return"string"==typeof e&&(e=E(e)),!!~w.indexOf(e)},filter:function(e){var t=(e=e.target||e.srcElement)["tagName"];let n=!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1;return n},trigger:function(t){let n=1{v[e].filter(e=>e.scope===n&&e.shortcut===t).forEach(e=>{e&&e.method&&e.method()})})},unbind:j,keyMap:o,modifier:k,modifierMap:u};for(const r in n)Object.prototype.hasOwnProperty.call(n,r)&&(L[r]=n[r]);if("undefined"!=typeof window){const a=window.hotkeys;L.noConflict=e=>(e&&window.hotkeys===L&&(window.hotkeys=a),L),window.hotkeys=L}return L}); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 80971e49..acd5fe89 100644 --- a/src/index.js +++ b/src/index.js @@ -123,7 +123,10 @@ function clearModifier(event) { function unbind(keysInfo, ...args) { // unbind(), unbind all keys if (typeof keysInfo === 'undefined') { - Object.keys(_handlers).forEach((key) => delete _handlers[key]); + Object.keys(_handlers).forEach((key) => { + Array.isArray(_handlers[key]) && _handlers[key].forEach((info) => eachUnbind(info)); + delete _handlers[key]; + }); removeKeyEvent(null); } else if (Array.isArray(keysInfo)) { // support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}])