简体   繁体   English

获取小键盘(数字键盘)键的正确 keyCode

[英]Get Correct keyCode for keypad(numpad) keys

I'm getting codes [96..105] by calling String.fromCharCode(event.keyCode) when pressing keys [0..9] (digits) on the keypad.当按下键盘上的键[0..9] (数字)时,我通过调用String.fromCharCode(event.keyCode)获得代码[96..105] Though these codes correspond to characters: 'abcdefgh i' instead of [0..9] .尽管这些代码对应于字符: 'abcdefgh i'而不是[0..9]

Question:题:

I have 3 inputs in the form.我在表单中有 3 个输入。 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?如何纠正?

My implementation in JSFiddle我在 JSFiddle 中的实现

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) )如果您想使用keyupkeydown处理程序,您可以从e.keyCode减去 48 以获得数字(所以String.fromCharCode(e.keyCode-48)

I fixed the issue using following javascript code.我使用以下 javascript 代码解决了这个问题。 The numpad keys lie between 96 to 105 .小键盘键位于96105之间。 but the real numbers are less 48 from the numpad values.但实数比小键盘值少48 Works with keyup or keydown handler.与 keyup 或 keydown 处理程序一起使用。

var keyCode = e.keyCode || e.which;
if (keyCode >= 96 && keyCode <= 105) {
  // Numpad keys
  keyCode -= 48;
}
var number = String.fromCharCode(keyCode);

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.这些示例假定“事件”是 keydown 事件。 keyIdentifier is a hexidecimal number that represents the unicode value for the related char. keyIdentifier 是一个十六进制数,表示相关字符的 unicode 值。 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.使用 keyIdentifier,数字小键盘/小键盘上的数字和 QWERTY 键盘上方的数字都将具有相同的值,48 - 57 (U+0030 - U+0039),即使有 keyDown 事件也是如此。

Unicode values in the browsers will look like U+0030 or U+002F.浏览器中的 Unicode 值看起来像 U+0030 或 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.解析此字符串以仅获取十六进制值,然后使用基数为 16 的 parseInt() 将其转换为基数 10。

Ok.好的。 But In Firfox Gecko It doesn't work.但是在 Firfox Gecko 中它不起作用。 I use bellow code and I'm happy with it :)我使用波纹管代码,我很满意:)

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

event.charCode at onKeyPress return the same code when press a number in keyboard and keypad.当按下键盘和小键盘中的数字时,onKeyPress 中的 event.charCode 返回相同的代码。 but event.keyCode at onKeyDown (or up) return different code.但 onKeyDown(或 up)处的 event.keyCode 返回不同的代码。 => 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.preventDefault() (或 event.returnValue=false 在 IE 上)使用 event.charCode 获取数字在 onKeyDown 事件中使用 event.keyCode

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_*".您可以通过 KeyboardEvent 对象获取工作值并访问其属性“DOM_VK_*”。 Let me give you an example.让我给你举个例子。

 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;

I've only assumed that from 0 to 9 will be sequential in those two disjoint numerical digit pattern set.我只是假设在这两个不相交的数字模式集中从 0 到 9 是连续的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM