Skip to content Skip to sidebar Skip to footer

Check For Capslock On In "onfocus" Event

My following code for checking whether Capslock is on or not works fine on 'onkeypress' event. But i want it for 'onfocus' event. i tried replacing 'onkeypress' with 'onfocus' for

Solution 1:

There is a jQuery plugin called capslockstate which will keep track of the state of the caps lock key, allowing you to use that information as and when needed.

It will monitor the state for the entire page, and then you can retrieve the state when the desired element gains focus.

It's also based on watching key presses, but not limited to lower ASCII characters and handles situations like the Caps Lock key itself being pressed.

Your situation would become something like:

<scriptsrc="{path-to}/jquery-capslockstate.js"></script><script>
    $(document).ready(function() {
        $(window).capslockstate();

        $(window).bind("capsOn", function(event) {
            if ($("#txtPassword:focus").length > 0) {
                document.getElementById('divMayus').style.visibility = 'visible';
            }
        });
        $(window).bind("capsOff capsUnknown", function(event) {
            document.getElementById('divMayus').style.visibility = 'hidden';
        });
        $("#txtPassword").bind("focusout", function(event) {
            document.getElementById('divMayus').style.visibility = 'hidden';
        });
        $("#txtPassword").bind("focusin", function(event) {
            if ($(window).capslockstate("state") === true) {
                document.getElementById('divMayus').style.visibility = 'visible';
            }
        });
    });
</script><inputtype="text"name="txtuname" /><inputtype="password"name="txtPassword"id="txtPassword" /><divid="divMayus"style="visibility:hidden">Caps Lock is on.</div>

Note that I've only jQueryified the essential bits, more could still be done.

Solution 2:

Unfortunately not - the keyCode property of the event object is only sent on key-based events (for obvious reasons), which is why it wouldn't work onfocus, onclick etc.

There aren't any other JavaScript ways of doing it - although there is a potential solution if you use flash - but that seems somewhat overkill for your requirements...

Solution 3:

Its seems impossible to detect caps lock onfocus I think this may help you please visit this

http://jaspreetchahal.org/jquery-caps-lock-detection-plugin/

Post a Comment for "Check For Capslock On In "onfocus" Event"