question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onKeyPress and onKeyDown not working

See original GitHub issue

Hello, Im having a problem with onKeyPress and onKeyDown. I did create a component called Input, and have this line:

<MaskedInput id="message" mask={this.props.mask} type={this.props.type} className="inpt inpt-default" placeholder={this.state.text} onBlur={this.onBlur} onFocus={this.onFocus} name={this.props.name} onChange={this.onChange} onKeyPress={this.props.onKeyPress} value={this.state.message}/>

And Im calling the Input component like this:

<Input ref="input" type="phone" name="phone" mask="(11) 11111 1111" onClick={this.inputOnClick} onKeyPress={this.handleKeyPress}/>

handleKeyPress(event){
     if(event.key === "Enter"){
      this.processUserInput(event.target.name);
     }

}

I know that my handleKeyPress function is working for two reasons, first, is working at other regular inputs, second, I had replaced use onKeyPress={this.props.onKeyPress} for onKeyUp={this.props.onKeyPress} and worked.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

7reactions
Byron2016commented, Jun 22, 2018

You can find the solution here:

https://gist.github.com/krambertech/76afec49d7508e89e028fce14894724c

handleKeyPress(event){ if(event.key === "13"){ this.processUserInput(event.target.name); } }

<Input ref="input" type="phone" name="phone" mask="(11) 11111 1111" onClick={this.inputOnClick} onKeyPress={(e) => this.handleKeyPress(e)}/>

2reactions
cooperjonescommented, Jun 20, 2018

Similar issue here. onKeyDown and onKeyUp both work, but onKeyPress in the same context doesn’t function. Would love to have a fix for this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

onKeyDown event not working on divs in React - Stack Overflow
You should use tabIndex attribute to be able to listen onKeyDown event on a div in React. Setting tabIndex="0" should fire your handler....
Read more >
How to use onKeyPress event in ReactJS? - GeeksforGeeks
The onKeyPress event in ReactJS occurs when the user presses a key on the keyboard but it is not fired for all keys...
Read more >
onkeypress and onkeydown not working in Mozilla - MSDN
Hi,. I have a javascript function called in keypress event. The event works in IE but not in Mozilla. ... How to make...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
keydown keyup. Focus on the input field and press a key. ... On the other hand, there's a problem with event.code .
Read more >
onKeyDown is not working for me. : r/reactjs - Reddit
Add tabIndex={0} to your div. Normally div doesn't accept input, so it doesn't receive focus for keyboard input. Setting a tab index changes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found