...

View Full Version : controlling arrow keys



ali haydar
02-01-2012, 12:31 PM
hi everybody;

i'm using this code to trigger functions with left and right arrow keys..

but they work even if the arrow keys pressed in input boxes.. is there any way to prevent this code to work when user presses the arrow keys in input boxes?





document.onkeydown = function(evt) {
evt = evt || window.event;
switch (evt.keyCode) {
case 37:
geri();
gostergeyaz();
break;

case 39:
ileri();
gostergeyaz();
break;
}
};

theghostofc
02-01-2012, 12:56 PM
hii Ali Haydar

You can try to unbind and bind the document.onkeydown event depending on if the textbox has focus or not.

eg.
txtbox.onfocus = unbind the document.onkeydown function
txtbox.onblur = bind the document.onkeydown function

Hope this helps!

Cheers

Logic Ali
02-01-2012, 01:08 PM
document.onkeydown = function( e )
{
var evt = e || window.event,
srcElem = evt.target || evt.srcElement;

if( !/^(INPUT|TEXTAREA)/.test( srcElem.nodeName ) )
{
switch (evt.keyCode)
{
case 37:
geri();
gostergeyaz();
break;

case 39:
ileri();
gostergeyaz();
break;
}
}

};

ali haydar
02-01-2012, 01:14 PM
thank you very much Logic Ali, fantastic.. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum