Get the location of the key:
var x = event.location;
Press any key on the keyboard in the input field to get the location of the key.
<!DOCTYPE html> <html> <body> <input type="text" size="40" onkeydown="myFunction(event)"> <p id="demo"></p> <script> function myFunction(event) {/*from w w w .j ava2 s . com*/ var x = event.location; document.getElementById("demo").innerHTML = "Key location: " + x; } </script> </body> </html>
The location property returns a number that indicates the location of a key on the keyboard or device.
The number is represented by 4 constants:
Value | Meaning |
---|---|
DOM_KEY_LOCATION_STANDARD | The key is not pressed on the right or left side of the keyboard, and was not pressed on the numeric keypad. |
DOM_KEY_LOCATION_LEFT | A left key was pressed. For example, the left "CTRL" key or left "ALT" key on a standard 101 key US keyboard. |
DOM_KEY_LOCATION_RIGHT | A right key was pressed. For example, the right "CTRL" key or right "CTRL" key on a standard 101 key US keyboard. |
DOM_KEY_LOCATION_NUMPAD | The key was pressed on the numeric keypad. For example, key on the right side on a standard keyboard. |
The location property can only be used on the onkeydown and onkeyup event, not onkeypress.
This property is read-only.