...

View Full Version : Detecting a keyCode for TAB during on blur



contactanantha
03-16-2012, 12:41 PM
Hi,

I have do a calculation when an user enters the value in the textbox during onblur event. But during the onblur event I need to capture the tabkey (keyCode) and if it is 9 do the calculate else not do that. But the problem is we cannot detect a keyCode for tab in onblur. But for sure I need to use onblur in this case and cannot use onkeydown as this will have impact on the UI screen for this particular scenario. How to go about this?
in the textbox I am calling onblur = "test()"

Please provide a helping hand.

Thanks.

felgall
03-16-2012, 09:39 PM
That field doesn't receive the tab key. The browser intercepts the tab key and actions it and so it never reaches anywhere the JavaScript can see with respect to that field.

You might be able to detect it with the window.onkeypress event

Philip M
03-17-2012, 10:01 AM
<input type = "text" id = "textfield" size = "20" onkeydown = "chk(event)">

<script type = "text/javascript">

function chk(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 9) {
alert ("You pressed the TAB key");
return false;
}
}

</script>

felgall
03-17-2012, 11:13 PM
keydown

browsers don't pass the actual code of the key that is pressed into that event as they haven't yet received the code when they trigger that event. When the code is received the keypress event is triggered. At least that's always been my understanding of the difference between those two events and corresponds to what testing I have done with them.

webdev1958
03-18-2012, 02:49 AM
browsers don't pass the actual code of the key that is pressed into that event as they haven't yet received the code when they trigger that event. When the code is received the keypress event is triggered. At least that's always been my understanding of the difference between those two events and corresponds to what testing I have done with them.

Not sure what you're waffling on about :rolleyes:.

philip_m's code works fine in my IE9 and FF11 running on an xampp server. But I would have used onkeypress instead of onkeydown

felgall
03-18-2012, 03:04 AM
Not sure what you're waffling on about

The keydown event isn't guaranteed to receive the keycode as it is intended to test whether ANY key has been pressed (including those that don't have keycodes).

The keypress event is guaranteed to have the keycode available.

keydown is triggered by a key (any key being depressed)
keypress is triggered by a character being typed

where you are interested in the keycode you should be using the keypress event as then you don't waste time on processing key presses that don't generate characters of input for example CTRL, SHIFT, ALT, scroll lock, pause,the window key, and the contextmenu key all trigger a keydown event but don't trigger a keypress event because they don't have a keycode to pass.

Philip M
03-18-2012, 10:43 AM
philip_m's code works fine in my IE9 and FF11 running on an xampp server. But I would have used onkeypress instead of onkeydown

When I test it using onkeypress nothing happens. :p As felgall says, keypress is triggered by a character being typed, not a key being presed.

My understanding is that the onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the user has pressed a key, IMO you should use the onkeydown event instead, because it works for all keys, including TAB.

Logic Ali
03-18-2012, 07:38 PM
Hi,

But during the onblur event I need to capture the tabkey (keyCode) and if it is 9 do the calculate else not do that.What if the blur wasn't the result of a keystroke?

You could apply an onblur handler to all form elements, which stores a reference to that element in a persistent variable. Then you have a record of which element had the focus last. If it was the one in question, don't do the calculation. No key reading needed.

contactanantha
03-18-2012, 07:51 PM
Thanks. The problem as I stated before when i use onkeydown I am able to detect the keyCode for TAB , but I want to handle on blur event once the key has been detected as TAB. How to do that any examples are appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum