Open On-Screen Keyboard for Xamarin/Monogame

江枫思渺然 提交于 2019-11-30 04:56:46

问题


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.


回答1:


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)

var pView = game.Services.GetService<View>();
var inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.ShowSoftInput(pView, ShowFlags.Forced);
inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);

Some key notes about this:

  • The View class is Android.Views.View
  • The InputMethodManager class is Android.Views.InputMethods.InputMethodManager
  • This code is called from within an Activity class, so Application refers to a property on that class (this.Application)
  • game is your game class (in Mono) that is of type Microsoft.Xna.Framework.Game

Hiding the soft input (on-screen keyboard)

InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.HideSoftInputFromWindow(pView.WindowToken, HideSoftInputFlags.None);

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:

public class AndroidActivity : AndroidGameActivity
{
    private void OnKeyPress(object sender, View.KeyEventArgs e)
    {
        // do stuff with the input
    }
}

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!



来源:https://stackoverflow.com/questions/38810168/open-on-screen-keyboard-for-xamarin-monogame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!