Using Android virtual keyboard in MonoGame

So I guess this is more a Xamarin thing but I was wondering if perhaps this is already covered by MonoGame inputs. I have managed to throw up a virtual keyboard pretty easily, but its for an age gate and I’m now stuck switching it to be numeric only.

Anyone got any experience with this?

Heres the code, might be useful for anyone else doing the same.

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var g = Game.Instance;
            view = (View)g.Services.GetService(typeof(View));

            SetContentView(view);
            g.Run();
            inputMethodManager = Application.GetSystemService(InputMethodService) as InputMethodManager;
            view.KeyPress += OnKeyPress;

            editText = new EditText(this);
            editText.InputType = Android.Text.InputTypes.ClassNumber;

            g.ShowKeyboardAction = () => ShowKeyboard();
            g.HideKeyboardAction = () => HideKeyboard();
        }

        private void ShowKeyboard()
        {
            inputMethodManager.ShowSoftInput(view, ShowFlags.Forced);
            inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
        }

        private void HideKeyboard()
        {
            inputMethodManager.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.None);
        }

        private void OnKeyPress(object sender, View.KeyEventArgs e)
        {
            // do stuff with the input
            System.Diagnostics.Debug.WriteLine(e.KeyCode);
        }
1 Like