Skip to content
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
40 changes: 29 additions & 11 deletions keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
_mods[key] = true;
// 'assignKey' from inside this closure is exported to window.key
for(k in _MODIFIERS) if(_MODIFIERS[k] == key) assignKey[k] = true;
return;
if(!(key in _handlers)) // allow single modifier shortcuts to execute
return; // for combinations, return and wait for the second keypress
}
updateModifierKey(event);

Expand Down Expand Up @@ -150,13 +151,22 @@
// set modifier keys if any
mods = [];
key = keys[i].split('+');
if (key.length > 1){
mods = getMods(key);
key = [key[key.length-1]];

// account for the case where shortcut is a single modifier
if (key in _MODIFIERS){
key = _MODIFIERS[key];
mods = [key];
}
// all other cases
else{
if (key.length > 1){
mods = getMods(key);
key = [key[key.length-1]];
}
// convert to keycode and...
key = key[0]
key = code(key);
}
// convert to keycode and...
key = key[0]
key = code(key);
// ...store handler
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({ shortcut: keys[i], scope: scope, method: method, key: keys[i], mods: mods });
Expand All @@ -174,12 +184,20 @@
for (j = 0; j < multipleKeys.length; j++) {
keys = multipleKeys[j].split('+');

if (keys.length > 1) {
mods = getMods(keys);
// account for the case where shortcut is a single modifier
if (key in _MODIFIERS){
key = _MODIFIERS[key];
mods = [key];
}
// all other cases
else {
if (keys.length > 1) {
mods = getMods(keys);
}

key = keys[keys.length - 1];
key = code(key);
key = keys[keys.length - 1];
key = code(key);
}

if (scope === undefined) {
scope = getScope();
Expand Down
44 changes: 44 additions & 0 deletions test/keymaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ <h1>Keymaster unit tests</h1>
var event = document.createEvent('Event');
event.initEvent('keydown', true, true);
event.keyCode = code;
if(code in modifierMap)
modifiers = [code];
if (modifiers && modifiers.length > 0)
for(i in modifiers) event[modifierMap[modifiers[i]]] = true;
(el||document).dispatchEvent(event);
Expand Down Expand Up @@ -127,6 +129,48 @@ <h1>Keymaster unit tests</h1>
t.assertEqual(1, cntCommandCtrlAltShiftA);
},

testModifierOnlyShortcuts: function(t){
var ctrl = 0, shift = 0, shiftA = 0;
key('ctrl', function(){ ctrl++ });
key('shift', function(){ shift++ });
key('shift+a', function(){ shiftA++ });

keydown(KEYS.ctrl); keyup(KEYS.ctrl);
t.assertEqual(1, ctrl);
t.assertEqual(0, shift);
t.assertEqual(0, shiftA);

keydown(KEYS.shift);
t.assertEqual(1, ctrl);
t.assertEqual(1, shift);
t.assertEqual(0, shiftA);
keydown(65,[KEYS.shift]); keyup(65);
t.assertEqual(1, ctrl);
t.assertEqual(1, shift);
t.assertEqual(1, shiftA);
keydown(65,[KEYS.shift]); keyup(65);
t.assertEqual(1, ctrl);
t.assertEqual(1, shift);
t.assertEqual(2, shiftA);
keyup(KEYS.shift);
t.assertEqual(1, ctrl);
t.assertEqual(1, shift);
t.assertEqual(2, shiftA);

key.unbind('ctrl','all');
key.unbind('shift','all');
key.unbind('shift+a','all');

keydown(KEYS.ctrl); keyup(KEYS.ctrl);
t.assertEqual(1, ctrl);
keydown(KEYS.shift);
t.assertEqual(1, shift);
keydown(65,[KEYS.shift]); keyup(65);
t.assertEqual(2, shiftA);
keyup(KEYS.shift);
t.assertEqual(1, shift);
},

testUnbindWithModifiers: function(t){
var cntA = 0, cntShiftA = 0, cntCtrlShiftA = 0, cntCommandCtrlShiftA = 0, cntCommandCtrlAltShiftA = 0;
key('a', function(){ cntA++ });
Expand Down