Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where newly added handlers were called immediately #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

// handle keydown event
function dispatch(event) {
var key, handler, k, i, modifiersMatch, scope;
var key, handler, k, i, modifiersMatch, scope, handlers;
key = event.keyCode;

if (index(_downKeys, key) == -1) {
Expand All @@ -89,10 +89,12 @@
if (!(key in _handlers)) return;

scope = getScope();
// clone handlers array to ignore modifications
handlers = _handlers[key].slice(0);

// for each potential shortcut
for (i = 0; i < _handlers[key].length; i++) {
handler = _handlers[key][i];
for (i = 0; i < handlers.length; i++) {
handler = handlers[i];

// see if it's in the current scope
if(handler.scope == scope || handler.scope == 'all'){
Expand Down
12 changes: 12 additions & 0 deletions test/keymaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ <h1>Keymaster unit tests</h1>

key.filter = oldFilter;
key.setScope();
},

testNewlyAddedHandlers: function(t) {
var keyCalls = 0, k = key;
k('a', function() {
keyCalls++;
k('a', function() { keyCalls++; });
});

keydown(65);
keyup(65);
t.assertEqual(1, keyCalls);
}
});
</script>
Expand Down