caps lock state

# Rob Garrison (9 years ago)

There is no way to determine the actual state of a caps lock key. I would like to request an enhancement of the event object to include the caps lock state:

document.addEventListener( 'keydown', function( event ) {
  if ( event.capsLock ) {
    alert( 'caps lock on!' );
  }
});

This basic method targets the "a" through "z" and capital "A" through "Z" characters to make a comparison. This method does not include accented characters that would be available on non-U.S. or international keyboards, and it can only be used during a "keypress" event. "keypress" is the only event in which event.charCode returns accurate values.

document.addEventListener( 'keypress', function( event ) {
  var k = event.charCode;
  // ASCII uppercase A = 65; ascii uppercase Z = 90
  if ( ( ( k >= 65 && k <= 90 ) && !event.shiftKey ) ||
       // ASCII lowercase a = 97; ASCII lowercase z = 122
       ( ( k >= 97 && k <= 122) && e.shiftKey ) ) {
    alert( 'caps lock on!' );
  }
});

I know modern keyboards are starting to replace the caps lock key with a control key, but I think this would still be a useful addition.

Thanks for your attention. Rob

# Andrea Giammarchi (9 years ago)

this ML is about ECMAScript (aka JavaScript ... or vice-versa)

What you are suggesting is part of DOMland w3c.github.io/uievents which is a different working group/specification.

We could even all agree here about features like capsLock as alternative metaKey but it's the wrong channel to propose this change/improvement.

Best