Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How To check whether Caps Lock is on

    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

  • #2
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Resolved

    problem resolved

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts

  • Users who have thanked Philip M for this post:

    Eldarrion (06-01-2009)

  • #4
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down Incorrect Solution

    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.

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Quote Originally Posted by MikeyJudd View Post
    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.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    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:

    Code:
    <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.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    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, 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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •