DummyEdit.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using Android.Content;
  3. using Android.Runtime;
  4. using Android.Util;
  5. using Android.Views;
  6. using Android.Views.InputMethods;
  7. namespace Urho.Android
  8. {
  9. public class DummyEdit : View, View.IOnKeyListener
  10. {
  11. private SDLInputConnection ic;
  12. protected DummyEdit(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
  13. {
  14. }
  15. public DummyEdit(Context context) : base(context)
  16. {
  17. FocusableInTouchMode = true;
  18. Focusable = true;
  19. SetOnKeyListener(this);
  20. }
  21. public override bool OnCheckIsTextEditor()
  22. {
  23. return true;
  24. }
  25. public bool OnKey(View v, Keycode keyCode, KeyEvent e)
  26. {
  27. // This handles the hardware keyboard input
  28. if (e.IsPrintingKey) {
  29. if (e.Action == KeyEventActions.Down)
  30. {
  31. ic.CommitText(Java.Lang.String.ValueOf(e.GetUnicodeChar(0)), 1);
  32. }
  33. return true;
  34. }
  35. if (e.Action == KeyEventActions.Down)
  36. {
  37. SDLActivity.onNativeKeyDown(keyCode);
  38. return true;
  39. }
  40. else if (e.Action == KeyEventActions.Up)
  41. {
  42. SDLActivity.onNativeKeyUp(keyCode);
  43. return true;
  44. }
  45. return false;
  46. }
  47. public override bool OnKeyPreIme(Keycode keyCode, KeyEvent e)
  48. {
  49. // As seen on StackOverflow: http://stackoverflow.com/questions/7634346/keyboard-hide-event
  50. // FIXME: Discussion at http://bugzilla.libsdl.org/show_bug.cgi?id=1639
  51. // FIXME: This is not a 100% effective solution to the problem of detecting if the keyboard is showing or not
  52. // FIXME: A more effective solution would be to change our Layout from AbsoluteLayout to Relative or Linear
  53. // FIXME: And determine the keyboard presence doing this: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
  54. // FIXME: An even more effective way would be if Android provided this out of the box, but where would the fun be in that :)
  55. if (e.Action == KeyEventActions.Up && keyCode == Keycode.Back)
  56. {
  57. if (SDLActivity.mTextEdit != null && SDLActivity.mTextEdit.Visibility == ViewStates.Visible)
  58. {
  59. SDLActivity.onNativeKeyboardFocusLost();
  60. }
  61. }
  62. return base.OnKeyPreIme(keyCode, e);
  63. }
  64. public override IInputConnection OnCreateInputConnection(EditorInfo outAttrs)
  65. {
  66. ic = new SDLInputConnection(this, true);
  67. outAttrs.ImeOptions = ImeFlags.NoExtractUi | (ImeFlags)33554432 /* API 11: EditorInfo.IME_FLAG_NO_FULLSCREEN */;
  68. return base.OnCreateInputConnection(outAttrs);
  69. }
  70. }
  71. }