获取键盘(小键盘)键的正确keyCode [英] Get Correct keyCode for keypad(numpad) keys

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

问题描述

按键盘上的键时,我收到代码 [96..105] [0..9]
这些代码对应字符:尝试以这种方式获取代码时'abcdefgh i'而不是 [0..9] String.fromCharCode(event.keyCode)。

I'm getting codes [96..105] when pressing keys on the keypad [0..9] These codes correspond to characters: 'a b c d e f g h i' instead of [0..9] while trying to get code with this way String.fromCharCode(event.keyCode).

问题:

Question:

表格中有3个输入。用户只允许输入第一个输入。当用户按下键盘上的键时,如果按下键是数字,则某些功能需要对其进行过滤并将其写入第二个输入,否则必须将其写入第二个输入。如何纠正?请看下面的链接,因为它描述了我实施的所有流程和代码

I have 3 inputs in the form. User allowed to enter only in the first input. While user press keys on keyboard some function need to filter it and write it to second input if pressed key is digit otherwise it must write it to the second input. How it can be corrected? Please see below link as it describes all my flow and codes which I have implemented

这是我的代码

推荐答案

使用 keypress handler:

Use the keypress handler:

[somelement].onkeypress = function(e){
  e = e || event;
  console.log(String.fromCharCode(e.keyCode));
}

参见:此W3C测试文档

如果您想使用 keyup keydown 处理程序,你可以从 e.keyCode 中减去48来获取数字(所以 String.fromCharCode(e.keyCode-48)

if you want to use the keyup or keydown handler, you can subtract 48 from e.keyCode to get the number (so String.fromCharCode(e.keyCode-48))

这篇关于获取键盘(小键盘)键的正确keyCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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