AndroidKeyboardEventsHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.ComponentModel;
  3. using Android.Content;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Views.InputMethods;
  7. using Avalonia.Android.Platform.Input;
  8. using Avalonia.Controls;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Raw;
  11. using Avalonia.Platform;
  12. namespace Avalonia.Android.Platform.Specific.Helpers
  13. {
  14. public class AndroidKeyboardEventsHelper<TView> : IDisposable where TView :ITopLevelImpl, IAndroidView
  15. {
  16. private TView _view;
  17. private IInputElement _lastFocusedElement;
  18. public bool HandleEvents { get; set; }
  19. public AndroidKeyboardEventsHelper(TView view)
  20. {
  21. this._view = view;
  22. HandleEvents = true;
  23. }
  24. public bool? DispatchKeyEvent(KeyEvent e, out bool callBase)
  25. {
  26. if (!HandleEvents)
  27. {
  28. callBase = true;
  29. return null;
  30. }
  31. return DispatchKeyEventInternal(e, out callBase);
  32. }
  33. private bool? DispatchKeyEventInternal(KeyEvent e, out bool callBase)
  34. {
  35. if (e.Action == KeyEventActions.Multiple)
  36. {
  37. callBase = true;
  38. return null;
  39. }
  40. var rawKeyEvent = new RawKeyEventArgs(
  41. AndroidKeyboardDevice.Instance,
  42. Convert.ToUInt32(e.EventTime),
  43. e.Action == KeyEventActions.Down ? RawKeyEventType.KeyDown : RawKeyEventType.KeyUp,
  44. AndroidKeyboardDevice.ConvertKey(e.KeyCode), GetModifierKeys(e));
  45. _view.Input(rawKeyEvent);
  46. if (e.Action == KeyEventActions.Down && e.UnicodeChar >= 32)
  47. {
  48. var rawTextEvent = new RawTextInputEventArgs(
  49. AndroidKeyboardDevice.Instance,
  50. Convert.ToUInt32(e.EventTime),
  51. Convert.ToChar(e.UnicodeChar).ToString()
  52. );
  53. _view.Input(rawTextEvent);
  54. }
  55. if (e.Action == KeyEventActions.Up)
  56. {
  57. //nothing to do here more call base no need of more events
  58. callBase = true;
  59. return null;
  60. }
  61. callBase = false;
  62. return false;
  63. }
  64. private static InputModifiers GetModifierKeys(KeyEvent e)
  65. {
  66. var rv = InputModifiers.None;
  67. if (e.IsCtrlPressed) rv |= InputModifiers.Control;
  68. if (e.IsShiftPressed) rv |= InputModifiers.Shift;
  69. return rv;
  70. }
  71. private bool NeedsKeyboard(IInputElement element)
  72. {
  73. //may be some other elements
  74. return element is TextBox;
  75. }
  76. private void TryShowHideKeyboard(IInputElement element, bool value)
  77. {
  78. var input = _view.View.Context.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>();
  79. if (value)
  80. {
  81. //show keyboard
  82. //may be in the future different keyboards support e.g. normal, only digits etc.
  83. //Android.Text.InputTypes
  84. input.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
  85. }
  86. else
  87. {
  88. //hide keyboard
  89. input.HideSoftInputFromWindow(_view.View.WindowToken, HideSoftInputFlags.None);
  90. }
  91. }
  92. public void UpdateKeyboardState(IInputElement element)
  93. {
  94. var focusedElement = element;
  95. bool oldValue = NeedsKeyboard(_lastFocusedElement);
  96. bool newValue = NeedsKeyboard(focusedElement);
  97. if (newValue != oldValue || newValue)
  98. {
  99. TryShowHideKeyboard(focusedElement, newValue);
  100. }
  101. _lastFocusedElement = element;
  102. }
  103. public void ActivateAutoShowKeyboard()
  104. {
  105. var kbDevice = (KeyboardDevice.Instance as INotifyPropertyChanged);
  106. //just in case we've called more than once the method
  107. kbDevice.PropertyChanged -= KeyboardDevice_PropertyChanged;
  108. kbDevice.PropertyChanged += KeyboardDevice_PropertyChanged;
  109. }
  110. private void KeyboardDevice_PropertyChanged(object sender, PropertyChangedEventArgs e)
  111. {
  112. if (e.PropertyName == nameof(KeyboardDevice.FocusedElement))
  113. {
  114. UpdateKeyboardState(KeyboardDevice.Instance.FocusedElement);
  115. }
  116. }
  117. public void Dispose()
  118. {
  119. HandleEvents = false;
  120. }
  121. }
  122. }