Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'noConflict' of https://github.com/bleadof/keymaster int…

…o bleadof-noConflict

Conflicts:
	keymaster.js
	test/keymaster.html
  • Loading branch information...
commit 085087300a2730314a5cbffc98bbc91be10da535 2 parents f4fef75 + 8526991
@madrobby authored
Showing with 35 additions and 12 deletions.
  1. +19 −8 keymaster.js
  2. +16 −4 test/keymaster.html
View
27 keymaster.js
@@ -44,7 +44,7 @@
function dispatch(event, scope){
var key, handler, k, i, modifiersMatch;
key = event.keyCode;
-
+
if (index(_downKeys, key) == -1) {
_downKeys.push(key);
}
@@ -93,12 +93,12 @@
function clearModifier(event){
var key = event.keyCode, k,
i = index(_downKeys, key);
-
+
// remove key from _downKeys
if (i >= 0) {
_downKeys.splice(i, 1);
}
-
+
if(key == 93 || key == 224) key = 91;
if(key in _mods) {
_mods[key] = false;
@@ -142,20 +142,20 @@
_handlers[key].push({ shortcut: keys[i], scope: scope, method: method, key: keys[i], mods: mods });
}
};
-
+
// Returns true if the key with code 'keyCode' is currently down
// Converts strings into key codes.
function isPressed(keyCode) {
if (typeof(keyCode)=='string') {
if (keyCode.length == 1) {
- keyCode = (keyCode.toUpperCase()).charCodeAt(0);
- } else {
- return false;
+ keyCode = (keyCode.toUpperCase()).charCodeAt(0);
+ } else {
+ return false;
}
}
return index(_downKeys, keyCode) != -1;
}
-
+
function getPressedKeyCodes() {
return _downKeys;
}
@@ -201,6 +201,16 @@
// reset modifiers to false whenever the window is (re)focused.
addEvent(window, 'focus', resetModifiers);
+ // store previously defined key
+ var previousKey = global.key;
+
+ // restore previously defined key and return reference to our key object
+ function noConflict() {
+ var k = global.key;
+ global.key = previousKey;
+ return k;
+ }
+
// set window.key and window.key.set/get/deleteScope, and the default filter
global.key = assignKey;
global.key.setScope = setScope;
@@ -209,6 +219,7 @@
global.key.filter = filter;
global.key.isPressed = isPressed;
global.key.getPressedKeyCodes = getPressedKeyCodes;
+ global.key.noConflict = noConflict;
if(typeof module !== 'undefined') module.exports = key;
View
20 test/keymaster.html
@@ -230,23 +230,35 @@
t.assertEqual(1, filterCalls);
t.assertEqual(1, keyCalls);
},
-
+
testIsPressed: function (t) {
keydown(65);
t.assertTrue(key.isPressed(65));
t.assertTrue(key.isPressed("a"));
keyup(65);
-
+
keydown(66); keyup(66);
- t.assertFalse(key.isPressed(66));
+ t.assertFalse(key.isPressed(66));
},
-
+
testGetPressedKeyCodes: function (t) {
keydown(65); keydown(KEYS.shift);
var pressedKeys = key.getPressedKeyCodes();
t.assertTrue(pressedKeys.indexOf(65) >= 0);
t.assertTrue(pressedKeys.indexOf(16) >= 0);
keyup(65); keyup(KEYS.shift);
+ },
+
+ testNoConflict: function(t) {
+ var k = key.noConflict();
+ t.assertEqual(undefined, window.key);
+ var called = false;
+ k('a', function() {
+ called = true;
+ });
+ keydown(65); keyup(65);
+ t.assertEqual(called, true);
+ window.key = k;
}
});
</script>
Please sign in to comment.
Something went wrong with that request. Please try again.