-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TextInput] Get keydown event to handle "Enter" #1882
Comments
Not sure if |
No luck with |
I would like to see |
Unfortunately I don't have enough skill to help. |
@brentvatne I think the way you emit events for your |
@JonasJonny I have a working implementation of Right now it is returning the character of the key that was pressed and for Would this take care of your use case? @brentvatne any other ideas as to how the I'd like to figure these things out before submitting a PR. |
Ah, it looks like there really isn't a |
Thank you @dsibiski for following. I really appreciate that. In my opinion you should return "keyCode" http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes |
Agreed with @JonasJonny - I think we should emulate the modern browser version of this though, rather than keyCode: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key - so it would return |
Thanks for the feedback guys! I pretty much have this ready for a PR, I'll make these changes and you guys can review it and see what you think... |
@brentvatne not sure how you want to handle issues w/ pending PRs. |
|
@browniefed - yeah it's a little awkward, maybe PR submitted tag would be good but that might be hard to keep up to date unless it's added automatically |
GitHub should build this if there isn't already a way to write this query. |
@ide +1 ... oh wait they haven't built a voting system yet ... not holding my breath. |
Anything I can do to help this task along? Pretty essential to native apps I believe. |
Summary: - When a key is pressed, it's `key value` is passed as an argument to the callback handler. - For `Enter` and `Backspace` keys, I'm using their `key value` as defined [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values). As per JonasJonny & brentvatne's [suggestion](#1882 (comment)). - Example ```javascript _handleKeyPress: function(e) { console.log(e.nativeEvent.key); }, render: function() { return ( <View style={styles.container}> <TextInput style={{width: 150, height: 25, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} /> <TextInput style={{width: 150, height: 100, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} multiline={true} /> </View> ); } ``` - Implements [shouldChangeCharactersInRange](https://developer.apple.com/library/prerelease/ios/documentat Closes #2082 Reviewed By: javache Differential Revision: D2280460 Pulled By: nicklockwood fb-gh-sync-id: 1f824f80649043dc2520c089e2531d428d799405
@brentvatne We can close this now as my PR for |
Awesome 😄 |
Summary: - When a key is pressed, it's `key value` is passed as an argument to the callback handler. - For `Enter` and `Backspace` keys, I'm using their `key value` as defined [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values). As per JonasJonny & brentvatne's [suggestion](facebook#1882 (comment)). - Example ```javascript _handleKeyPress: function(e) { console.log(e.nativeEvent.key); }, render: function() { return ( <View style={styles.container}> <TextInput style={{width: 150, height: 25, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} /> <TextInput style={{width: 150, height: 100, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} multiline={true} /> </View> ); } ``` - Implements [shouldChangeCharactersInRange](https://developer.apple.com/library/prerelease/ios/documentat Closes facebook#2082 Reviewed By: javache Differential Revision: D2280460 Pulled By: nicklockwood fb-gh-sync-id: 1f824f80649043dc2520c089e2531d428d799405
Summary: - When a key is pressed, it's `key value` is passed as an argument to the callback handler. - For `Enter` and `Backspace` keys, I'm using their `key value` as defined [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values). As per JonasJonny & brentvatne's [suggestion](facebook#1882 (comment)). - Example ```javascript _handleKeyPress: function(e) { console.log(e.nativeEvent.key); }, render: function() { return ( <View style={styles.container}> <TextInput style={{width: 150, height: 25, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} /> <TextInput style={{width: 150, height: 100, borderWidth: 0.5}} onKeyPress={this._handleKeyPress} multiline={true} /> </View> ); } ``` - Implements [shouldChangeCharactersInRange](https://developer.apple.com/library/prerelease/ios/documentat Closes facebook#2082 Reviewed By: javache Differential Revision: D2280460 Pulled By: nicklockwood fb-gh-sync-id: 1f824f80649043dc2520c089e2531d428d799405
what about android? is onKeyPress worked on android version? because it's not possible to make 2 versions of code only for "enter clicked". |
cc @dsibiski ^ |
Does Android version has support for onKeyPress ? Documentation suggests otherwise also I just tried and it's not working with version 0.26.2, but I just wanted to get this confirmed. |
@yfsx @Abhay-Joshi-Git: No, it hasn't yet been implemented for Android. If either of you want to give it a shot that would be awesome 👍 |
Any update for android. |
What the problem with #2082 ? |
Heads up: I'm taking a look at doing this on Android. |
Does anyone have any thoughts on the best way to approach this in terms of detecting the keypresses on ReactEditText? It seems that there might be a few approaches. TextWatcher KeyListener InputConnectionWrapper @janicduplessis I'm not sure if you can offer any insight :)? edit: It's nothing to do with the keyboard type being shown, it's to do with the keys themselves. onKeyDown/onKeyUp are only firing if the characters being pressed themselves are numeric. edit: Looks like I was completely wrong and you actually do need to do this with TextWatcher and potentially have some special cases for Tab, Return etc. Backspace (detected by InternalInputWrapper). |
Hi ! Any news on this ? |
Hi ! e~~~ |
Hi, you can use |
onSubmitEditing works great for Enter but how about listening for other input events like backspace? |
@joshyhargreaves very excited for this PR - however I've changed my entire project to work around this shortcoming already. |
Looking forward to see this in RN next version as well ! :) |
So a bit of an update on the PR. the tl:dr is that there are some limitations to the native APIs that stop us being able to have a completely 'flawless' |
@facebook-github-bot icebox |
@react-native-bot tells me to close this issue because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally! |
Summary: This implements onKeyPress for Android on TextInputs and addresses #1882. **N.B. that this PR has not yet addressed hardware keyboard inputs**, but doing will be fairly trivial. The main challenge was doing this for soft keyboard inputs. I've tried to match the style as much as I could. Will happily make any suggested edits be they architectural or stylistic design (edit: and of course implementation), but hopefully this is a good first pass :). I think important to test this on the most popular keyboard types; maybe different languages too. I have not yet added tests to test implementation, but will be happy to do that also. - Build & run RNTester project for Android and open TextInput. - Enter keys into 'event handling' TextInput. - Verify that keys you enter appear in onKeyPress below the text input - Test with autocorrect off, on same input and validate that results are the same. Below is a gif of PR in action. ![onkeypressandroid](https://user-images.githubusercontent.com/1807207/27512892-3f95c098-5949-11e7-9364-3ce9437f7bb9.gif) Closes #14720 Differential Revision: D6661592 Pulled By: hramos fbshipit-source-id: 5d53772dc2d127b002ea5fb84fa992934eb65a42
Summary: This implements onKeyPress for Android on TextInputs and addresses facebook/react-native#1882. **N.B. that this PR has not yet addressed hardware keyboard inputs**, but doing will be fairly trivial. The main challenge was doing this for soft keyboard inputs. I've tried to match the style as much as I could. Will happily make any suggested edits be they architectural or stylistic design (edit: and of course implementation), but hopefully this is a good first pass :). I think important to test this on the most popular keyboard types; maybe different languages too. I have not yet added tests to test implementation, but will be happy to do that also. - Build & run RNTester project for Android and open TextInput. - Enter keys into 'event handling' TextInput. - Verify that keys you enter appear in onKeyPress below the text input - Test with autocorrect off, on same input and validate that results are the same. Below is a gif of PR in action. ![onkeypressandroid](https://user-images.githubusercontent.com/1807207/27512892-3f95c098-5949-11e7-9364-3ce9437f7bb9.gif) Closes facebook/react-native#14720 Differential Revision: D6661592 Pulled By: hramos fbshipit-source-id: 5d53772dc2d127b002ea5fb84fa992934eb65a42
Summary: This implements onKeyPress for Android on TextInputs and addresses facebook#1882. **N.B. that this PR has not yet addressed hardware keyboard inputs**, but doing will be fairly trivial. The main challenge was doing this for soft keyboard inputs. I've tried to match the style as much as I could. Will happily make any suggested edits be they architectural or stylistic design (edit: and of course implementation), but hopefully this is a good first pass :). I think important to test this on the most popular keyboard types; maybe different languages too. I have not yet added tests to test implementation, but will be happy to do that also. - Build & run RNTester project for Android and open TextInput. - Enter keys into 'event handling' TextInput. - Verify that keys you enter appear in onKeyPress below the text input - Test with autocorrect off, on same input and validate that results are the same. Below is a gif of PR in action. ![onkeypressandroid](https://user-images.githubusercontent.com/1807207/27512892-3f95c098-5949-11e7-9364-3ce9437f7bb9.gif) Closes facebook#14720 Differential Revision: D6661592 Pulled By: hramos fbshipit-source-id: 5d53772dc2d127b002ea5fb84fa992934eb65a42
Summary: This implements onKeyPress for Android on TextInputs and addresses facebook#1882. **N.B. that this PR has not yet addressed hardware keyboard inputs**, but doing will be fairly trivial. The main challenge was doing this for soft keyboard inputs. I've tried to match the style as much as I could. Will happily make any suggested edits be they architectural or stylistic design (edit: and of course implementation), but hopefully this is a good first pass :). I think important to test this on the most popular keyboard types; maybe different languages too. I have not yet added tests to test implementation, but will be happy to do that also. - Build & run RNTester project for Android and open TextInput. - Enter keys into 'event handling' TextInput. - Verify that keys you enter appear in onKeyPress below the text input - Test with autocorrect off, on same input and validate that results are the same. Below is a gif of PR in action. ![onkeypressandroid](https://user-images.githubusercontent.com/1807207/27512892-3f95c098-5949-11e7-9364-3ce9437f7bb9.gif) Closes facebook#14720 Differential Revision: D6661592 Pulled By: hramos fbshipit-source-id: 5d53772dc2d127b002ea5fb84fa992934eb65a42
@joshyhargreaves, thanks for creating this PR. That's exact what I am looking for. |
Same question to @joshyhargreaves on the status on #14720 |
@gianpaj this landed a while ago! https://twitter.com/joshyhargreaves/status/949022712814555142 |
thanks great! I got confused because the docs mention it's only for I've opened a PR to update the docs: |
Hello.
I tried to catch keydown/keypress events for several hours with no luck.
I can get whole .text string (onChange={}) but what if I need to do an action when "Enter/Backspace" are pressed?
Is there any solution how to know which keyboard character was just pressed in TextInput keyboard?
Thank you.
The text was updated successfully, but these errors were encountered: