HTML | KeyboardEvent which Property

The KeyboardEvent which property is used for returning the Unicode character code of the key which triggers the onkeypress event. It also returns the Unicode key code of the key that triggered the onkeydown or onkeyup event.
The key code is a number which represents an actual key on the keyboard, unlike the character codes which represents the ASCII character.

Syntax :

event.which

Below program illustrates the KeyboardEvent which property :
Example: Returning the Unicode character code of the key.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>KeyboardEvent which Property in HTML</title>
    <style>
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>KeyboardEvent which Property</h2>
  
    <p> To get the Unicode character code of the key, 
      Press the key on the keyboard in the input field.</p>
  
    <input type="text" size="50" onkeypress="MyEvent(event)">
  
    <p id="test"></p>
    <!-- Script to return Unicode Value. -->
    <script>
          
        function MyEvent(event) {
            var e = event.which;
            document.getElementById("test").innerHTML = 
              "Unicode value of the pressed key is: " + e;
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

After clicking the 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.