jfklein
jfklein

Reputation: 927

How can I detect numeric keypad Enter in Javascript

I'd like to have Javascript respond to a keypress or keydown event from only the numeric keypad Enter key, not the usual Enter key. They both seem to generate the same event data according to the demo in the jQuery keypress docs, so I'm not sure it's possible.

Upvotes: 8

Views: 6607

Answers (2)

Eric J.
Eric J.

Reputation: 150228

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).

Upvotes: 9

Gram
Gram

Reputation: 101

it is possible to detect the numpad Enter as seperate key nowadays. With the KeyboardEvent.location property. this way you can firstly check the keycode 13 and after if the key is on the numpad which devines the numpad enter.

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

example:

    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);
         }
   }

Upvotes: 10

Related Questions