AvaloniaKeyboardDriver.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Ryujinx.Ava.Common.Locale;
  4. using Ryujinx.Input;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using AvaKey = Avalonia.Input.Key;
  9. using Key = Ryujinx.Input.Key;
  10. using TextInputEventArgs = OpenTK.Windowing.Common.TextInputEventArgs;
  11. namespace Ryujinx.Ava.Input
  12. {
  13. public class AvaloniaKeyboardDriver : IGamepadDriver
  14. {
  15. private static readonly string[] _keyboardIdentifers = new string[1] { "0" };
  16. private readonly Control _control;
  17. private readonly HashSet<AvaKey> _pressedKeys;
  18. public event EventHandler<KeyEventArgs> KeyPressed;
  19. public event EventHandler<KeyEventArgs> KeyRelease;
  20. public event EventHandler<TextInputEventArgs> TextInput;
  21. public string DriverName => "Avalonia";
  22. public ReadOnlySpan<string> GamepadsIds => _keyboardIdentifers;
  23. public AvaloniaKeyboardDriver(Control control)
  24. {
  25. _control = control;
  26. _pressedKeys = new HashSet<AvaKey>();
  27. _control.KeyDown += OnKeyPress;
  28. _control.KeyUp += OnKeyRelease;
  29. _control.TextInput += Control_TextInput;
  30. }
  31. private void Control_TextInput(object sender, Avalonia.Input.TextInputEventArgs e)
  32. {
  33. TextInput?.Invoke(this, new TextInputEventArgs(e.Text.First()));
  34. }
  35. public event Action<string> OnGamepadConnected
  36. {
  37. add { }
  38. remove { }
  39. }
  40. public event Action<string> OnGamepadDisconnected
  41. {
  42. add { }
  43. remove { }
  44. }
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. }
  49. public IGamepad GetGamepad(string id)
  50. {
  51. if (!_keyboardIdentifers[0].Equals(id))
  52. {
  53. return null;
  54. }
  55. return new AvaloniaKeyboard(this, _keyboardIdentifers[0], LocaleManager.Instance["AllKeyboards"]);
  56. }
  57. protected virtual void Dispose(bool disposing)
  58. {
  59. if (disposing)
  60. {
  61. _control.KeyUp -= OnKeyPress;
  62. _control.KeyDown -= OnKeyRelease;
  63. }
  64. }
  65. protected void OnKeyPress(object sender, KeyEventArgs args)
  66. {
  67. AvaKey key = args.Key;
  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. AvaloniaMappingHelper.TryGetAvaKey(key, out var nativeKey);
  83. return _pressedKeys.Contains(nativeKey);
  84. }
  85. public void ResetKeys()
  86. {
  87. _pressedKeys.Clear();
  88. }
  89. }
  90. }