PDA

View Full Version : Check asynchronly for keys



wuschba
14 Sep 2008, 11:27 PM
Hello!

How can I check asynchronly if a key (for example shift) is pressed?

I have a tab-layout and wanted to do different things depending on the user changing the tab with shift pressed and without shift pressed.

evant
14 Sep 2008, 11:37 PM
Are you using a KeyMap?

If so:



new Ext.KeyMap(Ext.getDoc(),
{
key: 'f',
fn: function(key, e)
{
if (e.shiftKey)
;//foo
}
}
);

wuschba
15 Sep 2008, 12:21 AM
Are you using a KeyMap?

Not yet ;-) So the way would be using a KeyMap and store the value of the shift-key in a variable in my onTabChanged-hanlder? How do I get notified when the key is released?

evant
15 Sep 2008, 12:27 AM
The function automatically gets called when the key is pressed.

wuschba
15 Sep 2008, 12:37 AM
The function automatically gets called when the key is pressed.
Yes, but how do I get notfied when the key is released?

What I need is:


myTab.on('tabchange', function()
{ if (shiftPressed)
// Do something
else
// Do something else
});


The idea is to do something like:



var shiftPressed = false;

new Ext.KeyMap(Ext.getDoc(),
{
key: 'shift',
fn: function(key, e)
{ shiftPressed = true;
},
fnReleased : function(key, e)
{ shiftPressed = false;

}
);



This doesn't work of course in that way, it's just to get the idea.

evant
15 Sep 2008, 12:40 AM
At the moment you can't, if they keys are held down it will continue to fire.

santosh.rajan
15 Sep 2008, 12:42 AM
I have worked earlier on key handling in javascript, but not in Ext. Even in javascript it is a headache, because you have to not only detect keydown and wait for keyup. Keypress is of no use in this case. There are browser compatibility issues also with this. I would recommend that you dont use shift handling at all unless absolutely necessary.

wuschba
15 Sep 2008, 12:45 AM
At the moment you can't, if they keys are held down it will continue to fire.
Thanks for your replies. Even the shift-key? I tought this key is handled differntly by the OS, but I might be wrong of course.

wuschba
15 Sep 2008, 12:46 AM
I have worked earlier on key handling in javascript, but not in Ext. Even in javascript it is a headache, because you have to not only detect keydown and wait for keyup. Keypress is of no use in this case. There are browser compatibility issues also with this. I would recommend that you dont use shift handling at all unless absolutely necessary.
Thanks for this hint - then I wont even try to use window.onkeypress. I will think of antoher way to do it then.