6

我想让 Javascript 仅从数字键盘的 Enter 键而不是通常的 Enter 键响应 keypress 或 keydown 事件。根据jQuery keypress docs 中的演示,它们似乎都生成了相同的事件数据,所以我不确定这是否可能。

4

2 回答 2

9

They do generate the same keystroke data, at the level of abstraction that JavaScript has access to. Remember, JavaScript lives in a sandbox (the browser) and has no direct access to the underlying hardware (there are plenty of platforms that don't have a numeric keypad at all, but do have a browser).

This cannot be done.

EDIT:

Support for this has been added for some browsers but does not seem to be universal (see the other answer).

于 2012-05-23T20:24:53.290 回答
6

现在可以将小键盘 Enter 检测为单独的键。使用 KeyboardEvent.location 属性。这样,您可以首先检查键码 13,然后检查键是否在数字键盘上,这决定了数字键盘的输入。

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/location

例子:

    window.onkeydown=function(ev)
    {
         var e= ev || window.event,
         key = e.keyCode
         if ((key===13) && (e.location===3)) {
            console.log("got ya");
            console.log(e.location);
         }
   }
于 2018-04-20T10:36:37.283 回答