HTML | DOM KeyboardEvent code Property

The keyboardEvent code property in HTML represents a physical key on the keyboard. It is used to return the key that triggers the event.

Syntax:

event.code

Return Value: It returns a string which represents which key is pressed.



Example 1: With KeyDown Event

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <title>DOM keyboardEvent code Property</title>
</head>
  
<body style="text-align: center;">
    
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
  
    <h2>
            DOM keyboardEvent code Property
        </h2
    
      Input:
    <input type="text" placeholder="Press any key..">
  
    <p id="p"></p>
  
    <script>
        // Adding a event listener function
        window.addEventListener("keydown", function(event) {
            var code = "keydown code = '" +
                event.code + "'" + "<br/>";
  
            // Creating a span element
            var element = document.createElement("span");
            element.innerHTML = code;
  
            // Appending the created element to paragraph
            document.getElementById("p").appendChild(element);
        }, true);
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2: With KeyPress Event

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <title>DOM keyboardEvent code Property</title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
  
    <h2>
            DOM keyboardEvent code 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) {
            var code = "keypress code = '" +
                event.code + "'" + "<br/>";
  
            // Creating a span element
            var element = document.createElement("span");
            element.innerHTML = code;
  
            // Appending the created element to paragraph
            document.getElementById("p").appendChild(element);
        }, true);
    </script>
</body>
  
</html>            

chevron_right


Output:

Example 3: With KeyUp Event

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <title>DOM keyboardEvent code Property</title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
  
    <h2>
            DOM keyboardEvent code Property
        </h2
    
      Input:
    <input type="text" placeholder="Press any key..">
  
    <p id="p"></p>
  
    <script>
        // Adding a event listener function
        window.addEventListener("keyup", function(event) {
            var code = "keyup code = '" +
                event.code + "'" + "<br/>";
  
            // Creating a span element
            var element = document.createElement("span");
            element.innerHTML = code;
  
            // Appending the created element to paragraph
            document.getElementById("p").appendChild(element);
        }, true);
    </script>
</body>
  
</html>            

chevron_right


Output:

Supported Browsers: The browser supported by keyboardEvent code property are listed below:

  • Apple Safari 10.0
  • Google Chrome 48.0
  • Firefox 32.0
  • Opera
  • Internet Explorer Not supported


My Personal Notes arrow_drop_up


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.