Friday, October 24, 2025
HomeLanguagesJavascriptJavaScript Detecting the pressed arrow key

JavaScript Detecting the pressed arrow key

Sometimes we need to detect the keys and sometimes even detect which keys were pressed. To detect which arrow key is pressed, We are going to look at 2 examples.

onkeydown Event: This event occurs when someone presses a key (on the keyboard). 

Syntax:

  • in HTML:
<element onkeydown="newScript" >
  • in JavaScript:
object.onkeydown = function(){newScript};
  • in JavaScript using the addEventListener() method:
object.addEventListener("keydown", newScript);

JavaScript addEventListener() method: This method attaches an event handler to the document. 

  • Syntax:
document.addEventListener(event, function, captureBoolean)

Parameters:

  • event: This parameter is required. It specifies the name of the event in string format.
  • function: This parameter is required. It specifies the function to run when the event happens. When the event occurs, an event object as a first parameter is passed to the function. Event object’s type depends on the specified event.
  • captureBoolean: This parameter is optional. It specifies the boolean value that tells whether the event should be executed in the capturing or in the bubbling phase. The default value is false and event executed in the bubbling phase and for true event executed in capturing phase.

Example 1: This example detects the arrow key by onkeydown Event by using event.keyCode

html




<h1 style="color:green;">
    GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size: 16px;">
    Press an arrow key and click the
    button to know which key was pressed
    last time.
</p>
<button onclick="gfg_Run()">
    Detect Arrow key
</button>
<p id="GFG_DOWN" style="color:green;
            font-size: 20px;
            font-weight: bold;">
</p>
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var str = 'No key pressed';
      
    function gfg_Run() {
        el_down.innerHTML = str;
    }
    document.onkeydown = function(e) {
        switch (e.keyCode) {
            case 37:
                str = 'Left Key pressed!';
                break;
            case 38:
                str = 'Up Key pressed!';
                break;
            case 39:
                str = 'Right Key pressed!';
                break;
            case 40:
                str = 'Down Key pressed!';
                break;
        }
    };
</script>


Output:

JavaScript Detecting the pressed arrow key

JavaScript Detecting the pressed arrow key

Example 2: This example detects the arrow key by adding a eventListener(keydown) to the body by using event.key

html




<h1 style="color:green;">
    GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size: 16px;">
    Press an arrow key and click the
    button to know which key was pressed
    last time.
</p>
<button onclick="gfg_Run()">
    Detect Arrow key
</button>
<p id="GFG_DOWN" style="color:green;
            font-size: 20px;
            font-weight: bold;">
</p>
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var str = 'No key pressed';
      
    function gfg_Run() {
        el_down.innerHTML = str;
    }
    document.body.addEventListener('keydown', function(event)
                                {
        const key = event.key;
        switch (key) {
            case "ArrowLeft":
                str = 'Left';
                break;
            case "ArrowRight":
                str = 'Right';
                break;
            case "ArrowUp":
                str = 'Up';
                break;
            case "ArrowDown":
                str = 'Down';
                break;
        }
    });
</script>


Output:

JavaScript Detecting the pressed arrow key

JavaScript Detecting the pressed arrow key

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS