From 1a967e8ce7f1503f3807c52623689965e11a2de2 Mon Sep 17 00:00:00 2001 From: Anson Kao Date: Sat, 4 Oct 2014 17:42:01 -0400 Subject: [PATCH 1/2] Single modifier shortcuts now possible! --- keymaster.js | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/keymaster.js b/keymaster.js index 8f5b5fc..8e9ce4d 100644 --- a/keymaster.js +++ b/keymaster.js @@ -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); @@ -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 }); @@ -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(); From 99ed6b7629bd76af6628c9faf901fef3e9e43957 Mon Sep 17 00:00:00 2001 From: Anson Kao Date: Sat, 4 Oct 2014 18:39:08 -0400 Subject: [PATCH 2/2] Test cases for single modifier shortcuts --- test/keymaster.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/keymaster.html b/test/keymaster.html index d329317..d0466c4 100644 --- a/test/keymaster.html +++ b/test/keymaster.html @@ -36,6 +36,8 @@

Keymaster unit tests

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); @@ -127,6 +129,48 @@

Keymaster unit tests

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++ });