2

Is it possible to trigger a keyboard button with JavaScript, and to get a input-case depending on the Caps Lock button?

So, if my Caps Lock is on, "IT SHOULD BE UPPERCASED" or "it should be lowercased if it's off".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    http://stackoverflow.com/questions/348792/how-do-you-tell-if-caps-lock-is-on-using-javascript#answer-348802 – EricG Oct 22 '12 at 10:59
  • Does this answer your question? [How do you tell if caps lock is on using JavaScript?](https://stackoverflow.com/questions/348792/how-do-you-tell-if-caps-lock-is-on-using-javascript) – Brian Tompsett - 汤莱恩 Nov 06 '20 at 15:21

2 Answers2

2

Trigger an key event:

var ev = jQuery.Event("keypress");
ev.ctrlKey = false;
ev.which = 37;
$("container").trigger(ev);

Hope that helps.

tobspr
  • 8,200
  • 5
  • 33
  • 46
  • 1
    This site might help you: http://dougalmatthews.com/articles/2008/jul/2/javascript-detecting-caps-lock/ But notice you can call this function only on a user executed key-press. So your user has to have pressed at least one key to detect whether CapsLock is on. – tobspr Oct 22 '12 at 11:02
  • yes, I notice that... using your example, I could automize it more ;) thank you –  Oct 22 '12 at 11:05
1

javascript event object gives you different key code depending on either you capslock is pressed or not.

$('#yourTxtBox').keypress(function(e){

//eg:for small case letter 'a' it give you '65' and for capital 'A' it gives you '97'

    console.log(e.which);

});

so you can easily identify that your capslock is pressed or not.

Haroon Yousuf
  • 504
  • 4
  • 9