• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Detect Caps Lock Is On

Detect Caps Lock Is On

Detect Caps Lock Is On

To check if the caps lock is on, you use the getModifierState() method of the KeyboardEvent object:

const capslockIsOn = event.getModifierState(modifier);

The getModifierState() method returns true if a modifier is active; otherwise, it returns false.

The event.getModifierState('CapsLock') can be used to detect if the caps lock is on.

Suppose you have a password field like this:

  <input type="password" name="password" id="password" placeholder="Enter a password">
    <div class="message"></div>

The following shows a warning message if you turn on the caps lock and type the password:

const password = document.querySelector('#password');
const message = document.querySelector('.message');

password.addEventListener('keyup', function (e) {
     if (e.getModifierState('CapsLock')) {
         message.textContent = 'Caps lock is on';
     } else {
         message.textContent = '';
     }
});

Source

https://www.javascripttutorial.net/dom/events/detect-caps-lock-is-on/

Miscellaneous

Related Snippets:

  • How to Get the Query String in JavaScript
  • Looping over an object’s keys and values
  • Short-Circuit Evaluation Using &&
  • Clone an element

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us