Is it possible to trigger a keyboard button with J

2019-07-16 13:05发布

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".

2条回答
干净又极端
2楼-- · 2019-07-16 13:07

Trigger an key event:

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

Hope that helps.

查看更多
Rolldiameter
3楼-- · 2019-07-16 13:23

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.

查看更多
登录 后发表回答