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.

About Josh

Husband. Father. Pug dad. Born again Linux user. Founder of Holiday API, Backend Engineering Manager and Emoji Specialist at Mailshake (WE'RE HIRING!), and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Pears and Perils
Antifragile: Things That Gain from Disorder
Code Girls: The Untold Story of the American Women Code Breakers of World War II

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles