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.

[0.54] [iOS] Wrong 'Backspace' event fired onKeyPress after clearing TextInput

See original GitHub issue

In IOS, after clearing TextInput value (setting it to empty string) and then pressing any key (e.g. ‘a’) two onKeyPress events are fired: A, Backspace. First is for the pressed key and second is a Backspace.

Environment

Environment: OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: 1.3.2 npm: 5.5.1 Watchman: 4.9.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed) react: ^16.3.0-alpha.1 => 16.3.0-alpha.1 react-native: 0.54.2 => 0.54.2

Expected Behavior

Only one event for the pressed key should be fired (e. g. ‘A’).

Actual Behavior

An additional ‘Backspace’ event is fired.

Steps to Reproduce

  1. write anythig in the textfield
  2. press clear
  3. press ‘a’ >>> events ‘A’ and ‘Backspace’ will be fired
type Props = {};
type State = {
  text: string,
  keys: string
};
export default class App extends Component<Props, State> {
  state = {text: '', keys: ''}
  render() {
    return (
      <View style={styles.container}>
        <TextInput style={styles.textInput} value={this.state.text} onChangeText={this.onChangeText} onKeyPress={this.onKeyPress}/>
        <Button title="Clear" onPress={this.onClear}/>
        <Text>Text: {this.state.text}</Text>
        <Text>Keys: {this.state.keys}</Text>
      </View>
    );
  }

  onChangeText = (text: string) => {
    this.setState({text})
  }

  onKeyPress = ({ nativeEvent }: Object) => {
    this.setState({keys: this.state.keys + nativeEvent.key + ', '})
  }

  onClear = () => {
    this.setState({text: '', keys: ''})
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

29reactions
amarwcommented, May 18, 2018

I was able to workaround this bug by adding a check in the onKeyPress callback function,

 onKeyPress(e) {
    if (e.nativeEvent.key === 'Backspace') {
      // Return if duration between previous key press and backspace is less than 20ms
      if (Math.abs(this.lastKeyEventTimestamp - e.timeStamp) < 20) return;

      // Your code here
 
    } else {
      // Record non-backspace key event time stamp
      this.lastKeyEventTimestamp = e.timeStamp;
    }
  }

I found while debugging that duration between receiving backspace event after key press is less than ~10ms. So I record the non-backspace key event timeStamp and use it to compare with backspace event timeStamp. If the difference between these two is less than 20ms (arbitrary value > 10ms) then I return from the callback. Hope this helps!

8reactions
hamaroncommented, Mar 30, 2018

I opened a pull request to fix this bug: #18627

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to detect in javascript that the Backspace is long ...
Try the following: onKeydown(key, event) { let input = document. ... and no text after the cursor position; focus the next field if...
Read more >
Previous Textinput Autofocus On Backspace Press React ...
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character...
Read more >
ClientEvents OnKeyPress + Backspace/Delete - Telerik
Hi Mike, Delete, backspace and other special keys do not fire the KeyPress event in IE. What you can do is use the...
Read more >
MathLive Changelog - CortexJS
#1572 Keyboard events ( keyup , keydown , keypress ) are now fired more ... Long press on the backspace key is now...
Read more >
TextInput - React Native Archive
There are also other events, such as onSubmitEditing and onFocus that can be subscribed to. A minimal example: import React, { Component }...
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