AvaloniaKeyboardDriver.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Interactivity;
  4. using Ryujinx.Ava.Common.Locale;
  5. using Ryujinx.Input;
  6. using System;
  7. using System.Collections.Generic;
  8. using AvaKey = Avalonia.Input.Key;
  9. using Key = Ryujinx.Input.Key;
  10. namespace Ryujinx.Ava.Input
  11. {
  12. internal class AvaloniaKeyboardDriver : IGamepadDriver
  13. {
  14. private static readonly string[] _keyboardIdentifers = new string[1] { "0" };
  15. private readonly Control _control;
  16. private readonly HashSet<AvaKey> _pressedKeys;
  17. public event EventHandler<KeyEventArgs> KeyPressed;
  18. public event EventHandler<KeyEventArgs> KeyRelease;
  19. public event EventHandler<string> TextInput;
  20. public string DriverName => "AvaloniaKeyboardDriver";
  21. public ReadOnlySpan<string> GamepadsIds => _keyboardIdentifers;
  22. public AvaloniaKeyboardDriver(Control control)
  23. {
  24. _control = control;
  25. _pressedKeys = new HashSet<AvaKey>();
  26. _control.KeyDown += OnKeyPress;
  27. _control.KeyUp += OnKeyRelease;
  28. _control.TextInput += Control_TextInput;
  29. _control.AddHandler(InputElement.TextInputEvent, Control_LastChanceTextInput, RoutingStrategies.Bubble);
  30. }
  31. private void Control_TextInput(object sender, TextInputEventArgs e)
  32. {
  33. TextInput?.Invoke(this, e.Text);
  34. }
  35. private void Control_LastChanceTextInput(object sender, TextInputEventArgs e)
  36. {
  37. // Swallow event
  38. e.Handled = true;
  39. }
  40. public event Action<string> OnGamepadConnected
  41. {
  42. add { }
  43. remove { }
  44. }
  45. public event Action<string> OnGamepadDisconnected
  46. {
  47. add { }
  48. remove { }
  49. }
  50. public IGamepad GetGamepad(string id)
  51. {
  52. if (!_keyboardIdentifers[0].Equals(id))
  53. {
  54. return null;
  55. }
  56. return new AvaloniaKeyboard(this, _keyboardIdentifers[0], LocaleManager.Instance[LocaleKeys.AllKeyboards]);
  57. }
  58. protected virtual void Dispose(bool disposing)
  59. {
  60. if (disposing)
  61. {
  62. _control.KeyUp -= OnKeyPress;
  63. _control.KeyDown -= OnKeyRelease;
  64. }
  65. }
  66. protected void OnKeyPress(object sender, KeyEventArgs args)
  67. {
  68. _pressedKeys.Add(args.Key);
  69. KeyPressed?.Invoke(this, args);
  70. }
  71. protected void OnKeyRelease(object sender, KeyEventArgs args)
  72. {
  73. _pressedKeys.Remove(args.Key);
  74. KeyRelease?.Invoke(this, args);
  75. }
  76. internal bool IsPressed(Key key)
  77. {
  78. if (key == Key.Unbound || key == Key.Unknown)
  79. {
  80. return false;
  81. }
  82. AvaloniaKeyboardMappingHelper.TryGetAvaKey(key, out var nativeKey);
  83. return _pressedKeys.Contains(nativeKey);
  84. }
  85. public void ResetKeys()
  86. {
  87. _pressedKeys.Clear();
  88. }
  89. public void Dispose()
  90. {
  91. Dispose(true);
  92. }
  93. }
  94. }