So I've been researching solutions for Caps Lock detection for a form, using JavaScript/jQuery. There is a huge thread on stackoverflow offering many solutions. But the only problem is they don't check whether Caps Lock is pressed directly per se. They detect whether a capital letter has been entered and that the shift key has not been depressed. The drawbacks of this is that my Caps Lock warning message will not toggle on/off until the user starts typing into the form, which is hardly optimal. Here is my code.
document.onkeypress = function(e){
e = e || window.event;
var s = String.fromCharCode(e.keyCode || e.which);
if (s.toUpperCase() === s && !e shiftKey) {
capsWarning.style.visibility = 'visible';
} else {
capsWarning.style.visibility = 'hidden';
}
}
Is there a better way, that I can trigger a caps lock message, with JavaScript when I press the Caps Lock key? That is, not delay toggling message until I start typing?
Preferably I'd prefer a pure JavaScript solution to JQuery. Although if you think jQuery would be really really really good here say why and I could pressure my colleague to let me include it here ;o