Forum Moderators: open

Message Too Old, No Replies

Keypress event for Shift-Tab in javascript

         

anejoel

9:54 am on Nov 3, 2008 (gmt 0)

10+ Year Member



I would like to know how to capture the key press event when Shift and Tab keys are pressed together in Javascript. Actually, I have written the following code for this operation. But I'm not able to check both the keys together. I get a true for shift key press but the keyCode is returned as 0. I also want to know where should I actually trigger this event. Is it during keypress, or keydown or onblur etc. Can anyone help me out at the earliest? Its really urgent.

if (window.event.shiftKey) // to capture shift key press
{
if (window.event.keyCode == 09) // to capture tab key press
{
alert('That\'s the Tab key');
}
}

daveVk

10:43 am on Nov 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this test code

<html>
<head>
<script type="text/javascript">
function test(e) { alert( 'keycode ' + e.keyCode + ' shiftKey ' + e.shiftKey );
}
</script>
</head>
<body>
<input type="text" onkeypress="test(event)" />
</body>
</html>

anejoel

11:09 am on Nov 3, 2008 (gmt 0)

10+ Year Member



Can I use the same code for a radio button. I have four radio buttons and one text box in my page. I have set the focus on any one of the radio buttons. Irrespective of the focus on whichever radio button, when I click on Shift+Tab, my focus should move from this radio button to the text box. I'm not able to capture this using the keypress event. Please help me with this....

daveVk

12:28 am on Nov 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While you can have onkeypress code on radio buttons, and this works on firefox, IE treats Shift+Tab as back Tab and does it own thing with it, I dont think javascript can catch the key in this case.

You could try placing something prior to the radio buttons with an onfocus handler to refocus on the input box (messy).

anejoel

6:12 am on Nov 4, 2008 (gmt 0)

10+ Year Member



Do you know, by any chance, if there is a way to implement the same thing using VB Script? If so, please send a code snippet for better understanding.

daveVk

7:13 am on Nov 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry never used VB Scripts.

anejoel

7:32 am on Nov 4, 2008 (gmt 0)

10+ Year Member



Ok fine... thanks for your help..