
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Simplest way to detect keypresses in JavaScript?
The easiest way to detect keypresses in JavaScript, use the onKeyPress event handler −
document.onkeypress
The key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale= 1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <script> document.onkeypress = function (eventKeyName) { eventKeyName = eventKeyName || window.event; if(eventKeyName.keyCode==13){ console.log('You have pressed enter key'); } else { alert(String.fromCharCode(eventKeyName.keyCode)) } }; </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
Case 1 − On pressing ENTER key, you will get the following output on console.
Case 2 − When you press any other key except ENTER, example letter g, then the same would be visible on console −
- Related Articles
- Simplest way to detect client locale in PHP
- What is the simplest way to SSH using Python?
- what is the simplest way to print a java array
- What's the simplest way to print a Java array?
- What's the best way to detect a 'touch screen' device using JavaScript?
- Simplest way to copy data from one table to another new table in MySQL?
- What is the simplest way to make Matplotlib in OSX work in a virtual environment?
- The simplest way to get the user's current location on Android
- How to Detect User Timezone in JavaScript?
- Simplest code for array intersection in JavaScript?
- The simplest way to get the user's current location on Android using Kotlin?
- How to detect a mobile device with JavaScript?
- How to detect the screen resolution with JavaScript?
- How to detect that JavaScript Cookies are disabled?
- How to detect all active JavaScript event handlers?

Advertisements