Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

javascript - 您如何判断大写锁定是否在使用JavaScript?(How do you tell if caps lock is on using JavaScript?)

How do you tell if caps lock is on using JavaScript?(您如何判断大写锁定是否在使用JavaScript?)

One caveat though: I did google it and the best solution I could find was to attach an onkeypress event to every input, then check each time if the letter pressed was uppercase, and if it was, then check if shift was also held down.(需要注意的是:我用google搜索了,找到的最佳解决方案是将onkeypress事件附加到每个输入,然后每次检查所按字母是否为大写,如果是大写,则检查是否还按住shift键。)

If it wasn't, therefore caps lock must be on.(如果不是,则必须打开大写锁定。) This feels really dirty and just... wasteful - surely there's a better way than this?(这感觉真的很脏,只是...很浪费 -当然有比这更好的方法了吗?)   ask by nickf translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In jQuery,(在jQuery中,)

$('#example').keypress(function(e) { 
    var s = String.fromCharCode( e.which );
    if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) {
        alert('caps is on');
    }
});

Avoid the mistake, like the backspace key, s.toLowerCase() !== s is needed.(避免错误,例如需要使用backspace键s.toLowerCase() !== s 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...