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.
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.
Share Improve this question asked May 23, 2012 at 20:23 jfkleinjfklein 9271 gold badge11 silver badges14 bronze badges 2- 2 Personally, and of course depending on what you are building, I think this is not a very good Idea, because (a) the user is used to identical behaviour on both "enter" keys and (b) think of those users, who don't have a NumPad - as for example me on my laptop right now. – Lukx Commented May 23, 2012 at 20:27
- 2 It can be a useful as an alternative to an existing UI control, e.g. the usual way to control something is with a mouse click, but if the user prefers the keyboard and has this key, accept that as well. Of course you can't count on the user having a numeric keypad. – jfklein Commented May 23, 2012 at 21:56
2 Answers
Reset to default 10it 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);
}
}
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).