I'm developing a game using Xamarin/Monogame and I need to open the keyboard on a mobile device when they click on my input control. I recognize that I can capture input using Keyboard.GetState()
when I'm using the emulator and my keyboard, but real users will have physical devices, and I need to open the on-screen keyboard for them to enter in information. I don't see anything in any documentation describing this, but it's hard to believe I'm the first person to run into this. I've looked through Xamarin/Monogame's docs on input and I've read this StackOverflow article. Let me know if you need more information to help solve this. Thanks in advance for any help.
相关问题
- Sorting 3 numbers without branching [closed]
- How can I create this custom Bottom Navigation on
- Graphics.DrawImage() - Throws out of memory except
- Bottom Navigation View gets Shrink Down
- Why am I getting UnauthorizedAccessException on th
After much digging I was able to figure out the solution. I'll first start by explaining how I got there: I stopped thinking "how do I display the on-screen keyboard in Mono?" and started thinking "how do you display the keyboard on an Android device?" This lead me to this article which was perfect; Xamarin and Android. Here's the relevant snippets:
Showing the soft input (on-screen keyboard)
Some key notes about this:
View
class isAndroid.Views.View
InputMethodManager
class isAndroid.Views.InputMethods.InputMethodManager
Activity
class, soApplication
refers to a property on that class (this.Application
)game
is your game class (in Mono) that is of typeMicrosoft.Xna.Framework.Game
Hiding the soft input (on-screen keyboard)
After I solved this, the way I was capturing input (the XNA/Mono way) was not capturing this input. So I had to continue digging. While it doesn't necessarily pertain to this question, I'd like to post it here so it exists somewhere. This documentation on the Android Developers site helped me.
To capture input directly (which is what we need to do to capture to OSK input), you need to override the
OnKeyPress
method on the activity your game is running in. For me, this looks as follows:Hope this helps anyone else who got stuck with this; the information was hard to find, but now that I've changed my frame of mind to "how does Android/iOS handle this thing" I've been able to locate answers more easily. Good luck!