Explain Codes LogoExplain Codes Logo

What is the key code for shift+tab?

javascript
event-handling
keyboard-events
accessibility
Alex KataevbyAlex Kataev·Jan 12, 2025
TLDR

The key combination Shift+Tab in JavaScript can be detected using the event.key and event.shiftKey properties. You'd want to check if the Tab keypress event occurred with the shiftKey property set to true in your event handler:

document.addEventListener('keydown', (event) => { if (event.key === 'Tab' && event.shiftKey) { // This part is reversed! Now we're going backwards! } });

Opt for the event.key instead of key codes for readable, modern code.

Embrace the modern: e.key and e.code

Web standards evolve and so should our code! e.key and e.code are now the go-to over the deprecated e.keyCode. The e.key property gives us a friendly key value, while e.code pins down the physical key location, irrespective of locale.

Playing fair with older browsers

While event.keyCode is effectively retired, a handful of legacy browsers may not support e.key. In such cases, you can use feature detection to revert to e.keyCode.

Think Accessibility: It's not a shift but a giant leap

When crafting key event handlers, keep accessibility in the forefront of your mind. By using proper HTML and ARIA roles, you pave the way for assistive technologies to navigate your app with ease.

Advanced key properties: Meet code and key

  • event.code doesn't care about locale or keyboard layout – it's all about the physical keys.
  • event.key is all about the character or function the key represents (like Tab) - it's the champ of navigation and action triggering.

Making event handling robust

Setting up event listeners? Take care while handling keyboard events, such as Shift+Tab. Weave in conditional logic using e.shiftKey and e.code to spring into action when the keys form a duo.

Applied Shift+Tab: Inventive use cases

  • Dynamic forms: Leap backward through form inputs, no mouse necessary.
  • Grid navigation: Fluidly move focus between grid cells for easy data entry and review.
  • Game controls: Dictate player movements or UI elements in browser games.