PDA

View Full Version : Keymap catch enter ignores shift/ctrl/alt



mdissel
11 Sep 2008, 12:49 AM
I'm adding a keymap using this code: (this = panel object)


this.getEl().addKeyListener({ key: Ext.EventObject.ENTER, shift: false, ctrl: false, alt: false }, Ext.app.activateDefaultButton, this.defaultButton);

But when i press CTRL+enter or ALT+enter or SHIFT+enter, but i only want to catch the ENTER without CTRL/SHIFT or ALT..

Thanks

Marco

evant
11 Sep 2008, 1:40 AM
I don't think that's the intention.

Setting alt/ctrl/shift to says it will only be handled if those keys are pressed. It doesn't say anything about the opposite.

mdissel
11 Sep 2008, 3:30 AM
ok, i'm currently using this 'workaround':


var el = e.getTarget(null, null, true);
if (e.type !== 'keydown' || el.is('textarea') || e.altKey === true || e.ctrlKey === true || e.shiftKey === true)
return;

evant
11 Sep 2008, 3:33 AM
Looks good,

you could short it a tiny bit:



var el = e.getTarget(null, null, true);
if (e.type != 'keydown' || el.is('textarea') || e.altKey || e.ctrlKey || e.shiftKey)
return;

mdissel
11 Sep 2008, 3:53 AM
Yes / No..i'm used to explicitely check for a value and if i want to know if the property is declared i use Ext.isEmpty(..)

asmishra
29 Sep 2008, 11:42 PM
Hi,
I was looking for the same issue.

When i set create a key map like this -


var map = new Ext.KeyMap(this.el, {
key: 'a',
fn: somefn,
shift: false,
ctrl: true,
alt: false,
scope: this,
stopEvent: true
});


It the 'somefn' is called when i press ctrl+a. But its also triggered with other key combinations such that ctrl+alt+a.

As you have already clarified in the post that "Setting alt/ctrl/shift to says it will only be handled if those keys are pressed. It doesn't say anything about the opposite."

But these are the norms which other applications follow. Can you please let me know why keymap behaves like this because i really want to trigger the field when ctrl+a (only this key combination) is pressed. Or is there any other (Ext provided) way i can achieve this? I surely don't want to write my own handler to check all the key combinations. [:)].


Thanks
Ashish

Condor
30 Sep 2008, 12:17 AM
I tend to agree.
Specifying no ctrl config option should mean 'both ctrl pressed and not pressed', but specifying ctrl:false should mean 'ctrl not pressed'.

Suggested fix:

Ext.override(Ext.KeyMap, {
addBinding : function(config){
if(Ext.isArray(config)){
for(var i = 0, len = config.length; i < len; i++){
this.addBinding(config[i]);
}
return;
}
var keyCode = config.key,
shift = config.shift,
ctrl = config.ctrl,
alt = config.alt,
fn = config.fn || config.handler,
scope = config.scope;
if (config.stopEvent) {
this.stopEvent = config.stopEvent;
}
if(typeof keyCode == "string"){
var ks = [];
var keyString = keyCode.toUpperCase();
for(var j = 0, len = keyString.length; j < len; j++){
ks.push(keyString.charCodeAt(j));
}
keyCode = ks;
}
var keyArray = Ext.isArray(keyCode);
var handler = function(e){
if(((shift === undefined) || (shift === e.shiftKey)) &&
((ctrl === undefined) || (ctrl === e.ctrlKey)) &&
((alt === undefined) || (alt === e.altKey))){
var k = e.getKey();
if(keyArray){
for(var i = 0, len = keyCode.length; i < len; i++){
if(keyCode[i] == k){
if(this.stopEvent){
e.stopEvent();
}
fn.call(scope || window, k, e);
return;
}
}
}else{
if(k == keyCode){
if(this.stopEvent){
e.stopEvent();
}
fn.call(scope || window, k, e);
}
}
}
};
this.bindings.push(handler);
}
});

asmishra
30 Sep 2008, 2:02 AM
Thanks for the quick turnaround.
For the time being i could put it in my code and use it but i would like it to be fixed in Ext so that in next release i don't override keymap; as there may be other changes happening in that part of code.
Should i raise a bug to get it fixed in coming release?

Thanks

Condor
30 Sep 2008, 2:42 AM
It's not a bug (it behaves according to specs), but you could post it as a feature request.

asmishra
7 Oct 2008, 12:27 AM
As we discussed I have created a feature request http://extjs.com/forum/showthread.php?t=48621.

When do you think i can expect this feature and in which version.

Condor
7 Oct 2008, 12:30 AM
When do you think i can expect this feature and in which version.

Sorry, but I'm the wrong person to ask. Only Ext Core members make code changes.

TopKatz
19 Nov 2008, 10:00 AM
Ill second this. How else would you trap for both tab, and shift tab?

perler
10 Mar 2009, 9:10 AM
That obviously didn't make it in the 2.2.1 release /:)

Condor
10 Mar 2009, 9:34 AM
That obviously didn't make it in the 2.2.1 release /:)

No, but the current SVN trunk does contain this fix, so it will be part of Ext 3.0 (next month).