From 778997ec1856dffeabbba908b584e4970fc0ae0e Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 25 Apr 2011 17:38:40 +0200 Subject: [PATCH] Simplify Key further and fix ToolEvent#getModifiers. --- src/tool/ToolEvent.js | 2 +- src/ui/Key.js | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/tool/ToolEvent.js b/src/tool/ToolEvent.js index 51bb12dc..138aef5e 100644 --- a/src/tool/ToolEvent.js +++ b/src/tool/ToolEvent.js @@ -192,7 +192,7 @@ var ToolEvent = this.ToolEvent = Base.extend({ }, getModifiers: function() { - Key.modifiers; + return Key.modifiers; } // TODO: implement hitTest first diff --git a/src/ui/Key.js b/src/ui/Key.js index fd4b7e33..a26e1bd0 100644 --- a/src/ui/Key.js +++ b/src/ui/Key.js @@ -19,7 +19,7 @@ var Key = new function() { var activeKeys = {}; - var modifiers = this.modifiers = { + var modifiers = { shift: false, control: false, alt: false, @@ -32,13 +32,8 @@ var Key = new function() { keyDown = type == 'keyDown'; this[type.toLowerCase()] = function(event) { var code = event.which || event.keyCode, - key = keys[code] || String.fromCharCode(code).toLowerCase(), - keyActive = activeKeys[key]; - if (!keyActive && keyDown) { - activeKeys[key] = true; - } else if (keyActive && !keyDown) { - delete activeKeys[key]; - } + key = keys[code] || String.fromCharCode(code).toLowerCase(); + activeKeys[key] = keyDown; // If the key is a modifier, update the modifiers: if (modifiers[key] !== undefined) @@ -54,11 +49,12 @@ var Key = new function() { modifiers: modifiers }); } - } + }; }, {}); Event.add(document, eventHandlers); return { + modifiers: modifiers, isDown: function(key) { return !!activeKeys[key]; }