UWP, input fields and focus: How can I automatically type in an input field in a UWP app without any use of a mouse?
Hi,
I am creating a mouseless game which involves the player typing into an input field which is automatically put into focus as it appears via these 2 lines:
EventSystem.current.SetSelectedGameObject(this.gameObject, null);
GetComponent<InputField>().OnPointerClick(new PointerEventData(EventSystem.current));
I found this to work perfectly in Windows and Mac builds and in the editor, but when I tested UWP, something was not working right. When the input field is created, I couldn't type into the input field like I could in Windows/Mac build despite it being in focus. I know it is in focus because the caret is activated (it wouldn't be if input field is not in focus, I've tested). The interesting thing is if I click with my mouse anywhere on the window of the running app, or if I go to another window (say my web browser) and go back to the app via taskbar, alt+tab or just clicking into it, I am then able to type into the input field (in that instance and every other instance following without clicking anything, as long as I don't close and reload the app).
I have tested both from Visual Studio and via an installed sideload app and both result with this same problem. With the game designed to be played without a mouse, having to click the window to type (even just once) defeats the the whole purpose of what I'm trying to do, so having this seemingly tiny problem be such a major roadblock has been super dismaying. It's also worth noting that the game receives all my other inputs, e.g. navigating with arrows, escape to pause, etc. It is only when it has to do with an input field in a UWP app and only for the first time in a session.
Does anyone have any idea as to what's happening? Any way to fix this or delve deeper into the problem? Does it have anything to do with focus of the UWP app window with the whole having to click the window first? I'm thinking it's plausible, so I've been trying to find a way to force focus on the window without luck; any pointers here? I'm a bit of a greenhorn in programming and making UWP apps, so any help would be great help, thanks!
Unfortunately I was not able to find a fix for this, but I applied a hacky workaround that others who encounter this very circumstantial problem might find acceptable.
I ended up starting the game with a screen that prompts the player to click on the screen to start the game. This means the player always has clicked the screen when they try to input an answer and thus will not have issues inputting. I also decided to detail concisely but precisely that this was the only time a mouse was needed (because of the nature of my game).
This is preferable but not ideal. So if anyone has a fix, please do post it here still.
Answer by Kevin1970 · Oct 21, 2021 at 08:06 PM
You might try checking the "force module active" on the standard input module component of your EventSystem object.
Your answer
Follow this Question
Related Questions
Hide the curser the moment i press play not having to press the play window 0 Answers
How to change caret position inside Input field using mouse position 0 Answers
Prevent InputField from being focused on Start (Text Adventure Game Part 1) 1 Answer
Can't keep the InputField focused 1 Answer
Why is my MouseLook script not letting me look up? 0 Answers