How to make a key fire when key is pressed using JavaScript ? Last Updated : 24 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 the current key status: A flag variable is defined which keeps track of the current key press. The 'keyup' and 'keydown' events are both set to modify this flag variable so that it correctly reflects the current status of the key press. The 'keydown' event only allows for the respective event to fire if the key is not already held down by checking the flag variable. The 'keyup' event on the other hand sets the flag variable to indicate that the key has been released. Using both these with event listeners, one can make sure that the key fires only once even if it is being held down. Syntax: let isPressed = false; element.onkeydown = function (e) { if (!isPressed) { isPressed = true; console.log('Key Fired!'); } }; element.onkeyup = function (e) { isPressed = false; } Example: html <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to make a key fire only once when pressed? </b> <p> Press any button and observe the logs to verify that a key only fires once. </p> <input type="text"> <script type="text/javascript"> let element = document.querySelector('input'); let isPressed = false; element.onkeydown = function (e) { if (!isPressed) { isPressed = true; console.log('Key Fired!'); } }; element.onkeyup = function (e) { isPressed = false; } </script> </body> Output: Method 2: Using the repeat property: The 'repeat' property of the KeyboardEvent interface is used to check if a key is getting repeated as a result of being held down by the user. This property can be checked on every 'keydown' event and allow the specified event to only fire if it returns false. This prevents the key from firing multiple times even if the user holds the key down. Syntax: element.onkeydown = function (e) { if (!e.repeat) { console.log("Key Fired!"); } }; Example: html <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to make a key fire only once when pressed? </b> <p> Press any button and observe the logs to verify that a key only fires once. </p> <input type="text"> <script type="text/javascript"> let element = document.querySelector('input'); element.onkeydown = function (e) { if (!e.repeat) { console.log("Key Fired!"); } }; </script> </body> Output: Comment More infoAdvertise with us Next Article How to make a key fire when key is pressed using JavaScript ? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 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 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 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 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 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:ob 2 min read Like