I'm simply trying to receive the characters that the users press when using the Android's system keyboard. The OSK displays fine, but I cannot receive the characters entered by the user. I've tried two different methods (the only two I can find).
The first method is using the View
object within the Activity
class as demonstrated here. I could not get it to work so I kept searching and trying different things and nothing worked.
My Activity
class
[Activity(Label = "Game1"
, MainLauncher = true
, Icon = "@drawable/icon"
, Theme = "@style/Theme.Splash"
, AlwaysRetainTaskState = true
, LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
, ScreenOrientation = ScreenOrientation.Portrait
, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
View pView = null;
Game1 game = null;
IAsyncResult ar = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
game = new Game1();
pView = (View)game.Services.GetService(typeof(View));
SetContentView(pView);
game.Run();
}
public void ShowKeyboard()
{
TakeKeyEvents(true);
InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.ShowSoftInput(pView, ShowFlags.Forced);
inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
}
public void HideKeyboard()
{
InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.HideSoftInputFromWindow(pView.WindowToken, HideSoftInputFlags.None);
TakeKeyEvents(false);
}
public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
return base.OnKeyUp(keyCode, e); // Never gets called
}
private void OnKeyPress(object sender, View.KeyEventArgs e)
{
// Never gets called
}
}
The second method uses the Guide.BeginShowKeyboardInput()
method that comes with XNA. It displays a textbox with a keyboard. When you hit the accept button there is an AsyncCallback that gets called. Inside of that callback I'm trying to figure out how to get the text that the user typed, but can't figure out how. I can't find any useful documentation on actually getting the value from the Soft Keyboard here.
Method 2 implementation
void KeyboardCallback(IAsyncResult Result)
{
System.Runtime.Remoting.Messaging.AsyncResult res = Result as System.Runtime.Remoting.Messaging.AsyncResult;
}
void CreateButtonClicked(Panel panel)
{
ar = Guide.BeginShowKeyboardInput(PlayerIndex.One, "Game1", "Enter a username", "", KeyboardCallback, obj);
}
ar
is an IAsyncResult
object that is returned by the show keyboard method. I've tried to look at the values in the IAsyncResult objects to find the text I enter into the keyboard, but I don't see anything.
I know I am probably too late but this is what you need to do:
void KeyboardCallback(IAsyncResult Result)
{
var text = Guide.EndKeyboardInput(result);
//text now contains your keyboard input
}
void CreateButtonClicked(Panel panel)
{
ar = Guide.BeginShowKeyboardInput(PlayerIndex.One, "Game1", "Enter a username", "", KeyboardCallback, null);
}
Hope that helps ...