JavaScript Keyboard Event | JavaScript Keyboard Key Code
How to get JavaScript Keyboard Event value. Here is the quick solution to get a keyboard event in JavaScript. The event occurs when you press a key on the keyboard.
In this post, you can test yourself on which key you were pressed. When you get this event contains all the event information that you might need to write your own code.
Sometimes you need to prevent some keyboard key operations like prevent to select all text, copy, paste, etc. At that time you need to write a JavaScript keyboard event function to handle it as per your need.
JavaScript Keyboard Key Event
The following JavaScript code, when a keypress event fires, you will get all event properties. For example, when you pressing “1”, you will get “49” value. It is recommended to write logic on an event.which
instead of event.keyCode
and event.charCode
.
document.addEventListener("keydown", function(event) {
console.log(event.which);
console.log(event.altKey);
console.log(event.ctrlKey);
console.log(event.shiftKey);
console.log(event.metaKey);
});
The event.which
property is recommended the property to track keyboard key input. Moreover, avoid using the event.keyCode
and event.charCode
properties.
JavaScript KeyboardEvent Test
Click on the below box and press any keyboard key to get keyboard event information.
JavaScript KeyboardEvent Properties
The following table contains the KeyboardEvent properties and their description.
Properties | Description |
---|---|
altKey | A Boolean value returns when a key event was triggered whether the ALT key was pressed. Example: event.altKey |
charCode | Return the Unicode character code of the key when the event was triggered. Example: event.charCode |
code | Return the code of the key when the event was triggered. Example: event.code |
ctrlKey | Boolean value returns when a key event was triggered. Example: event.ctrlKey |
key | Return the identifier of the key when the event was triggered. Example: event.key |
keyCode | Return the Unicode key code of the key when the event was triggered. Example: event.keyCode |
metaKey | A Boolean value returns whether the META key (Command/Cmd/⌘ key) was pressed when the key event occurred. Example: event.metaKey |
shiftKey | A Boolean value returns when a key event was triggered whether the SHIFT key was pressed. Example: event.shiftKey |
which | Return the Unicode character code of the key when onkeypress event was triggered. Example: event.which |
JavaScript Keycode values
The following table contains values that are getting from event.which
property.
|
|
|
Final Thoughts
That all you need to know how to get a JavaScript keyboard event. Moreover, we see event.which
property is used to write custom logic when the key event triggered. Furthermore, we used the JavaScript keyboard tester tool. Finally, we see JavaScript keycode values that are obtained from the event.which
property.
We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other JavaScript related articles.
If you like our article, please consider buying a coffee for us.
Thanks for your support!
Buy me a coffee!
Greetings from Perú!
Mate! Thanks a lot. I felt the programming is like build lego system. Extremely modular and patience!
Kind regards.
Richard S. (@leptium)