数字小键盘的 keyCode 值? [英] keyCode values for numeric keypad?

查看:40
本文介绍了数字小键盘的 keyCode 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数字小键盘上的数字与键盘顶部的数字是否具有不同的键码?

这是一些应该在 keyup 事件上运行的 JavaScript,但前提是键码在 48 到 57 之间.代码如下:

$('#rollNum').keyup(function(e) {if(e.keyCode >= 48 && e.keyCode <= 57) {//仅0-9无功最大值 = 15;var textLen = $(this).val().length;var textLeft = max - textLen;...

我的问题是此代码仅响应在键盘顶部输入的数字而运行,而不会响应从数字键盘输入的数字.

我想答案一定是数字小键盘有不同的 keyCode 值,但我如何找出它们是什么?

解决方案

键码不同.Keypad 0-9 是 Keycode 96 to 105

你的 if 语句应该是:

if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)){//仅 0-9}

这是键码参考指南


-- 更新 --

这是一个旧答案,keyCode 已被弃用.现在有其他方法可以实现这一点,例如使用 key:

if ((e.key >= 48 && e.key <= 57) || (e.key >= 96 && e.key <= 105)){//仅 0-9}

这是一个event.key的输出测试器,感谢@Danziger提供链接.

Do the numbers on a numeric keypad have a different keycode than the numbers at the top of a keyboard?

Here is some JavaScript that is supposed to run on the keyup event, but only if the keycode is between 48 and 57. Here is the code:

$('#rollNum').keyup(function(e) {
    if(e.keyCode >= 48 && e.keyCode <= 57) { //0-9 only
        var max = 15;
        var textLen = $(this).val().length;
        var textLeft = max - textLen;
        . . . 

My problem is that this code only runs in response to the numbers entered at the top of the keyboard, but does not run in response to numbers entered from the numeric keypad.

I'm thinking the answer must be that the numeric keypad has different keyCode values, but how do I find out what those are?

解决方案

The keycodes are different. Keypad 0-9 is Keycode 96 to 105

Your if statement should be:

if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) { 
  // 0-9 only
}

Here's a reference guide for keycodes


-- UPDATE --

This is an old answer and keyCode has been deprecated. There are now alternative methods to achieve this, such as using key:

if ((e.key >= 48 && e.key <= 57) || (e.key >= 96 && e.key <= 105)) { 
  // 0-9 only
}

Here's an output tester for event.key, thanks to @Danziger for the link.

这篇关于数字小键盘的 keyCode 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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