JS Coding Questions Logo
JS Coding Questions
#79💼 Interview💻 Code

How do you detect caps lock key turned on or not

Advertisement

728x90

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
1<input type="password" onmousedown="enterInput(event)" />
2
3  <p id="feedback"></p>
4
5  <script>
6
7function enterInput(e) {
8  var flag = e.getModifierState("CapsLock");
9  if (flag) {
10  document.getElementById("feedback").innerHTML = "CapsLock activated";
11  } else {
12  document.getElementById("feedback").innerHTML =
13  "CapsLock not activated";
14  }
15}
16</script>

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 80

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
79of476