Closed Thread Icon

Topic awaiting preservation: js caps lock detection (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26322" title="Pages that link to Topic awaiting preservation: js caps lock detection (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: js caps lock detection <span class="small">(Page 1 of 1)</span>\

 
qwertyuiop
Nervous Wreck (II) Inmate

From: California
Insane since: Jul 2005

posted posted 07-24-2005 22:48

Hi there,
I was just playing around with a form, and I was wondering if it's possible to detect when the Caps Lock is pressed.
I've seen a function that doesn't really detect the Caps Lock, it only assumes that the Caps Lock is pressed when it notices uppercase characters without the Shift key being pressed.

Is this possible? Can javascript detect the Caps Lock key?

Here's a small test, but it didn't seem to work:

code:
<script type="text/javascript">
function capsLock(e){
if(e.which==20)
alert("Caps Lock");
}
document.onkeypress=capsLock;
</script>



kryogenix
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jul 2005

posted posted 07-25-2005 02:04

You can't detect it correctly; in fact, as far as I'm aware, nothing at all in software can detect Caps Lock, because the keyboard handles it itself and just sends capital letters. Checking for capitals without shift is a good (and the only) way to do it.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-25-2005 18:05

I gotta believe that the machine knows if the caps lock is on. If you go to log into a Windows XP/2003 box, and the caps lock is on, you get a warning that caps lock is on.

qwertyuiop
Nervous Wreck (II) Inmate

From: California
Insane since: Jul 2005

posted posted 07-25-2005 18:48

So javascript can't detect Caps Lock state then? I was trying to make a function that when Caps Lock is ON, then there will be a indicator next to the form, and if it's OFF, the no indicator.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-25-2005 20:47

What you can do actually is when someone enters a text field, determine whether the shift key is down when they type. If the characters are upper case and the shift key isn't pressed then it means the caps lock is on.

But it wouldn't let you have a consistent indicator.



.:[ Never resist a perfect moment ]:.

Danaan
Nervous Wreck (II) Inmate

From: Here, there and everywhere
Insane since: May 2005

posted posted 07-25-2005 22:56

I'm pretty sure your computer does know if Caps Lock is on. In Microsoft Word, pressing that key displays CAPS in the corner...somewhere. I think. I don't see it right now, but I'm sure I've seen it do that before.

[edit]Oh, and I agree with Pugzly's point.[/edit]



(Edited by Danaan on 07-25-2005 22:57)

qwertyuiop
Nervous Wreck (II) Inmate

From: California
Insane since: Jul 2005

posted posted 07-27-2005 21:06

Ok, thanks. I just wanted to confirm if this was possible.

BillyRayPreachersSon
Bipolar (III) Inmate

From: London
Insane since: Jul 2004

posted posted 07-30-2005 12:00
quote:

bitdamaged said:

What you can do actually is when someone enters a text
field, determine whether the shift key is down when they type. If the
characters are upper case and the shift key isn't pressed then it means
the caps lock is on.

But it wouldn't let you have a consistent indicator.



That test is slightly flawed. Consider someone entering uppercase characters using ALT+keycode - neither shift nor caps lock have to be used to do this.

If that's not really an issue (i.e. if your users aren't likely to try and break your system), then there are plenty of examples that do just that (found via Google). Here is one such example:

http://www.howtocreate.co.uk/jslibs/htmlhigh/capsDetect.html

Hope this helps,
Dan

« BackwardsOnwards »

Show Forum Drop Down Menu