How to Use onKeyPress Event in ReactJS? Last Updated : 18 Sep, 2024 Comments Improve Suggest changes Like Article Like Report The onKeyPress event in ReactJS occurs when the user presses a key on the keyboard but it is not fired for all keys e.g. ALT, CTRL, SHIFT, ESC in all browsers.ApproachTo use the onKeyPress event in React we will use the predefined onKeyPress event prop. We will be using a text input field, and pass an event handler in the onKeyPress prop and console log the pressed key using event.key property. Steps to Create React ApplicationStep 1: Create a React application using the following commandnpx create-react-app functiondemoStep 2: After creating your project folder i.e. functiondemo, move to it using the following commandcd functiondemoProject Structure:It will look like the following.Project StructureExample: In this example, we are going to build an application that displays the key pressed in the input box. JavaScript // Filename - App.js import React, { useState } from "react"; const App = () => { const [state, setState] = useState(""); const handler = (event) => { // changing the state to the name of the key // which is pressed setState(event.key); }; return ( <div> <h1>Hi Geeks!</h1> <p>Key pressed is: {state}</p> {/* Passing the key pressed to the handler function */} <input type="text" onKeyPress={(e) => handler(e)} /> </div> ); }; export default App; Note: You can define your own styling in the App.css file.Step to Run Application: Run the application using the following command from the root directory of the project:npm startOutput:Â ConclusionTo use the onKeyPress event create a hander funtion and use it with the onKeyPress prop. Console log event.key in the handler to print which key has been pressed. Comment More infoAdvertise with us Next Article How to Use onKeyPress Event in ReactJS? rbbansal Follow Improve Article Tags : Web Technologies ReactJS Similar Reads How to use events in ReactJS ? Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m 2 min read React onKeyPress Event React onKeyPress event is an event listener that is used to detect the key press in a browser. onKeyPress is now deprecated because it does not work for all keys (like CTRL, SHIFT, and ALT) in all browsers, so onKeyDown is a new event listener that is used to detect the key press event. It is simila 2 min read How to get the enter key in ReactJS ? Let us create a React project and then we will create a UI that takes input from users. Users can interact with the UI and press Enter Key to trigger an event through this. Approach:Enter Key in React JS comes in the KeyDown Event. To get the enter key we check the event to verify which key is press 2 min read What is onCopyCapture Event in ReactJS ? React onCopyCapture is an event handler that gets triggered whenever we copy the text on the webpage. like onCopy, but the difference is that onCopyCapture acts in the capture phase whereas onBlur acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReact JSPhases of JavaSc 2 min read What is onPasteCapture Event in ReactJS ? React onPasteCapture is an event handler that gets triggered whenever we paste some text in an input field. like onpaste, but the difference is that onPasteCapture acts in the capture phase whereas onPaste acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReact JSPhases 1 min read Like