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
16 changes: 10 additions & 6 deletions keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,8 @@

// see if it's in the current scope
if(handler.scope == scope || handler.scope == 'all'){
// check if modifiers match if any
modifiersMatch = handler.mods.length > 0;
for(k in _mods)
if((!_mods[k] && index(handler.mods, +k) > -1) ||
(_mods[k] && index(handler.mods, +k) == -1)) modifiersMatch = false;
// call the handler and stop the event if neccessary
if((handler.mods.length == 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) || modifiersMatch){
if (_downKeys.length == (handler.mods.length + 1) && checkKeys(handler.key)) {
if(handler.method(event, handler)===false){
if(event.preventDefault) event.preventDefault();
else event.returnValue = false;
Expand All @@ -114,6 +109,14 @@
}
};

function checkKeys(key) {
var keys = key.split('+');
for (var i = 0, l = keys.length; i < l; i++) {
if (index(_downKeys, _MODIFIERS[keys[i]] || code(keys[i])) == -1) return false;
}
return true;
}

// unset modifier keys on keyup
function clearModifier(event){
var key = event.keyCode, k,
Expand All @@ -134,6 +137,7 @@
function resetModifiers() {
for(k in _mods) _mods[k] = false;
for(k in _MODIFIERS) assignKey[k] = false;
_downKeys = [];
};

// parse and assign shortcut
Expand Down
49 changes: 49 additions & 0 deletions test/keymaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,55 @@ <h1>Keymaster unit tests</h1>
t.assertEqual(1, cnt);
},

testShortcutWithMultipleKeysCombination: function (t){
var cntCtrlWP = 0, cntShiftWP = 0, cntCtrlShiftP = 0, cntCommandAltP = 0, cntCommandCtrlShiftWP = 0;
key('ctrl+w+p', function() { cntCtrlWP++ });
key('shift+w+p', function() { cntShiftWP++ });
key('ctrl+shift+p', function() { cntCtrlShiftP++ });
key('command+alt+p', function() { cntCommandAltP++ });
key('command+shift+w+p', function() { cntCommandCtrlShiftWP++ });

keydown(KEYS.ctrl); keydown(87); keydown(80);
t.assertEqual(1, cntCtrlWP);
t.assertEqual(0, cntShiftWP);
t.assertEqual(0, cntCtrlShiftP);
t.assertEqual(0, cntCommandAltP);
t.assertEqual(0, cntCommandCtrlShiftWP);
keyup(KEYS.ctrl); keyup(87); keyup(80);

keydown(KEYS.shift); keydown(87); keydown(80);
t.assertEqual(1, cntCtrlWP);
t.assertEqual(1, cntShiftWP);
t.assertEqual(0, cntCtrlShiftP);
t.assertEqual(0, cntCommandAltP);
t.assertEqual(0, cntCommandCtrlShiftWP);
keyup(KEYS.shift); keyup(87); keyup(80);

keydown(KEYS.ctrl); keydown(KEYS.shift); keydown(80);
t.assertEqual(1, cntCtrlWP);
t.assertEqual(1, cntShiftWP);
t.assertEqual(1, cntCtrlShiftP);
t.assertEqual(0, cntCommandAltP);
t.assertEqual(0, cntCommandCtrlShiftWP);
keyup(KEYS.ctrl); keyup(KEYS.shift); keyup(80);

keydown(KEYS.command); keydown(KEYS.alt); keydown(80);
t.assertEqual(1, cntCtrlWP);
t.assertEqual(1, cntShiftWP);
t.assertEqual(1, cntCtrlShiftP);
t.assertEqual(1, cntCommandAltP);
t.assertEqual(0, cntCommandCtrlShiftWP);
keyup(KEYS.command); keyup(KEYS.alt); keyup(80);

keydown(KEYS.command); keydown(KEYS.shift); keydown(87); keydown(80);
t.assertEqual(1, cntCtrlWP);
t.assertEqual(1, cntShiftWP);
t.assertEqual(1, cntCtrlShiftP);
t.assertEqual(1, cntCommandAltP);
t.assertEqual(1, cntCommandCtrlShiftWP);
keyup(KEYS.command); keyup(KEYS.shift); keyup(87); keyup(80);
},

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