How to Detect Keypress using JavaScript ? Last Updated : 05 Aug, 2025 Comments Improve Suggest changes 3 Likes Like Report 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 Sheet". Cascading style sheets are used to design web page layouts. The styles are defined to give styles to tables, sizes, and texts. JavaScript is a scripting programming language used on the client and server side, which makes the web pages talk and communicate with each other. All of the above technologies are used to implement keypress detection. Program editor applications like Atom or Sublime Text can be used to compile the programs.Example: In this example, we will create a program to detect keypresses using JavaScript.keypresses HTML <!DOCTYPE html> <html> <head> <style> body { font-family: monospace, arial; font-size: 38px; text-align: center; } </style> </head> <body> <div id="demo"></div> <script> window.onload = function() { var demo = document.getElementById('demo'); var value = 0; var space_bar = 32; var right_arrow = 39; window.onkeydown = function(gfg) { if (gfg.keyCode === space_bar) { value++; demo.innerHTML = value; } if (gfg.keyCode === right_arrow) { alert("Welcome to GeeksForGeeks!"); } }; }; </script> </body> </html> Here,var space_bar = 32 andvar right_arrow = 39 are actually the key codes that correspond to the specific key. Whenever the space bar or the right arrow is clicked, the HTML will detect the type of click and respond by the number of times clicked or by a message.Output: A number gets displayed when the space bar is once clicked. When the right arrow is clicked, the page responds with a message. Create Quiz Comment P polokghosh53 Follow 3 Improve P polokghosh53 Follow 3 Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like