DEV Community

vedant-jain03
vedant-jain03

Posted on

3 2

Add key event listener in React.

It is very simple in react, you can add any key event listener. I have used example of "Enter".

I have defined a sample function for the explaination.

    const handleInputEnter = (e) => {
        // You can 
        if(e.code === 'Enter') {
            console.log("Hello");
        }
    }
Enter fullscreen mode Exit fullscreen mode

Add onKeyUp attribute in the element.
Example:

<input type="text" onKeyUp={handleInputEnter}  />
Enter fullscreen mode Exit fullscreen mode

This will work completely fine.
Follow me on twitter: https://twitter.com/vedantj_03

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay