aquraishi
aquraishi

Reputation: 137

How to detect caps lock in javascript without comparing what was typed with if shift pressed?

There are lots of solutions including a jQuery one ( http://plugins.jquery.com/plugin-tags/caps-lock) where you check to see if the letter typed is uppercase and if yes, was the shift key pressed. If not then caps lock is on.

This is not what I want. Just go to GMail.com and place your cursor in the password field. As soon as you press caps lock it's indicated. You do not have to type a thing.

This is what I'm looking for.

Upvotes: 0

Views: 1331

Answers (1)

Andy E
Andy E

Reputation: 344585

You can check if the pressed key is the caps lock key in the onkeydown event:

element.onkeydown = function () {
    var evt = arguments[0] || window.evt,
        key = evt.which || evt.keyCode;

    if (key == 20) {
        alert("Caps lock pressed");
    }
}

Note that this won't help if the user already had CAPs lock enabled when moving focus to the input (I'm pretty sure that's indetectable), and that is a more viable use-case for showing a warning IMO.

As pointed out in the comments, however, some operating systems or browsers will natively show you a warning if you have caps lock on whilst focusing a password field. GMail doesn't for me on Win 7, so I don't think it's something they wrote in JavaScript.

Upvotes: 2

Related Questions