...

View Full Version : How To check whether Caps Lock is on



Nirbhay
05-27-2009, 12:43 PM
Hi all ,

I want to put an additional check for user while entering password whether
CapsLock is On or not..

can any one help me out ....:)

Thanx in advance

Nirbhay
05-27-2009, 01:19 PM
problem resolved

Philip M
05-27-2009, 01:58 PM
Google is your friend!

http://javascript.internet.com/forms/check-cap-locks.html

MikeyJudd
06-01-2009, 06:18 PM
The link provided does not detect if the caps lock is on. It only detects if you use a capital letter. This is a false script and should be renamed.

Eldarrion
06-01-2009, 06:32 PM
The link provided does not detect if the caps lock is on. It only detects if you use a capital letter. This is a false script and should be renamed.

Actually, it checks if what's inputted is coming in as being an upper-case letter while shift is not being held or whether it is a lower-case letter while shift is being held. Considering CAPS LOCK is a toggle on the KEYBOARD itself, which changes what code the keyboard sends to the computer rather than a toggle on the computer and JavaScript can only detect currently pressed keys.... (at least as far as I know, correct me if I'm wrong)... that seems like a pretty good way to mimic CAPS LOCK detection. Of course, it won't work that great when the user has caps lock pressed and is putting in numbers or special symbols, due to them being unaffected by CAPS LOCK, but eh... it still works in a way. Solution is correct within the limitation of the programming language used. Try to actually have some comprehension of what's being done before pointing accusatory fingers.

rnd me
06-01-2009, 10:38 PM
in firefox for sure, possibly elsewhere, you could simply simulate a key press, and detect the value.

This would be able to actually detect if capslock was on, instead of examining unknown input.

you would know for example, that [A] was pressed, and if it's === to "A" then caps is on...

this has the further benefit of warning a user about capslock BEFORE they start typing, unlike the posted solution which nags them after they have begun typing...

Eldarrion
06-02-2009, 12:19 AM
Not sure how you plan to do that, considering generating a key event requires you to pass the direct key code that it's going to be simulating. So if you give it the code for 'A' (which is 65), what will be sent to the browser is 65... no matter whether you have caps lock turned on or not. What I'm trying in this case in FF3 is:



<script type="text/javascript">
function simulateClick() {
var fireOnThis = document.getElementById('someID');
evObj = document.createEvent('KeyboardEvent');
evObj.initKeyEvent( 'keypress', true, true, window, false, false, false, false, 65, 65 );
fireOnThis.dispatchEvent(evObj);
}

function checkKeyCode( e ) {
var KeyID = e.which;
alert(KeyID);
}
</script>
</head>
<body>
<input type="text" id="someID" value="" onkeypress="checkKeyCode(event)" />
<input type="button" value="keypress" onclick="simulateClick()" />


It does trigger the function assigned to onkeypress on the element I'm firing the event on, but every time the function fires, I get the same key code back no matter whether CAPS LOCK is pressed or not. All in all, that doesn't seem to actually solve the issue, even though it was a good idea.

After some thinking though, I personally doubt JS would have that kind of capability... i.e. simulating a user actually pressing a key on their keyboard... because that would have plenty of fun exploit uses... like someone simulating a CTRL + ALT + DEL combo and other things like that. As such, the event wouldn't go beyond the scope of the browser and the browser itself does not know whether CAPS LOCK is turned on or not... or if it does, it doesn't give JavaScript the permission to alter and/or read it.

rnd me
06-02-2009, 10:30 AM
hmmm. thanks for checking this, sad it didn't work.

i figured if you sent the raw key code (the second to last argument) it would render the keypress. this is true to an extent; sending a backspace is a very fast way to clear a textarea selection without slicing. i knew the fun ones like print screen and ctrl+v were censored. but some of the keys, a lot of them actually, do indeed work as expected.

here is a list of the raw keyCodes (http://websvn.wyzo.com/filedetails.php?repname=wyzo&path=%2Fmozilla%2Fdom%2Fpublic%2Fidl%2Fevents%2FnsIDOMKeyEvent.idl&sc=1), it's a pretty cool list, and it's hard to track down.

sadly, A-Z are blocked from accepting raw keycodes, so you have to specify a unicode char instead, and that has a preset case.

so, we must wait for the first actual keypress to test for caps lock.

i wonder if vbscript's sendKeys method suffers the same lockout...

mic2100
06-02-2009, 11:01 AM
i had this same problem, i was trying to detect whether the caps lock was pressed or not for a password field.

I spent a long time looking but until the user inputs anything you are not gonna detect caps lock pressed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum