0

I would like to know if it is possible to detect the state of caps lock key using jquery and without any key press.

Let me be more clear. I have a password field. When I just click on it or when the password field is on focus, I need to display a warning message if the caps lock is on (similar to windows login). I know about the method of comparing character codes, but that is not what i need.

This is for a Yii application. So if it have any default function to check caps lock state, please let me know.

Any help will be highly appreciated..!

Thanks..!

tanuja90
  • 127
  • 1
  • 3
  • 13

4 Answers4

1

In JavaScript in a browser, you either have a solution based on what is provided to you by the browser in the standard API or you don't.

In this case, there is no direct access to the state of the keyboard so the answer is no : you can't do better than displaying your message when the user hit a key.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Google search showed this. I don't thing you can check the status directly thought. So you'd find out only after a letter is typed.

http://capslock.mclean.ws/

So to answer your question: Is it possible to detect if 'capslock is on' in a click event in jquery - No!

Miro
  • 8,402
  • 3
  • 34
  • 72
0

Use this

document.onkeydown = function (e) { //check if capslock key was pressed in the whole window
    e = e || event;
    if (typeof (window.lastpress) === 'undefined') {
        window.lastpress = e.timeStamp-51; 
     }

        if (e.keyCode == 20 && e.timeStamp > window.lastpress + 50) {
            if (typeof (window.capsLockEnabled) !== 'undefined') {
                window.capsLockEnabled = !window.capsLockEnabled;
            }
            if(!$(document).data("capsOn")){
              $(document).data("capsOn", true);
            }else{
              $(document).data("capsOn", false);
            }
        }
        window.lastpress = e.timeStamp;
        //sometimes this function is called twice when pressing capslock once, so I use the timeStamp to fix the problem


};

and then below code

$("#password").click(function(){
if($(document).data("capsOn")){
 alert("CAPS ON");
}
});

DEMO

rahul maindargi
  • 5,359
  • 2
  • 16
  • 23
  • Doesn't work if the caps lock is already on when the user enters the page, and even if they press it while on the page it's very unreliable. It gives the alert seemingly at random. – JJJ May 13 '13 at 07:42
  • working for me every time .."except caps lock is already on when the user enters the page " – rahul maindargi May 13 '13 at 12:45
-1

try using jquery plugin for caps lock http://capslock.mclean.ws/

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/

Ganesh Bora
  • 1,133
  • 9
  • 17