如何在Javascript中监听键盘类型文本? [英] How to listener the keyboard type text in Javascript?

查看:112
本文介绍了如何在Javascript中监听键盘类型文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取键盘输入文本,而不是键码。例如,我按shift + f,我得到F,而不是听两个关键代码。另一个例子,我点击F3,我什么都没输入。我怎么知道在js?谢谢。

I want to get the keyboard typed text, not the key code. For example, I press shift+f, I get the "F", instead of listen to two key code. Another example, I click F3, I inputted nothing. How can I know that in js? Thank you.

推荐答案

要在文档范围内进行,请使用 keypress 事件如下。目前没有其他广泛支持的关键事件可以做:

To do it document-wide, use the keypress event as follows. No other currently widely supported key event will do:

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    if (charCode) {
        alert("Character typed: " + String.fromCharCode(charCode));
    }
};

对于所有与密钥相关的JavaScript问题,我推荐Jan Wolter的优秀文章: http://unixpapa.com/js/key.html

For all key-related JavaScript matters, I recommend Jan Wolter's excellent article: http://unixpapa.com/js/key.html

这篇关于如何在Javascript中监听键盘类型文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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