![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
HTML DOM KeyboardEvent key Property
The KeyboardEvent key property returns the key identifier corresponding to the key pressed using an event.
Syntax
Following is the syntax −
Returning latest typed character’s identifier −
event.key
Example
Let us see an example for KeyboardEvent key property −
<!DOCTYPE html> <html> <head> <title>KeyboardEvent key</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>KeyboardEvent-key</legend> <label>Fill in the blanks: <input type="text" id="textSelect" placeholder="__ for Ball" onkeypress="getEventData(event)" autocomplete="off"> </label> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var textSelect = document.getElementById("textSelect"); function getEventData(InputEvent) { if(InputEvent.key === 'B') divDisplay.textContent = 'Correct Answer'; else if(InputEvent.key === 'b') divDisplay.textContent = 'Close call, you might have caps lock turned off'; else divDisplay.textContent = 'Try Again, '+textSelect.placeholder; textSelect.textContent = ''; } </script> </body> </html>
Output
This will produce the following output −
Before typing anything in the text field −
After typing ‘b’ in the text field −
After typing ‘B’ in the text field −
Advertisements