在Javascript中,如何检测keydown&中的字符大小写(上/下)关键事件? [英] In Javascript, How to detect the character case (upper/lower) in keydown & keyup events?

查看:107
本文介绍了在Javascript中,如何检测keydown&中的字符大小写(上/下)关键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测事件keydown和keyup中字符的大小写

I need to detect the case of characters in event keydown and keyup

$('body').keydown(
    function(event) {
        var charCode = (event.which) ? event.which : event.keyCode;
        var char = String.fromCharCode(charCode);
        console.log(char + " is down pressed");
    }
);

$('body').keyup(
    function(event) {
        var charCode = (event.which) ? event.which : event.keyCode;
        var char = String.fromCharCode(charCode);
        console.log(char + " is up pressed");
    }
);

你可以在这里试试: http://jsfiddle.net/8dqwW/

即使没有按下大写锁定,也总是返回大写字母。

You may try it here: http://jsfiddle.net/8dqwW/
It's always returning the upper case letter even if no caps lock is pressed.

如何在这两个事件中检测用它的情况按下的字母,无论是上限还是下限?

How can I detect the letter pressed with its case, either upper or lower in those two events ?

推荐答案

keyup keydown 无法检测大写/小写。

只有 keypress 可以这样做!

keyup and keydown cannot detect the upper/lower case.
Only keypress can do so !

这篇关于在Javascript中,如何检测keydown&中的字符大小写(上/下)关键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆