From 5091b338649d2dad3c60fee50ace1710440a7a8f Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 18 Nov 2017 12:29:03 +0100 Subject: [PATCH] Added an event to get the key-change of special keys (G and M) --- CUE.NET.csproj | 4 ++ CueSDK.cs | 29 ++++++++++++ Devices/Generic/Enums/CorsairKeyId.cs | 46 +++++++++++++++++++ .../Keyboard/Enums/CorsairKeyboardKeyId.cs | 36 +++++++++++++++ Devices/Mouse/Enums/CorsairMouseKeyId.cs | 30 ++++++++++++ EventArgs/KeyPressedEventArgs.cs | 41 +++++++++++++++++ Examples/SimpleDevTest/Program.cs | 2 + Native/_CUESDK.cs | 10 ++++ 8 files changed, 198 insertions(+) create mode 100644 Devices/Generic/Enums/CorsairKeyId.cs create mode 100644 Devices/Keyboard/Enums/CorsairKeyboardKeyId.cs create mode 100644 Devices/Mouse/Enums/CorsairMouseKeyId.cs create mode 100644 EventArgs/KeyPressedEventArgs.cs diff --git a/CUE.NET.csproj b/CUE.NET.csproj index f738e4e..ff1ef4c 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -59,6 +59,7 @@ + @@ -71,6 +72,8 @@ + + @@ -79,6 +82,7 @@ + diff --git a/CueSDK.cs b/CueSDK.cs index 32ae110..205a401 100644 --- a/CueSDK.cs +++ b/CueSDK.cs @@ -1,6 +1,7 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Runtime.InteropServices; @@ -12,6 +13,7 @@ using CUE.NET.Devices.HeadsetStand; using CUE.NET.Devices.Keyboard; using CUE.NET.Devices.Mouse; using CUE.NET.Devices.Mousemat; +using CUE.NET.EventArgs; using CUE.NET.Exceptions; using CUE.NET.Native; @@ -100,6 +102,20 @@ namespace CUE.NET // ReSharper restore UnusedAutoPropertyAccessor.Global + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed); + private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed; + + #endregion + + #region Events + + /// + /// Occurs when the SDK reports that a key is pressed. + /// Notice that right now only G- (keyboard) and M- (mouse) keys are supported. + /// + public static event EventHandler KeyPressed; + #endregion #region Methods @@ -231,6 +247,11 @@ namespace CUE.NET Throw(error, true); } + _CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero); + error = LastError; + if (error != CorsairError.Success) + Throw(error, false); + InitializedDevices = new ReadOnlyCollection(devices); IsInitialized = true; @@ -322,6 +343,11 @@ namespace CUE.NET || HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model) throw new WrapperException("The previously loaded Headset Stand got disconnected."); + _CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero); + error = LastError; + if (error != CorsairError.Success) + Throw(error, false); + IsInitialized = true; } @@ -342,6 +368,9 @@ namespace CUE.NET throw new CUEException(error); } + private static void OnKeyPressed(IntPtr context, CorsairKeyId keyId, bool pressed) + => KeyPressed?.Invoke(null, new KeyPressedEventArgs(keyId, pressed)); + #endregion } } diff --git a/Devices/Generic/Enums/CorsairKeyId.cs b/Devices/Generic/Enums/CorsairKeyId.cs new file mode 100644 index 0000000..693c471 --- /dev/null +++ b/Devices/Generic/Enums/CorsairKeyId.cs @@ -0,0 +1,46 @@ +// ReSharper disable InconsistentNaming + +#pragma warning disable 1591 // Missing XML comment for publicly visible type or member + +namespace CUE.NET.Devices.Generic.Enums +{ + /// + /// Contains list of all KeyIds available for all corsair devices. + /// + public enum CorsairKeyId + { + Invalid = 0, + + G1 = 1, + G2 = 2, + G3 = 3, + G4 = 4, + G5 = 5, + G6 = 6, + G7 = 7, + G8 = 8, + G9 = 9, + G10 = 10, + G11 = 11, + G12 = 12, + G13 = 13, + G14 = 14, + G15 = 15, + G16 = 16, + G17 = 17, + G18 = 18, + + M1 = 19, + M2 = 20, + M3 = 21, + M4 = 22, + M5 = 23, + M6 = 24, + M7 = 25, + M8 = 26, + M9 = 27, + M10 = 28, + M11 = 29, + M12 = 30, + } +} diff --git a/Devices/Keyboard/Enums/CorsairKeyboardKeyId.cs b/Devices/Keyboard/Enums/CorsairKeyboardKeyId.cs new file mode 100644 index 0000000..9c2ce3c --- /dev/null +++ b/Devices/Keyboard/Enums/CorsairKeyboardKeyId.cs @@ -0,0 +1,36 @@ +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + +#pragma warning disable 1591 // Missing XML comment for publicly visible type or member + +using CUE.NET.Devices.Generic.Enums; + +namespace CUE.NET.Devices.Keyboard.Enums +{ + /// + /// Contains list of all LEDs available for corsair keyboards. + /// + public static class CorsairKeyboardKeyId + { + public const CorsairKeyId Invalid = CorsairKeyId.Invalid; + + public const CorsairKeyId G1 = CorsairKeyId.G1; + public const CorsairKeyId G2 = CorsairKeyId.G2; + public const CorsairKeyId G3 = CorsairKeyId.G3; + public const CorsairKeyId G4 = CorsairKeyId.G4; + public const CorsairKeyId G5 = CorsairKeyId.G5; + public const CorsairKeyId G6 = CorsairKeyId.G6; + public const CorsairKeyId G7 = CorsairKeyId.G7; + public const CorsairKeyId G8 = CorsairKeyId.G8; + public const CorsairKeyId G9 = CorsairKeyId.G9; + public const CorsairKeyId G10 = CorsairKeyId.G10; + public const CorsairKeyId G11 = CorsairKeyId.G11; + public const CorsairKeyId G12 = CorsairKeyId.G12; + public const CorsairKeyId G13 = CorsairKeyId.G13; + public const CorsairKeyId G14 = CorsairKeyId.G14; + public const CorsairKeyId G15 = CorsairKeyId.G15; + public const CorsairKeyId G16 = CorsairKeyId.G16; + public const CorsairKeyId G17 = CorsairKeyId.G17; + public const CorsairKeyId G18 = CorsairKeyId.G18; + } +} diff --git a/Devices/Mouse/Enums/CorsairMouseKeyId.cs b/Devices/Mouse/Enums/CorsairMouseKeyId.cs new file mode 100644 index 0000000..751f0a8 --- /dev/null +++ b/Devices/Mouse/Enums/CorsairMouseKeyId.cs @@ -0,0 +1,30 @@ +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + +#pragma warning disable 1591 // Missing XML comment for publicly visible type or member + +using CUE.NET.Devices.Generic.Enums; + +namespace CUE.NET.Devices.Mouse.Enums +{ + /// + /// Contains list of all LEDs available for corsair mice. + /// + public static class CorsairMouseKeyId + { + public const CorsairKeyId Invalid = CorsairKeyId.Invalid; + + public const CorsairKeyId M1 = CorsairKeyId.M1; + public const CorsairKeyId M2 = CorsairKeyId.M2; + public const CorsairKeyId M3 = CorsairKeyId.M3; + public const CorsairKeyId M4 = CorsairKeyId.M4; + public const CorsairKeyId M5 = CorsairKeyId.M5; + public const CorsairKeyId M6 = CorsairKeyId.M6; + public const CorsairKeyId M7 = CorsairKeyId.M7; + public const CorsairKeyId M8 = CorsairKeyId.M8; + public const CorsairKeyId M9 = CorsairKeyId.M9; + public const CorsairKeyId M10 = CorsairKeyId.M10; + public const CorsairKeyId M11 = CorsairKeyId.M11; + public const CorsairKeyId M12 = CorsairKeyId.M12; + } +} diff --git a/EventArgs/KeyPressedEventArgs.cs b/EventArgs/KeyPressedEventArgs.cs new file mode 100644 index 0000000..5741055 --- /dev/null +++ b/EventArgs/KeyPressedEventArgs.cs @@ -0,0 +1,41 @@ +// ReSharper disable MemberCanBePrivate.Global + +using CUE.NET.Devices.Generic.Enums; + +namespace CUE.NET.EventArgs +{ + /// + /// Represents the data provided by the -event. + /// + public class KeyPressedEventArgs : System.EventArgs + { + #region Properties & Fields + + /// + /// Gets the id of the key. + /// + public CorsairKeyId KeyId { get; } + + /// + /// Gets the current status of the key (true = pressed, flase = released). + /// + public bool IsPressed { get; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + /// The id of the key. + /// The current status of the key (true = pressed, flase = released). + public KeyPressedEventArgs(CorsairKeyId keyId, bool isPressed) + { + this.KeyId = keyId; + this.IsPressed = isPressed; + } + + #endregion + } +} diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index b55a9a4..8780c87 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -39,6 +39,8 @@ namespace SimpleDevTest CueSDK.Initialize(); Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK"); + CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}"); + //CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black; //CueSDK.KeyboardSDK[CorsairLedId.Z].Color = Color.Red; //CueSDK.KeyboardSDK[CorsairLedId.Z].IsLocked = true; diff --git a/Native/_CUESDK.cs b/Native/_CUESDK.cs index 8d5d3a9..9f8b939 100644 --- a/Native/_CUESDK.cs +++ b/Native/_CUESDK.cs @@ -63,6 +63,7 @@ namespace CUE.NET.Native _corsairReleaseControlPointer = (CorsairReleaseControlPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairReleaseControl"), typeof(CorsairReleaseControlPointer)); _corsairPerformProtocolHandshakePointer = (CorsairPerformProtocolHandshakePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairPerformProtocolHandshake"), typeof(CorsairPerformProtocolHandshakePointer)); _corsairGetLastErrorPointer = (CorsairGetLastErrorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer)); + _corsairRegisterKeypressCallbackPointer = (CorsairRegisterKeypressCallbackPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRegisterKeypressCallback"), typeof(CorsairRegisterKeypressCallbackPointer)); } private static void UnloadCUESDK() @@ -100,6 +101,7 @@ namespace CUE.NET.Native private static CorsairReleaseControlPointer _corsairReleaseControlPointer; private static CorsairPerformProtocolHandshakePointer _corsairPerformProtocolHandshakePointer; private static CorsairGetLastErrorPointer _corsairGetLastErrorPointer; + private static CorsairRegisterKeypressCallbackPointer _corsairRegisterKeypressCallbackPointer; #endregion @@ -138,6 +140,9 @@ namespace CUE.NET.Native [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate CorsairError CorsairGetLastErrorPointer(); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate bool CorsairRegisterKeypressCallbackPointer(IntPtr callback, IntPtr context); + #endregion // ReSharper disable EventExceptionNotDocumented @@ -231,6 +236,11 @@ namespace CUE.NET.Native return _corsairGetLastErrorPointer(); } + internal static bool CorsairRegisterKeypressCallback(IntPtr callback, IntPtr context) + { + return _corsairRegisterKeypressCallbackPointer(callback, context); + } + // ReSharper restore EventExceptionNotDocumented #endregion