Skip to content

Commit

Permalink
fix: Make it so unbind('') does not remove all event handlers (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
srmagura authored May 20, 2022
1 parent 845b005 commit 4aab68f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
19 changes: 11 additions & 8 deletions dist/hotkeys.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**!
* hotkeys-js v3.9.0
* hotkeys-js v3.9.3
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
*
* Copyright (c) 2022 kenny wong <wowohoo@qq.com>
Expand All @@ -11,9 +11,9 @@

var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件

function addEvent(object, event, method) {
function addEvent(object, event, method, useCapture) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
object.addEventListener(event, method, useCapture);
} else if (object.attachEvent) {
object.attachEvent("on".concat(event), function () {
method(window.event);
Expand Down Expand Up @@ -256,7 +256,7 @@ function clearModifier(event) {

function unbind(keysInfo) {
// unbind(), unbind all keys
if (!keysInfo) {
if (typeof keysInfo === 'undefined') {
Object.keys(_handlers).forEach(function (key) {
return delete _handlers[key];
});
Expand Down Expand Up @@ -480,7 +480,8 @@ function hotkeys(key, option, method) {
var i = 0;
var keyup = false;
var keydown = true;
var splitKey = '+'; // 对为设定范围的判断
var splitKey = '+';
var capture = false; // 对为设定范围的判断

if (method === undefined && typeof option === 'function') {
method = option;
Expand All @@ -495,6 +496,8 @@ function hotkeys(key, option, method) {

if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line

if (option.capture !== undefined) capture = option.capture; // eslint-disable-line

if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
}

Expand Down Expand Up @@ -531,19 +534,19 @@ function hotkeys(key, option, method) {
elementHasBindEvent.push(element);
addEvent(element, 'keydown', function (e) {
dispatch(e, element);
});
}, capture);

if (!winListendFocus) {
winListendFocus = true;
addEvent(window, 'focus', function () {
_downKeys = [];
});
}, capture);
}

addEvent(element, 'keyup', function (e) {
dispatch(e, element);
clearModifier(e);
});
}, capture);
}
}

Expand Down
4 changes: 2 additions & 2 deletions dist/hotkeys.common.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions dist/hotkeys.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**!
* hotkeys-js v3.9.0
* hotkeys-js v3.9.3
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
*
* Copyright (c) 2022 kenny wong <wowohoo@qq.com>
Expand All @@ -9,9 +9,9 @@

var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件

function addEvent(object, event, method) {
function addEvent(object, event, method, useCapture) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
object.addEventListener(event, method, useCapture);
} else if (object.attachEvent) {
object.attachEvent("on".concat(event), function () {
method(window.event);
Expand Down Expand Up @@ -254,7 +254,7 @@ function clearModifier(event) {

function unbind(keysInfo) {
// unbind(), unbind all keys
if (!keysInfo) {
if (typeof keysInfo === 'undefined') {
Object.keys(_handlers).forEach(function (key) {
return delete _handlers[key];
});
Expand Down Expand Up @@ -478,7 +478,8 @@ function hotkeys(key, option, method) {
var i = 0;
var keyup = false;
var keydown = true;
var splitKey = '+'; // 对为设定范围的判断
var splitKey = '+';
var capture = false; // 对为设定范围的判断

if (method === undefined && typeof option === 'function') {
method = option;
Expand All @@ -493,6 +494,8 @@ function hotkeys(key, option, method) {

if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line

if (option.capture !== undefined) capture = option.capture; // eslint-disable-line

if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
}

Expand Down Expand Up @@ -529,19 +532,19 @@ function hotkeys(key, option, method) {
elementHasBindEvent.push(element);
addEvent(element, 'keydown', function (e) {
dispatch(e, element);
});
}, capture);

if (!winListendFocus) {
winListendFocus = true;
addEvent(window, 'focus', function () {
_downKeys = [];
});
}, capture);
}

addEvent(element, 'keyup', function (e) {
dispatch(e, element);
clearModifier(e);
});
}, capture);
}
}

Expand Down
Loading

0 comments on commit 4aab68f

Please sign in to comment.