Detecting modifier keys being pressed with JavaScript

Josh Sherman
1 min read
Software Development JavaScript

Been hot on a new [old] project recently and I’ve not only been coding my ass off, but delving into some territory that I haven’t delved into as of late.

The latest is in terms of determining if a modifier key has been pressed in conjunction with another key press. So like, Shift being help while a letter or other key has been tapped.

Turns out, you can detect these modifier quite easily, and there are *Key values on the event object for all of the major modifier keys like control, alternate and such.

When interrogating an event object, you can simply look for the following parameters:

Checking the aforementioned values in addition to event.key makes it easy to determine if the user is pressing Ctrl+F and the like.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles