JavaScript Detecting the pressed arrow key Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Sometimes we need to detect the keys and sometimes even detect which keys were pressed. To detect which arrow key is pressed we can use JavaScript onkeydown event. Detecting the pressed arrow key using onkeydown EventThe DOM onkeydown Event in HTML occurs when a key is pressed by the user. Syntax:object.addEventListener("keydown", myScript);Example 1: This example detects the arrow key by onkeydown Event by using event.keyCode. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <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> let el_up = document.getElementById("GFG_UP"); let el_down = document.getElementById("GFG_DOWN"); let 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> </body> </html> Output: JavaScript Detecting the pressed arrow keyExample 2: This example detects the arrow key by adding a eventListener(keydown) to the body by using event.key. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <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> let el_up = document.getElementById("GFG_UP"); let el_down = document.getElementById("GFG_DOWN"); let 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> </body> </html> Output: JavaScript Detecting the pressed arrow key Comment More infoAdvertise with us Next Article JavaScript Detecting the pressed arrow key P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Detect Keypress using JavaScript ? In this article, keyboard detection is performed using HTML and CSS. HTML stands for "Hypertext Markup Language". HTML language helps the developer to create and design web page elements like links, sections, paragraphs, headings, and blockquotes for web applications. CSS stands for "Cascading Style 2 min read How to make a key fire when key is pressed using JavaScript ? In JavaScript, holding down a key will fire the key continuously until it is released. This behavior may be used in applications such as games where a key may be expected to fire only once even after being held down. This can be achieved using two methods: Method 1: Using a flag variable to check th 3 min read How to disable arrow key in textarea using JavaScript ? Given an HTML element containing the <textarea> element and the task is to disable scrolling through arrow keys with the help of JavaScript. Approach 1: Add an event listener onkeydown on the window.If the event happens then check if the keys are arrow or not.If arrow key is pressed then preve 3 min read How to find out which Character Key is Pressed using JavaScript? To find which key is pressed in JavaScript, use event listeners like keydown, keypress, or keyup. By accessing properties such as event.key or event.code, it becomes easy to identify the specific key pressed. This approach is useful for handling user input and triggering actions based on key events. 2 min read JavaScript onKeyPress onKeyUp and onKeyDown Events Whenever a key is pressed or released, there are certain events that are triggered. Each of these events has a different meaning and can be used for implementing certain functionalities depending on the current state and the key that is being used. These events that are triggered when a key is press 2 min read Like