How to make EditText box selected when enter key is pressed #2078
-
I am trying to make the EditText box have focus and have a blinking caret when the enter key is pressed. I feel like this should be a basic fundamental task, but I can't seem to figure out how to make it happen programatically! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
In theory, the (simplified) snippet below should work:
That said, I did seem to have an issue specifically with the enter key because it is used to exit selection when the EditText is active and interacting with UI elements seem to take several frames so in the snippet above it enters selection just to immediately exit it on the next frame. You can try using a different key, or (if you're looking to use the Enter key regardless) a workaround I came up with for this one which is to basically introduce a general button timer in the script in Update() or any method below it.
Then, enforce the selection being active by using the timer:
I'm sure there's a better solution out there like perhaps setting the EditText as read-only to disable the Enter key behavior and then toggling read-only when the selection key is pressed but this one works for me even if a bit of a clunky patch. Let me know if this helps! |
Beta Was this translation helpful? Give feedback.
-
The reason it wasn't working for me was that I had the EditBox hidden prior to trying to make it active as well. As long as you run IsSelectionActive = true on the next frame after you mark it as visible, it works. |
Beta Was this translation helpful? Give feedback.
The reason it wasn't working for me was that I had the EditBox hidden prior to trying to make it active as well. As long as you run IsSelectionActive = true on the next frame after you mark it as visible, it works.