Javascript 获取小键盘(数字键盘)键的正确 keyCode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5630918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Correct keyCode for keypad(numpad) keys
提问by AEMLoviji
I'm getting codes [96..105]by calling String.fromCharCode(event.keyCode)
when pressing keys [0..9](digits) on the keypad.
Though these codes correspond to characters: 'a b c d e f g h i'instead of [0..9].
当按下键盘上的键[0..9](数字)时,我通过调用获得代码[96..105]。尽管这些代码对应于字符:'abcdefgh i'而不是[0..9]。String.fromCharCode(event.keyCode)
Question:
题:
I have 3 inputs in the form. User allowed to enter only in the 1-st input. While user press keys on keyboard some function need to filter it and write it to 2-nd input if pressed key is digit otherwise it must write it to the 3-rd input. How it can be corrected?
我在表单中有 3 个输入。用户只允许在第一次输入中输入。当用户按下键盘上的键时,某些功能需要对其进行过滤并将其写入第二个输入,如果按下的键是数字,否则必须将其写入第三个输入。如何纠正?
回答by KooiInc
Use the keypress
handler:
使用keypress
处理程序:
[somelement].onkeypress = function(e){
e = e || event;
console.log(String.fromCharCode(e.keyCode));
}
See also: this W3C testdocument
另请参阅:此 W3C 测试文档
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)
)
如果你想使用keyup
orkeydown
处理程序,你可以减去 48 frome.keyCode
得到数字(所以String.fromCharCode(e.keyCode-48)
)
回答by Sacky San
I fixed the issue using following javascript code. The numpad keys lie between 96 to 105. but the real numbers are less 48 from the numpad values. Works with keyup or keydown handler.
我使用以下 javascript 代码解决了这个问题。小键盘键位于 96 到 105 之间。但实数比小键盘值少 48。与 keyup 或 keydown 处理程序一起使用。
var keyCode = e.keyCode || e.which;
if (keyCode >= 96 && keyCode <= 105) {
// Numpad keys
keyCode -= 48;
}
var number = String.fromCharCode(keyCode);
回答by ChrisN
There is a way to do this with keydown, if keypress is not workable due to event canceling needs, etc. Use an if() statement with this test:
如果由于事件取消需要等原因按键无法使用,则有一种方法可以使用 keydown 执行此操作。在此测试中使用 if() 语句:
parseInt(event.keyIdentifier.substring(2),16) > 47 && parseInt(event.keyIdentifier.substring(2),16) < 58
OR, with jQuery events:
或者,使用 jQuery 事件:
parseInt(event.originalEvent.keyIdentifier.substring(2),16) > 47 && parseInt(event.originalEvent.keyIdentifier.substring(2),16) < 58
These examples assume "event" is the keydown event. keyIdentifier is a hexidecimal number that represents the unicode value for the related char. Using keyIdentifier, numbers from the numberpad / keypad AND the numbers above your QWERTY keyboard will all have the same values, 48 - 57 (U+0030 - U+0039), even with the keyDown event.
这些示例假定“事件”是 keydown 事件。keyIdentifier 是一个十六进制数,表示相关字符的 unicode 值。使用 keyIdentifier,数字小键盘/小键盘上的数字和 QWERTY 键盘上方的数字都将具有相同的值,48 - 57 (U+0030 - U+0039),即使有 keyDown 事件也是如此。
Unicode values in the browsers will look like U+0030 or U+002F. Parse this string to only get the hexidecimal value, then use parseInt() with a radix of 16 to convert it to base-10.
浏览器中的 Unicode 值看起来像 U+0030 或 U+002F。解析此字符串以仅获取十六进制值,然后使用基数为 16 的 parseInt() 将其转换为基数 10。
回答by MSS
Ok. But In Firfox Gecko It doesn't work. I use bellow code and I'm happy with it :)
好的。但是在 Firfox Gecko 中它不起作用。我使用波纹管代码,我很满意:)
[somelement].onkeypress = function(e){
var singleChar=e.key || String.fromCharCode(e.keyCode);
console.log(singleChar);
}
回答by Hu?nh H?u ?n
event.charCode at onKeyPress return the same code when press a number in keyboard and keypad. but event.keyCode at onKeyDown (or up) return different code. => get char: use event.charCode at onKeyPress event event.preventDefault() (or event.returnValue=false on IE) for number use event.keyCode at onKeyDown event
当按下键盘和小键盘中的数字时,onKeyPress 中的 event.charCode 返回相同的代码。但 onKeyDown(或 up)处的 event.keyCode 返回不同的代码。=> 获取字符:在 onKeyPress 事件 event.preventDefault() (或 event.returnValue=false 在 IE 上)使用 event.charCode 获取数字在 onKeyDown 事件中使用 event.keyCode
回答by Daniel Bandeira
I think a safer way is not assuming the value keys that will be used. You could get the working value by the KeyboardEvent object and access its properties "DOM_VK_*". Let me give you an example.
我认为更安全的方法不是假设将使用的值键。您可以通过 KeyboardEvent 对象获取工作值并访问其属性“DOM_VK_*”。让我给你举个例子。
const kcN0=KeyboardEvent.DOM_VK_0;//get the 0 key value (commonly,48)
const kcN9=KeyboardEvent.DOM_VK_9;//get the 9 key value (commonly, 57)
const kcNPad0=KeyboardEvent.DOM_VK_NUMPAD0;//get the NUMPAD 0 key value (commonly,96)
const kcNPad9=KeyboardEvent.DOM_VK_NUMPAD9;//get the NUMPAD 9 key value (commonly,105)
const kcNPad_L=KeyboardEvent.DOM_KEY_LOCATION_NUMPAD;//get the key location of NUMPAD (commonly, 3)
and evalute the event variable "kbEv"
并评估事件变量“kbEv”
let pressKeyCode=(kbEv.which||kbEv.keyCode);
if( kbEv.location===kcNPad_L && pressKeyCode>=kcNumP0 && pressKeyCode<=kcNumP9 )
pressKeyCode=kcN0+kcNPad9-pressKeyCode;
let pressKeyCode=(kbEv.which||kbEv.keyCode);
if( kbEv.location===kcNPad_L && pressKeyCode>=kcNumP0 && pressKeyCode<=kcNumP9 )
pressKeyCode=kcN0+kcNPad9-pressKeyCode;
I've only assumed that from 0 to 9 will be sequential in those two disjoint numerical digit pattern set.
我只是假设在这两个不相交的数字模式集中从 0 到 9 是连续的。