mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
051d981a01
@ -21,7 +21,7 @@ namespace Artemis.Core.Services
|
||||
public ArtemisDevice? Device { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the LED that corresponds to the pressed key
|
||||
/// Gets the LED that corresponds to the pressed key
|
||||
/// </summary>
|
||||
public ArtemisLed? Led { get; }
|
||||
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains data for keyboard input events
|
||||
/// </summary>
|
||||
public class ArtemisKeyboardToggleStatusArgs : EventArgs
|
||||
{
|
||||
internal ArtemisKeyboardToggleStatusArgs(KeyboardToggleStatus oldStatus, KeyboardToggleStatus newStatus)
|
||||
{
|
||||
OldStatus = oldStatus;
|
||||
NewStatus = newStatus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the keyboard status before the change
|
||||
/// </summary>
|
||||
public KeyboardToggleStatus OldStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the keyboard status after the change
|
||||
/// </summary>
|
||||
public KeyboardToggleStatus NewStatus { get; }
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,7 @@ namespace Artemis.Core.Services
|
||||
public class InputProviderKeyboardEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="InputProviderKeyboardEventArgs " /> class
|
||||
/// </summary>
|
||||
/// <param name="device">The device that triggered the event</param>
|
||||
/// <param name="key">The key that triggered the event</param>
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains data for input provider keyboard status toggle events
|
||||
/// </summary>
|
||||
public class InputProviderKeyboardToggleEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="InputProviderKeyboardToggleEventArgs " /> class
|
||||
/// </summary>
|
||||
/// <param name="keyboardToggleStatus">The toggle status of the keyboard</param>
|
||||
public InputProviderKeyboardToggleEventArgs(KeyboardToggleStatus keyboardToggleStatus)
|
||||
{
|
||||
KeyboardToggleStatus = keyboardToggleStatus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the toggle status of the keyboard
|
||||
/// </summary>
|
||||
public KeyboardToggleStatus KeyboardToggleStatus { get; }
|
||||
}
|
||||
}
|
||||
@ -8,11 +8,23 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
public abstract class InputProvider : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the input service requests a <see cref="KeyboardToggleStatusReceived"/> event
|
||||
/// </summary>
|
||||
public virtual void OnKeyboardToggleStatusRequested()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the input provided has received keyboard data
|
||||
/// </summary>
|
||||
public event EventHandler<InputProviderKeyboardEventArgs>? KeyboardDataReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the input provider has received new toggle data for keyboards
|
||||
/// </summary>
|
||||
public event EventHandler<InputProviderKeyboardToggleEventArgs>? KeyboardToggleStatusReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the input provided has received mouse button data
|
||||
/// </summary>
|
||||
@ -95,6 +107,17 @@ namespace Artemis.Core.Services
|
||||
IdentifierReceived?.Invoke(this, new InputProviderIdentifierEventArgs(identifier, deviceType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the <see cref="KeyboardToggleStatusReceived" /> event which the <see cref="IInputService" /> listens to as
|
||||
/// long as
|
||||
/// this provider is registered
|
||||
/// </summary>
|
||||
/// <param name="keyboardToggleStatus">The toggle status of the keyboard</param>
|
||||
protected virtual void OnKeyboardToggleStatusReceived(KeyboardToggleStatus keyboardToggleStatus)
|
||||
{
|
||||
KeyboardToggleStatusReceived?.Invoke(this, new InputProviderKeyboardToggleEventArgs(keyboardToggleStatus));
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -36,16 +36,22 @@ namespace Artemis.Core.Services
|
||||
|
||||
private readonly List<InputProvider> _inputProviders = new();
|
||||
|
||||
public KeyboardToggleStatus KeyboardToggleStatus { get; private set; } = new(false, false, false);
|
||||
|
||||
public void AddInputProvider(InputProvider inputProvider)
|
||||
{
|
||||
inputProvider.IdentifierReceived += InputProviderOnIdentifierReceived;
|
||||
inputProvider.KeyboardDataReceived += InputProviderOnKeyboardDataReceived;
|
||||
inputProvider.KeyboardToggleStatusReceived += InputProviderOnKeyboardToggleStatusReceived;
|
||||
inputProvider.MouseButtonDataReceived += InputProviderOnMouseButtonDataReceived;
|
||||
inputProvider.MouseScrollDataReceived += InputProviderOnMouseScrollDataReceived;
|
||||
inputProvider.MouseMoveDataReceived += InputProviderOnMouseMoveDataReceived;
|
||||
_inputProviders.Add(inputProvider);
|
||||
|
||||
inputProvider.OnKeyboardToggleStatusRequested();
|
||||
}
|
||||
|
||||
|
||||
public void RemoveInputProvider(InputProvider inputProvider)
|
||||
{
|
||||
if (!_inputProviders.Contains(inputProvider))
|
||||
@ -54,6 +60,7 @@ namespace Artemis.Core.Services
|
||||
_inputProviders.Remove(inputProvider);
|
||||
inputProvider.IdentifierReceived -= InputProviderOnIdentifierReceived;
|
||||
inputProvider.KeyboardDataReceived -= InputProviderOnKeyboardDataReceived;
|
||||
inputProvider.KeyboardToggleStatusReceived -= InputProviderOnKeyboardToggleStatusReceived;
|
||||
inputProvider.MouseButtonDataReceived -= InputProviderOnMouseButtonDataReceived;
|
||||
inputProvider.MouseScrollDataReceived -= InputProviderOnMouseScrollDataReceived;
|
||||
inputProvider.MouseMoveDataReceived -= InputProviderOnMouseMoveDataReceived;
|
||||
@ -209,6 +216,18 @@ namespace Artemis.Core.Services
|
||||
// _logger.Verbose("Keyboard data: LED ID: {ledId}, key: {key}, is down: {isDown}, modifiers: {modifiers}, device: {device} ", ledId, e.Key, e.IsDown, keyboardModifierKey, e.Device);
|
||||
}
|
||||
|
||||
private void InputProviderOnKeyboardToggleStatusReceived(object? sender, InputProviderKeyboardToggleEventArgs e)
|
||||
{
|
||||
KeyboardToggleStatus old = KeyboardToggleStatus;
|
||||
if (KeyboardToggleStatus.CapsLock == e.KeyboardToggleStatus.CapsLock &&
|
||||
KeyboardToggleStatus.NumLock == e.KeyboardToggleStatus.NumLock &&
|
||||
KeyboardToggleStatus.ScrollLock == e.KeyboardToggleStatus.ScrollLock)
|
||||
return;
|
||||
|
||||
KeyboardToggleStatus = e.KeyboardToggleStatus;
|
||||
OnKeyboardToggleStatusChanged(new ArtemisKeyboardToggleStatusArgs(old, KeyboardToggleStatus));
|
||||
}
|
||||
|
||||
private bool UpdatePressedKeys(ArtemisDevice? device, KeyboardKey key, bool isDown)
|
||||
{
|
||||
if (device != null)
|
||||
@ -318,6 +337,8 @@ namespace Artemis.Core.Services
|
||||
public event EventHandler<ArtemisKeyboardKeyUpDownEventArgs>? KeyboardKeyUpDown;
|
||||
public event EventHandler<ArtemisKeyboardKeyEventArgs>? KeyboardKeyDown;
|
||||
public event EventHandler<ArtemisKeyboardKeyEventArgs>? KeyboardKeyUp;
|
||||
public event EventHandler<ArtemisKeyboardToggleStatusArgs>? KeyboardToggleStatusChanged;
|
||||
|
||||
public event EventHandler<ArtemisMouseButtonUpDownEventArgs>? MouseButtonUpDown;
|
||||
public event EventHandler<ArtemisMouseButtonEventArgs>? MouseButtonDown;
|
||||
public event EventHandler<ArtemisMouseButtonEventArgs>? MouseButtonUp;
|
||||
@ -340,6 +361,11 @@ namespace Artemis.Core.Services
|
||||
KeyboardKeyUp?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnKeyboardToggleStatusChanged(ArtemisKeyboardToggleStatusArgs e)
|
||||
{
|
||||
KeyboardToggleStatusChanged?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnMouseButtonUpDown(ArtemisMouseButtonUpDownEventArgs e)
|
||||
{
|
||||
MouseButtonUpDown?.Invoke(this, e);
|
||||
|
||||
@ -7,6 +7,11 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
public interface IInputService : IArtemisService, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the current keyboard toggle status
|
||||
/// </summary>
|
||||
KeyboardToggleStatus KeyboardToggleStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds an input provided
|
||||
/// </summary>
|
||||
@ -47,6 +52,11 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
event EventHandler<ArtemisKeyboardKeyEventArgs> KeyboardKeyUp;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs whenever a one or more of the keyboard toggle statuses changed
|
||||
/// </summary>
|
||||
event EventHandler<ArtemisKeyboardToggleStatusArgs> KeyboardToggleStatusChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs whenever a button on a mouse was pressed or released
|
||||
/// </summary>
|
||||
|
||||
36
src/Artemis.Core/Services/Input/KeyboardToggleStatus.cs
Normal file
36
src/Artemis.Core/Services/Input/KeyboardToggleStatus.cs
Normal file
@ -0,0 +1,36 @@
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the status of a keyboards special toggles
|
||||
/// </summary>
|
||||
public readonly struct KeyboardToggleStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="KeyboardToggleStatus" />
|
||||
/// </summary>
|
||||
/// <param name="numLock">A boolean indicating whether num lock is on</param>
|
||||
/// <param name="capsLock">A boolean indicating whether caps lock is on</param>
|
||||
/// <param name="scrollLock">A boolean indicating whether scroll lock is on</param>
|
||||
public KeyboardToggleStatus(bool numLock, bool capsLock, bool scrollLock)
|
||||
{
|
||||
NumLock = numLock;
|
||||
CapsLock = capsLock;
|
||||
ScrollLock = scrollLock;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether num lock is on
|
||||
/// </summary>
|
||||
public bool NumLock { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether caps lock is on
|
||||
/// </summary>
|
||||
public bool CapsLock { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether scroll lock is on
|
||||
/// </summary>
|
||||
public bool ScrollLock { get; }
|
||||
}
|
||||
}
|
||||
@ -14,9 +14,9 @@ namespace Artemis.UI.InputProviders
|
||||
public class NativeWindowInputProvider : InputProvider
|
||||
{
|
||||
private const int WM_INPUT = 0x00FF;
|
||||
private readonly IInputService _inputService;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private readonly IInputService _inputService;
|
||||
private DateTime _lastMouseUpdate;
|
||||
private SpongeWindow _sponge;
|
||||
|
||||
@ -32,6 +32,16 @@ namespace Artemis.UI.InputProviders
|
||||
RawInputDevice.RegisterDevice(HidUsageAndPage.Mouse, RawInputDeviceFlags.InputSink, _sponge.Handle);
|
||||
}
|
||||
|
||||
#region Overrides of InputProvider
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnKeyboardToggleStatusRequested()
|
||||
{
|
||||
UpdateToggleStatus();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -88,7 +98,6 @@ namespace Artemis.UI.InputProviders
|
||||
|
||||
ArtemisDevice device = null;
|
||||
if (identifier != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
device = _inputService.GetDeviceByIdentifier(this, identifier, InputDeviceType.Keyboard);
|
||||
@ -97,7 +106,6 @@ namespace Artemis.UI.InputProviders
|
||||
{
|
||||
_logger.Warning(e, "Failed to retrieve input device by its identifier");
|
||||
}
|
||||
}
|
||||
|
||||
// Duplicate keys with different positions can be identified by the LeftKey flag (even though its set of the key that's physically on the right)
|
||||
if (keyboardData.Keyboard.Flags == RawKeyboardFlags.LeftKey || keyboardData.Keyboard.Flags == (RawKeyboardFlags.LeftKey | RawKeyboardFlags.Up))
|
||||
@ -118,6 +126,16 @@ namespace Artemis.UI.InputProviders
|
||||
keyboardData.Keyboard.Flags != (RawKeyboardFlags.Up | RawKeyboardFlags.RightKey);
|
||||
|
||||
OnKeyboardDataReceived(device, key, isDown);
|
||||
UpdateToggleStatus();
|
||||
}
|
||||
|
||||
private void UpdateToggleStatus()
|
||||
{
|
||||
OnKeyboardToggleStatusReceived(new KeyboardToggleStatus(
|
||||
Keyboard.IsKeyToggled(Key.NumLock),
|
||||
Keyboard.IsKeyToggled(Key.CapsLock),
|
||||
Keyboard.IsKeyToggled(Key.Scroll)
|
||||
));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -142,7 +160,6 @@ namespace Artemis.UI.InputProviders
|
||||
ArtemisDevice device = null;
|
||||
string identifier = data.Device?.DevicePath;
|
||||
if (identifier != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
device = _inputService.GetDeviceByIdentifier(this, identifier, InputDeviceType.Keyboard);
|
||||
@ -151,7 +168,6 @@ namespace Artemis.UI.InputProviders
|
||||
{
|
||||
_logger.Warning(e, "Failed to retrieve input device by its identifier");
|
||||
}
|
||||
}
|
||||
|
||||
// Debug.WriteLine($"Buttons: {data.Mouse.Buttons}, Data: {data.Mouse.ButtonData}, Flags: {data.Mouse.Flags}, XY: {data.Mouse.LastX},{data.Mouse.LastY}");
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user