HTML | DOM KeyboardEvent keyCode Property

The KeyboardEvent keyCode property is used for returning the Unicode character code of the key that has been used to trigger an onkeypress event.
The KeyboardEvent keyCode property is also used for returning the Unicode character code of a key that has triggered an onkeydown or onkeyup event.
The key codes represent an actual key on the keyboard whereas character codes represent an ASCII character.
Upper case and lower case characters have different codes.

Syntax

event.keyCode

Below program illustrates the KeyboardEvent keyCode Property :
Example-1: Getting the Unicode value of a pressed keyboard key.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>KeyboardEvent keyCode Property in HTML
  </title>
    <style>
        div {
            border: 3px solid green;
            height: 100px;
            width: 500px;
        }
          
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>KeyboardEvent keyCode Property</h2>
  
    <p>To return the keycode of a key, 
      insert some character in the field.</p>
  
    <input type="text"
           size="20" 
           onkeypress="keyboard(event)">
  
    <p id="test"></p>
  
    <script>
        function keyboard(event) {
            
            
            //  Return key code.
            var k = event.which || event.keyCode;
            document.getElementById("test").innerHTML = 
              "The keycode value of the pressed key is : " + k;
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

Before pressing a button:

After pressing a button:

Supported Browsers:

  • Opera
  • Internet Explorer
  • Google Chrome
  • Firefox
  • Apple Safari


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.