...

View Full Version : Detecting CAPS/NUM when browser not in focus?



SlowCoder
11-21-2012, 03:57 PM
I know this is a strange question. My supervisor has asked me to see if there's a way to determine, via web browser, the state of the CAPS and NUM lock keys. However, we don't want to have to rely on the browser being in focus when the keys are toggled.

I've played a little with this code, which is plastered all over the Internet (and now here too :P):

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script language="Javascript">
$(document).ready(function(){
$('input').keypress(function(e) {
var s = String.fromCharCode( e.which );

if((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) ||
(s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)){
if($('#capsalert').length < 1) $(this).after('<b id="capsalert">CapsLock is on!</b>');
} else {
if($('#capsalert').length > 0 ) $('#capsalert').remove();
}
});
});
</script>
</head>
<body>
<label style="float:left;display:block;width:80px;">Login:</label><input type="text" /><br />
<label style="float:left;display:block;width:80px;">Password:</label><input type="password" /><br />
</body>
</html>
However, it doesn't alert until after an initial keypress. We don't want to have to press a key.

What may be our options?

Philip M
11-21-2012, 04:12 PM
However, it doesn't alert until after an initial keypress. We don't want to have to press a key.

What may be our options?

None. Not possible unless the browser is running.

JavaScript running in a browser is a client-side language. JavaScript has no capability to access the client's operating system or the Windows registry, or alter the default behaviour of the browser.

You can of course detect if the CAPSlock key or NUMlock key have been pressed if the browser is running.

Surely your keyboard has little coloured LEDs to show whether CAPSlock and NUMlock are activated?

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

SlowCoder
11-21-2012, 04:24 PM
None. Not possible unless the browser is running.

JavaScript running in a browser is a client-side language. JavaScript has no capability to access the client's operating system or the Windows registry, or alter the default behaviour of the browser.

You can of course detect if the CAPSlock key or NUMlock key have been pressed if the browser is running.

While I welcome other answers, this is basically the answer I was expecting. :(

rnd me
11-21-2012, 05:12 PM
i second that you are SOL.
for a brief while, document.createEvent("KeyEvents") could trigger caps-lock, so a before and after was possible, but that "security" problem got fixed quickly, maybe in beta.

the only info js has about keys is from events, and those are only created when keys are pressed.

if the username is persisted, most people start typing passwords without really looking closely at the screen. this means getting the warning one keystroke earlier won't make much diff in the real world. alert() is bad because someone might WANT caps-lock on, and it's annoying and blockable.

an unobtrusive visual warning is much better, especially on an error page.

SlowCoder
11-21-2012, 06:19 PM
Gracias, guys. I have thanked you both for your info, and have passed on the bad news to the boss. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum