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 What is onClickCapture Event in ReactJS? The onClickCapture event in React is part of Reactâs event handling system, and it is used to handle click events during the capture phase of event propagation. It is a variant of the standard onClick event, but it differs when it is triggered in the event lifecycle.In the event propagation model, e 5 min read What is onChangeCapture Event in ReactJS ? ReactJS, a popular JavaScript library for building user interfaces, provides a wide range of events that developers can utilize to create interactive and dynamic applications. One such event is the onChangeCapture event, which is specifically designed for handling changes to form inputs and capturin 2 min read What is onCutCapture Event in ReactJS ? React onCutCapture is an event handler that gets triggered whenever we cut a text. like oncut, but the difference is that onCutCapture acts in the capture phase whereas onCut acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReactJSSyntax: <input onCutCapture={functio 1 min read How to get text inside Text component onPress in ReactJS ? In this article, we will learn "How to get the text inside Text component onPress in ReactJS?". Problem Statement: Sometimes in an application, it is required to get the text inside certain components, in order to reuse the components again & again just by changing the text value. Approach: We w 4 min read React onKeyUp Event React onKeyUp is an event listener that is used to detect the key release event in a browser using JavaScript. This event occurs once after the key is pressed and released.It is similar to the HTML DOM onkeyup event but uses the camelCase convention in React.Syntax:<input onKeyUp={keyUpFunction}/ 2 min read Like