Skip to main content
InterviewHubPRO
Section Overview
Q83

Question 83 of 536

Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

15%
JavaScript & V8 Internals (536 Qs)•Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

How do you detect caps lock key turned on or not

mid LevelMost AskedCode ExampleAsync & Promises

The mouseEvent getModifierState() is used to return a boolean value that indicates whether the specified modifier key is activated or not. The modifiers such as CapsLock, ScrollLock and NumLock are activated when they are clicked, and deactivated when they are clicked again.

Let's take an input element to detect the CapsLock on/off behavior with an example:

html
<input type="password" onmousedown="enterInput(event)" />

<p id="feedback"></p>

<script>
  function enterInput(e) {
    var flag = e.getModifierState("CapsLock");
    if (flag) {
      document.getElementById("feedback").innerHTML = "CapsLock activated";
    } else {
      document.getElementById("feedback").innerHTML =
        "CapsLock not activated";
    }
  }
</script>