Simplify Key further and fix ToolEvent#getModifiers.

This commit is contained in:
Jonathan Puckey 2011-04-25 17:38:40 +02:00
parent 404bbac3a5
commit 778997ec18
2 changed files with 6 additions and 10 deletions

View file

@ -192,7 +192,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
}, },
getModifiers: function() { getModifiers: function() {
Key.modifiers; return Key.modifiers;
} }
// TODO: implement hitTest first // TODO: implement hitTest first

View file

@ -19,7 +19,7 @@ var Key = new function() {
var activeKeys = {}; var activeKeys = {};
var modifiers = this.modifiers = { var modifiers = {
shift: false, shift: false,
control: false, control: false,
alt: false, alt: false,
@ -32,13 +32,8 @@ var Key = new function() {
keyDown = type == 'keyDown'; keyDown = type == 'keyDown';
this[type.toLowerCase()] = function(event) { this[type.toLowerCase()] = function(event) {
var code = event.which || event.keyCode, var code = event.which || event.keyCode,
key = keys[code] || String.fromCharCode(code).toLowerCase(), key = keys[code] || String.fromCharCode(code).toLowerCase();
keyActive = activeKeys[key]; activeKeys[key] = keyDown;
if (!keyActive && keyDown) {
activeKeys[key] = true;
} else if (keyActive && !keyDown) {
delete activeKeys[key];
}
// If the key is a modifier, update the modifiers: // If the key is a modifier, update the modifiers:
if (modifiers[key] !== undefined) if (modifiers[key] !== undefined)
@ -54,11 +49,12 @@ var Key = new function() {
modifiers: modifiers modifiers: modifiers
}); });
} }
} };
}, {}); }, {});
Event.add(document, eventHandlers); Event.add(document, eventHandlers);
return { return {
modifiers: modifiers,
isDown: function(key) { isDown: function(key) {
return !!activeKeys[key]; return !!activeKeys[key];
} }