How to find out which Character Key is Pressed using JavaScript? Last Updated : 09 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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.ApproachAttach an event to the input box. like the onkeypress event.Call a function when that event happens and pass the event parameter in it.In the called function, identify the key pressed.Example 1: This example uses the approach defined above. html <!DOCTYPE HTML> <html> <head> <title> How to find out which character key is pressed using JavaScript? </title> </head> <body id="body" style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <form> Type Here: <input type="text" onkeypress="return GFG_Fun(event)" /> </form> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let up = document.getElementById('GFG_UP'); up.innerHTML = "Type in the Input box to see functioning."; let down = document.getElementById('GFG_DOWN'); function GFG_Fun(e) { let key; if (window.event) { key = e.keyCode; } else if (e.which) { key = e.which; } let str = down.innerHTML; str += String.fromCharCode(key); down.innerHTML = str; } </script> </body> </html> Output: Example 2: This example using the approach defined above. html <!DOCTYPE HTML> <html> <head> <title> How to find out which character key is pressed using JavaScript? </title> </head> <body id="body" style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <form> Type Here: <input type="text" onkeypress="return GFG_Fun(event)" /> </form> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let up = document.getElementById('GFG_UP'); up.innerHTML = "Type in the Input box to see functioning."; let down = document.getElementById('GFG_DOWN'); function GFG_Fun(e) { let str = down.innerHTML; str += e.key down.innerHTML = str; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to find out which Character Key is Pressed using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 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 Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 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 check whether the META key pressed when event is fired using jQuery ? jQuery is a feature-rich JavaScript library. It is a fast and most used JavaScript library. Before using jQuery you must have a basic knowledge of HTML, CSS, and JavaScript. In this article, we will learn about how you can check whether the META key was pressed when the event fired JQuery. META key: 2 min read Like