Skip to content

Commit

Permalink
Event.definePseudo now calls Events.definePseudos, also updating Keyb…
Browse files Browse the repository at this point in the history
…oard to simply use the full power of the keys pseudo
  • Loading branch information
seanmonstar authored and timwienk committed Jun 5, 2011
1 parent 7d30c9c commit 812aef4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
25 changes: 15 additions & 10 deletions Source/Element/Element.Event.Pseudos.Keys.js
Expand Up @@ -20,14 +20,19 @@ provides: [Element.Event.Pseudos.Keys]
(function(){

var keysStoreKey = '$moo:keys-pressed',
keysKeyupStoreKey = '$moo:keys-keyup';

keysKeyupStoreKey = '$moo:keys-keyup',
store = function(key, val){
this.store ? this.store(key,val) : this[key] = val;
return this;
},
retrieve = function(key, def){
return this.retrieve ? this.retrieve(key, def) : (this[key] || def);
};

Event.definePseudo('keys', function(split, fn, args){

var event = args[0],
keyCombos = split.value.split('|'),
pressed = this.retrieve(keysStoreKey, []);
pressed = retrieve.call(this, keysStoreKey, []);

keyCombos = keyCombos.map(function(key) {
var arr = [];
Expand All @@ -40,22 +45,22 @@ Event.definePseudo('keys', function(split, fn, args){

pressed.include(event.key);

if (keyCombos.some(function(combo) {
if (keyCombos.some(function(combo){
return combo.every(function(key){
return pressed.contains(key);
});
})) fn.apply(this, args);

this.store(keysStoreKey, pressed);
store.call(this, keysStoreKey, pressed);

if (!this.retrieve(keysKeyupStoreKey)){
if (!retrieve.call(this, keysKeyupStoreKey)){
var keyup = function(event){
(function(){
pressed = this.retrieve(keysStoreKey, []).erase(event.key);
this.store(keysStoreKey, pressed);
pressed = retrieve.call(this, keysStoreKey, []).erase(event.key);
store.call(this, keysStoreKey, pressed);
}).delay(0, this); // Fix for IE
};
this.store(keysKeyupStoreKey, keyup).addEvent('keyup', keyup);
store.call(this, keysKeyupStoreKey, keyup).addEvent('keyup', keyup);
}

});
Expand Down
1 change: 1 addition & 0 deletions Source/Element/Element.Event.Pseudos.js
Expand Up @@ -27,6 +27,7 @@ while (count--) pseudos[copyFromEvents[count]] = Events.lookupPseudo(copyFromEve

Event.definePseudo = function(key, listener){
pseudos[key] = Type.isFunction(listener) ? {listener: listener} : listener;
Events.definePseudo(key, listener);
return this;
};

Expand Down
30 changes: 4 additions & 26 deletions Source/Interface/Keyboard.js
Expand Up @@ -173,30 +173,14 @@ provides: [Keyboard]
Keyboard.parse = function(type, eventType, ignore){
if (ignore && ignore.contains(type.toLowerCase())) return type;

type = type.toLowerCase().replace(/^(keyup|keydown):/, function($0, $1){
var keys = type.toLowerCase().replace(/^(keyup|keydown):/, function($0, $1){
eventType = $1;
return '';
});

if (!parsed[type]){
var key, mods = {};
type.split('+').each(function(part){
if (regex.test(part)) mods[part] = true;
else key = part;
});


mods.control = mods.control || mods.ctrl; // allow both control and ctrl

var keys = [];
modifiers.each(function(mod){
if (mods[mod]) keys.push(mod);
});

if (key) keys.push(key);
parsed[type] = keys.join('+');
}

return eventType + ':keys(' + parsed[type] + ')';
return eventType + ':keys(' + keys + ')';
};

Keyboard.each = function(keyboard, fn){
Expand Down Expand Up @@ -225,13 +209,7 @@ provides: [Keyboard]
};

var handler = function(event){
var keys = [];
modifiers.each(function(mod){
if (event[mod]) keys.push(mod);
});

if (!regex.test(event.key)) keys.push(event.key);
Keyboard.manager._handle(event, event.type + ':keys(' + keys.join('+') + ')');
Keyboard.manager._handle(event, event.type);
};

document.addEvents({
Expand Down

0 comments on commit 812aef4

Please sign in to comment.