Javascript DOM KeyboardEvent getModifierState() Method

Introduction

Is the Caps Lock key activated?

var x = event.getModifierState("CapsLock");

Write som text in the input field, then activate the Cap Lock key and write some more text in the input field:

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeydown="myFunction(event)">
<p id="demo"></p>
<script>
function myFunction(event) {//from   w w w.j a  v  a2s  .c  o  m
  var x = event.getModifierState("CapsLock");
  document.getElementById("demo").innerHTML = "Caps Lock activated: " + x;
}
</script>

</body>
</html>

The getModifierState() method returns true if the specified modifier key was pressed, or activated.

Modifier keys that are activated only when they are being pressed down:

  • Alt
  • AltGraph
  • Control
  • Meta
  • Shift

Modifier keys that are activated when the following are clicked, and deactivated when they are clicked again:

  • CapsLock
  • NumLock
  • ScrollLock
getModifierState(modifierKey);

Parameter Values

Parameter
Description
modifierKey The key to check if is activated or not.
Legal Values:
"Alt"
"AltGraph"
"CapsLock"
"Control"
"Meta"
"NumLocK"
"ScrollLock"
"Shift"



PreviousNext

Related