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 5 of 5
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post

    Detecting CAPS/NUM when browser not in focus?

    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):
    Code:
    <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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Quote Originally Posted by SlowCoder View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    SlowCoder (11-21-2012)

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    SlowCoder (11-21-2012)

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Gracias, guys. I have thanked you both for your info, and have passed on the bad news to the boss.


  •  

    Posting Permissions

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