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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • 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 Append An Object To An Array In JavaScript
  • Add items to an array in JavaScript
  • Convert a string into an array
  • Check if an Item Exists in an Array

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers