PDA

View Full Version : KeyNav / KeyMap -> two normal keys at the same time



tobiu
18 Oct 2010, 6:04 AM
hi team and community,

i know this is not a good concept for key-navigation, but since google uses this in their apps, i have to consider to implement this too.

the question is, how to check if two normal keys are pressed at the same time, like keeping the key G pushed and afterwards pushing U. this is quite easy possible for alt, control, shift and a normal key.

1) does the browser-event itselt support this?
2) is there a clean way to check this?

for a kind of workaround, i would add a timeOut on the keypress-event of one key and if the next key is pressed inside a specified timeframe, the event would be triggered. but this szenario has a potential for memory-leaks, since those listeners would have to be deleted after the relevant timeouts. i have a plan how to implement the "workaround", but would like to know if there is a clean way first.

if there is none, this would also be a nice feature-request ;)


kind regards
tobiu

Animal
18 Oct 2010, 6:09 AM
Sounds like you want a keydown listener and a keyup listener.

Monitor whether both keys enter the "down" state at the same time.

Condor
18 Oct 2010, 6:20 AM
Most keyboards are not build to detect all multi-keystroke combinations (only some "gamer" keyboards can).

I wouldn't recommend using this in an application, since you can't predict what kind of keyboard the user has.

Animal
18 Oct 2010, 6:22 AM
monitoring for key downs and key ups works fine. Try it!

press and hold "G", it fires with "G"'s keycode.

Then press "A". It fires with "A"'s keycode.

You just have to detect when both are down by also monitoring which keycodes pass through a keyup listener.

tobiu
18 Oct 2010, 7:39 AM
hi nige and condor,

thanks for your replies! way faster than i expected :)

i tried out niges tip and it works fine.
here is the code, just in case someone else needs it too:



Ext.ns('Ext.pressedKeys');
// 71 -> G
// 85 -> U

Ext.getDoc().on({
keydown : function(e, f, g){
if(e.getCharCode() == 71){
Ext.pressedKeys['G'] = true;
}
}
,keyup : function(e){
if(e.getCharCode() == 85 && Ext.pressedKeys['G'] == true){
console.log('g and u');
}
Ext.pressedKeys['G'] = false;
}
});



kind regards
tobiu