Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture "shift+tab" keypress event

Tags:

jquery

I have a case where I want to capture the simultaneous key press event of "shift+tab" key using jquery. Actually as you all know it is used to move tab in backward direction but for me in one scenario tha tab was not working in any direction i.e. neither forward nor backward. so I found a jquery function for moving tab in forward direction as follows:-

$(':input').live('keydown', function(e) {         var keyCode = e.keyCode || e.which;          if (keyCode == 9) {             tindex = parseInt($(this).attr("tabindex")) + 1;         if($(":input[tabindex='" + tindex + "']"))         {             $(":input[tabindex='" + tindex + "']").focus();         }     } }); 

Now I want to move tah tab in backward direction as well. Can somebody guide me how can i achieve this???

like image 745
Hitesh Gupta Avatar asked Jun 21 '11 07:06

Hitesh Gupta


People also ask

How do you handle shift tab?

To detect shift+tab press with JavaScript, we can check if the shiftKey is true and the keyCode is 9. to check if the shiftKey is pressed and while tab is pressed. The keyCode for the tab key is 9. event is the keyboard event object we get from the keyboard event handler callback's parameter.

How do you shift tab in HTML?

Your question asks for they keycode for shift+tab, but you want to detect the up key? Peter, the keyCode 16 is for the Shift key.


1 Answers

You can use e.shiftKey to check whether the shift key was held when the event was triggered.

If you add an if statement to your event handler, checking whether the shift key was held, you can perform different actions:

if(keyCode == 9) {     if(e.shiftKey) {        //Focus previous input     }     else {        //Focus next input     } } 
like image 141
James Allardice Avatar answered Sep 17 '22 14:09

James Allardice