Detect Caps Lock with JavaScript

Anyone is capable of having their caps lock key on at any given time without realizing so. Users can easily spot unwanted caps lock when typing in most inputs, but when using a password input, the problem isn’t so obvious. That leads to the user’s password being incorrect, which is an annoyance. Ideally developers could […]

The post Detect Caps Lock with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Anyone is capable of having their caps lock key on at any given time without realizing so. Users can easily spot unwanted caps lock when typing in most inputs, but when using a password input, the problem isn’t so obvious. That leads to the user’s password being incorrect, which is an annoyance. Ideally developers could let the user know their caps lock key is activated.

To detect if a user has their keyboard’s caps lock turn on, we’ll employ KeyboardEvent‘s getModifierState method:

document.querySelector('input[type=password]').addEventListener('keyup', function (keyboardEvent) {
    const capsLockOn = keyboardEvent.getModifierState('CapsLock');
    if (capsLockOn) {
        // Warn the user that their caps lock is on?
    }
});

I’d never seen getModifierState used before, so I explored the W3C documentation to discover other useful values:

dictionary EventModifierInit : UIEventInit {
  boolean ctrlKey = false;
  boolean shiftKey = false;
  boolean altKey = false;
  boolean metaKey = false;

  boolean modifierAltGraph = false;
  boolean modifierCapsLock = false;
  boolean modifierFn = false;
  boolean modifierFnLock = false;
  boolean modifierHyper = false;
  boolean modifierNumLock = false;
  boolean modifierScrollLock = false;
  boolean modifierSuper = false;
  boolean modifierSymbol = false;
  boolean modifierSymbolLock = false;
};

getModifierState provides a wealth of insight as to the user’s keyboard during key-centric events. I wish I had known about getModifier earlier in my career!

The post Detect Caps Lock with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2024-02-06T10:33:10+00:00) Detect Caps Lock with JavaScript. Retrieved from https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/

MLA
" » Detect Caps Lock with JavaScript." David Walsh | Sciencx - Tuesday February 6, 2024, https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/
HARVARD
David Walsh | Sciencx Tuesday February 6, 2024 » Detect Caps Lock with JavaScript., viewed ,<https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/>
VANCOUVER
David Walsh | Sciencx - » Detect Caps Lock with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/
CHICAGO
" » Detect Caps Lock with JavaScript." David Walsh | Sciencx - Accessed . https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/
IEEE
" » Detect Caps Lock with JavaScript." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/. [Accessed: ]
rf:citation
» Detect Caps Lock with JavaScript | David Walsh | Sciencx | https://www.scien.cx/2024/02/06/detect-caps-lock-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.