20
Views
5
Comments
Solved
How can I detect the simultaneous press of the Tab and the Shift in JavaScript
Application Type
Reactive
Service Studio Version
11.53.24 (Build 61544)

Hi.

How can I detect the simultaneous pressing of the Shift key and the Tab key in JavaScript?

 I can successfully detect the Tab key alone or the Shift key alone, but my current method (event.keyCode === 16 && event.keyCode === 9) doesn't seem to work for detecting both at the same time.

Could you please suggest solution for it.

Solution

Hi,

I think you can try this: 

if (event.shiftKey && event.keyCode === 9) {  

// ...

}


Regards,

Khuong

Hi Khuong Truong.

I tried the way you taught me. But, even if I press the shift key and tab key at the same time, it will be determined that only the tab key is being pressed.


I tried again, I solved the problem. Thanks.

Solution

Hello,

To detect the simultaneous pressing of the Shift key and the Tab key in JavaScript, you can utilize the event.shiftKey property along with the event.keyCode (or event.which) property.

The event.shiftKey returns true if the shift key was pressed when the event was triggered, and false otherwise. For the Tab key, the keyCode is 9.

Here's a solution:


document.addEventListener('keydown', function(event) {

    if (event.shiftKey && event.keyCode === 9) {

        console.log('Shift + Tab was pressed together');

    }

});


With the above code, whenever the Shift key and Tab key are pressed simultaneously, it will log the message "Shift + Tab was pressed together" to the console.

Your current method (event.keyCode === 16 && event.keyCode === 9) doesn't work because event.keyCode cannot have two values at the same time. Instead, you should use the event.shiftKey property to check if the Shift key was pressed and then check the keyCode for the Tab key.

Hope this helps!


Best,
RAD Manage


I solved the problem. Thanks.

Solution

Hi,

I think you can try this: 

if (event.shiftKey && event.keyCode === 9) {  

// ...

}


Regards,

Khuong

Hi Khuong Truong.

I tried the way you taught me. But, even if I press the shift key and tab key at the same time, it will be determined that only the tab key is being pressed.


I tried again, I solved the problem. Thanks.

Solution

Hello,

To detect the simultaneous pressing of the Shift key and the Tab key in JavaScript, you can utilize the event.shiftKey property along with the event.keyCode (or event.which) property.

The event.shiftKey returns true if the shift key was pressed when the event was triggered, and false otherwise. For the Tab key, the keyCode is 9.

Here's a solution:


document.addEventListener('keydown', function(event) {

    if (event.shiftKey && event.keyCode === 9) {

        console.log('Shift + Tab was pressed together');

    }

});


With the above code, whenever the Shift key and Tab key are pressed simultaneously, it will log the message "Shift + Tab was pressed together" to the console.

Your current method (event.keyCode === 16 && event.keyCode === 9) doesn't work because event.keyCode cannot have two values at the same time. Instead, you should use the event.shiftKey property to check if the Shift key was pressed and then check the keyCode for the Tab key.

Hope this helps!


Best,
RAD Manage


I solved the problem. Thanks.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.