The keyboardEvent charCode property in HTML is used to return the unicode value of a character key pressed during a keypress event. It is a read-only property. The unicode character denotes the number of a character(e.g. the unicode for “A” is 65).
Syntax:
event.charCode
Note: This property has been DEPRECATED and is no longer recommended.
Return Value: It returns a number that represents the unicode number of the pressed character.
Example: In this example,we will use the keyboardEvent charCode.
HTML
< html >
< head >
< title >keyboardEvent charCode Property</ title >
</ head >
< body style = "text-align: center;" >
< h1 style = "color:green;" >
GeeksforGeeks
</ h1 >
< h2 >
keyboardEvent charCode Property
</ h2 > Input:
< input type = "text" placeholder = "Press any key.." >
< p id = "p" ></ p >
< script >
// Adding a event listener function
window.addEventListener("keypress", function (event) {
let code = event.charCode;
// Creating a span element
let element = document.createElement("span");
element.innerHTML = "Unicode value: " + code + "< br />";
// Appending span element to the paragraph
document.getElementById("p").appendChild(element);
}, true);
</ script >
</ body >
</ html >
|
Output:
Supported Browsers: The browser supported by keyboardEvent charCode property are listed below:
- Apple Safari
- Google Chrome
- Firefox
- Opera
- Internet Explorer 9.0
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Jun, 2023
Like Article
Save Article