Skip to content
This repository was archived by the owner on Aug 6, 2021. It is now read-only.

Commit 5091b33

Browse files
committed
Added an event to get the key-change of special keys (G and M)
1 parent c4ac6f0 commit 5091b33

File tree

8 files changed

+198
-0
lines changed

8 files changed

+198
-0
lines changed

CUE.NET.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
6060
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
6161
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
62+
<Compile Include="Devices\Generic\Enums\CorsairKeyId.cs" />
6263
<Compile Include="Devices\Generic\Enums\CorsairLedId.cs" />
6364
<Compile Include="Devices\Generic\EventArgs\ExceptionEventArgs.cs" />
6465
<Compile Include="Brushes\AbstractBrush.cs" />
@@ -71,6 +72,8 @@
7172
<Compile Include="Devices\HeadsetStand\CorsairHeadsetStandDeviceInfo.cs" />
7273
<Compile Include="Devices\HeadsetStand\Enums\CorsairHeadsetStandLedId.cs" />
7374
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
75+
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
76+
<Compile Include="Devices\Mouse\Enums\CorsairMouseKeyId.cs" />
7477
<Compile Include="Effects\AbstractLedGroupEffect.cs" />
7578
<Compile Include="Effects\AbstractBrushEffect.cs" />
7679
<Compile Include="Effects\AbstractEffectTarget.cs" />
@@ -79,6 +82,7 @@
7982
<Compile Include="Devices\Mousemat\CorsairMousematDeviceInfo.cs" />
8083
<Compile Include="Devices\Mousemat\Enums\CorsairMousematLedId.cs" />
8184
<Compile Include="Effects\MoveGradientEffect.cs" />
85+
<Compile Include="EventArgs\KeyPressedEventArgs.cs" />
8286
<Compile Include="Gradients\AbstractGradient.cs" />
8387
<Compile Include="Gradients\GradientStop.cs" />
8488
<Compile Include="Gradients\IGradient.cs" />

CueSDK.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ReSharper disable MemberCanBePrivate.Global
22
// ReSharper disable UnusedMember.Global
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
67
using System.Runtime.InteropServices;
@@ -12,6 +13,7 @@
1213
using CUE.NET.Devices.Keyboard;
1314
using CUE.NET.Devices.Mouse;
1415
using CUE.NET.Devices.Mousemat;
16+
using CUE.NET.EventArgs;
1517
using CUE.NET.Exceptions;
1618
using CUE.NET.Native;
1719

@@ -100,6 +102,20 @@ public static partial class CueSDK
100102

101103
// ReSharper restore UnusedAutoPropertyAccessor.Global
102104

105+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
106+
private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed);
107+
private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed;
108+
109+
#endregion
110+
111+
#region Events
112+
113+
/// <summary>
114+
/// Occurs when the SDK reports that a key is pressed.
115+
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
116+
/// </summary>
117+
public static event EventHandler<KeyPressedEventArgs> KeyPressed;
118+
103119
#endregion
104120

105121
#region Methods
@@ -231,6 +247,11 @@ public static void Initialize(bool exclusiveAccess = false)
231247
Throw(error, true);
232248
}
233249

250+
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
251+
error = LastError;
252+
if (error != CorsairError.Success)
253+
Throw(error, false);
254+
234255
InitializedDevices = new ReadOnlyCollection<ICueDevice>(devices);
235256

236257
IsInitialized = true;
@@ -322,6 +343,11 @@ public static void Reinitialize(bool exclusiveAccess)
322343
|| HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
323344
throw new WrapperException("The previously loaded Headset Stand got disconnected.");
324345

346+
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
347+
error = LastError;
348+
if (error != CorsairError.Success)
349+
Throw(error, false);
350+
325351
IsInitialized = true;
326352
}
327353

@@ -342,6 +368,9 @@ private static void Throw(CorsairError error, bool reset)
342368
throw new CUEException(error);
343369
}
344370

371+
private static void OnKeyPressed(IntPtr context, CorsairKeyId keyId, bool pressed)
372+
=> KeyPressed?.Invoke(null, new KeyPressedEventArgs(keyId, pressed));
373+
345374
#endregion
346375
}
347376
}

Devices/Generic/Enums/CorsairKeyId.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ReSharper disable InconsistentNaming
2+
3+
#pragma warning disable 1591 // Missing XML comment for publicly visible type or member
4+
5+
namespace CUE.NET.Devices.Generic.Enums
6+
{
7+
/// <summary>
8+
/// Contains list of all KeyIds available for all corsair devices.
9+
/// </summary>
10+
public enum CorsairKeyId
11+
{
12+
Invalid = 0,
13+
14+
G1 = 1,
15+
G2 = 2,
16+
G3 = 3,
17+
G4 = 4,
18+
G5 = 5,
19+
G6 = 6,
20+
G7 = 7,
21+
G8 = 8,
22+
G9 = 9,
23+
G10 = 10,
24+
G11 = 11,
25+
G12 = 12,
26+
G13 = 13,
27+
G14 = 14,
28+
G15 = 15,
29+
G16 = 16,
30+
G17 = 17,
31+
G18 = 18,
32+
33+
M1 = 19,
34+
M2 = 20,
35+
M3 = 21,
36+
M4 = 22,
37+
M5 = 23,
38+
M6 = 24,
39+
M7 = 25,
40+
M8 = 26,
41+
M9 = 27,
42+
M10 = 28,
43+
M11 = 29,
44+
M12 = 30,
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ReSharper disable UnusedMember.Global
2+
// ReSharper disable InconsistentNaming
3+
4+
#pragma warning disable 1591 // Missing XML comment for publicly visible type or member
5+
6+
using CUE.NET.Devices.Generic.Enums;
7+
8+
namespace CUE.NET.Devices.Keyboard.Enums
9+
{
10+
/// <summary>
11+
/// Contains list of all LEDs available for corsair keyboards.
12+
/// </summary>
13+
public static class CorsairKeyboardKeyId
14+
{
15+
public const CorsairKeyId Invalid = CorsairKeyId.Invalid;
16+
17+
public const CorsairKeyId G1 = CorsairKeyId.G1;
18+
public const CorsairKeyId G2 = CorsairKeyId.G2;
19+
public const CorsairKeyId G3 = CorsairKeyId.G3;
20+
public const CorsairKeyId G4 = CorsairKeyId.G4;
21+
public const CorsairKeyId G5 = CorsairKeyId.G5;
22+
public const CorsairKeyId G6 = CorsairKeyId.G6;
23+
public const CorsairKeyId G7 = CorsairKeyId.G7;
24+
public const CorsairKeyId G8 = CorsairKeyId.G8;
25+
public const CorsairKeyId G9 = CorsairKeyId.G9;
26+
public const CorsairKeyId G10 = CorsairKeyId.G10;
27+
public const CorsairKeyId G11 = CorsairKeyId.G11;
28+
public const CorsairKeyId G12 = CorsairKeyId.G12;
29+
public const CorsairKeyId G13 = CorsairKeyId.G13;
30+
public const CorsairKeyId G14 = CorsairKeyId.G14;
31+
public const CorsairKeyId G15 = CorsairKeyId.G15;
32+
public const CorsairKeyId G16 = CorsairKeyId.G16;
33+
public const CorsairKeyId G17 = CorsairKeyId.G17;
34+
public const CorsairKeyId G18 = CorsairKeyId.G18;
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ReSharper disable UnusedMember.Global
2+
// ReSharper disable InconsistentNaming
3+
4+
#pragma warning disable 1591 // Missing XML comment for publicly visible type or member
5+
6+
using CUE.NET.Devices.Generic.Enums;
7+
8+
namespace CUE.NET.Devices.Mouse.Enums
9+
{
10+
/// <summary>
11+
/// Contains list of all LEDs available for corsair mice.
12+
/// </summary>
13+
public static class CorsairMouseKeyId
14+
{
15+
public const CorsairKeyId Invalid = CorsairKeyId.Invalid;
16+
17+
public const CorsairKeyId M1 = CorsairKeyId.M1;
18+
public const CorsairKeyId M2 = CorsairKeyId.M2;
19+
public const CorsairKeyId M3 = CorsairKeyId.M3;
20+
public const CorsairKeyId M4 = CorsairKeyId.M4;
21+
public const CorsairKeyId M5 = CorsairKeyId.M5;
22+
public const CorsairKeyId M6 = CorsairKeyId.M6;
23+
public const CorsairKeyId M7 = CorsairKeyId.M7;
24+
public const CorsairKeyId M8 = CorsairKeyId.M8;
25+
public const CorsairKeyId M9 = CorsairKeyId.M9;
26+
public const CorsairKeyId M10 = CorsairKeyId.M10;
27+
public const CorsairKeyId M11 = CorsairKeyId.M11;
28+
public const CorsairKeyId M12 = CorsairKeyId.M12;
29+
}
30+
}

EventArgs/KeyPressedEventArgs.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ReSharper disable MemberCanBePrivate.Global
2+
3+
using CUE.NET.Devices.Generic.Enums;
4+
5+
namespace CUE.NET.EventArgs
6+
{
7+
/// <summary>
8+
/// Represents the data provided by the <see cref="CueSDK.KeyPressed"/>-event.
9+
/// </summary>
10+
public class KeyPressedEventArgs : System.EventArgs
11+
{
12+
#region Properties & Fields
13+
14+
/// <summary>
15+
/// Gets the id of the key.
16+
/// </summary>
17+
public CorsairKeyId KeyId { get; }
18+
19+
/// <summary>
20+
/// Gets the current status of the key (true = pressed, flase = released).
21+
/// </summary>
22+
public bool IsPressed { get; }
23+
24+
#endregion
25+
26+
#region Constructors
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="KeyPressedEventArgs"/> class.
30+
/// </summary>
31+
/// <param name="keyId">The id of the key.</param>
32+
/// <param name="isPressed">The current status of the key (true = pressed, flase = released).</param>
33+
public KeyPressedEventArgs(CorsairKeyId keyId, bool isPressed)
34+
{
35+
this.KeyId = keyId;
36+
this.IsPressed = isPressed;
37+
}
38+
39+
#endregion
40+
}
41+
}

Examples/SimpleDevTest/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public static void Main(string[] args)
3939
CueSDK.Initialize();
4040
Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK");
4141

42+
CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}");
43+
4244
//CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black;
4345
//CueSDK.KeyboardSDK[CorsairLedId.Z].Color = Color.Red;
4446
//CueSDK.KeyboardSDK[CorsairLedId.Z].IsLocked = true;

Native/_CUESDK.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private static void LoadCUESDK()
6363
_corsairReleaseControlPointer = (CorsairReleaseControlPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairReleaseControl"), typeof(CorsairReleaseControlPointer));
6464
_corsairPerformProtocolHandshakePointer = (CorsairPerformProtocolHandshakePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairPerformProtocolHandshake"), typeof(CorsairPerformProtocolHandshakePointer));
6565
_corsairGetLastErrorPointer = (CorsairGetLastErrorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer));
66+
_corsairRegisterKeypressCallbackPointer = (CorsairRegisterKeypressCallbackPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRegisterKeypressCallback"), typeof(CorsairRegisterKeypressCallbackPointer));
6667
}
6768

6869
private static void UnloadCUESDK()
@@ -100,6 +101,7 @@ private static void UnloadCUESDK()
100101
private static CorsairReleaseControlPointer _corsairReleaseControlPointer;
101102
private static CorsairPerformProtocolHandshakePointer _corsairPerformProtocolHandshakePointer;
102103
private static CorsairGetLastErrorPointer _corsairGetLastErrorPointer;
104+
private static CorsairRegisterKeypressCallbackPointer _corsairRegisterKeypressCallbackPointer;
103105

104106
#endregion
105107

@@ -138,6 +140,9 @@ private static void UnloadCUESDK()
138140
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
139141
private delegate CorsairError CorsairGetLastErrorPointer();
140142

143+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
144+
private delegate bool CorsairRegisterKeypressCallbackPointer(IntPtr callback, IntPtr context);
145+
141146
#endregion
142147

143148
// ReSharper disable EventExceptionNotDocumented
@@ -231,6 +236,11 @@ internal static CorsairError CorsairGetLastError()
231236
return _corsairGetLastErrorPointer();
232237
}
233238

239+
internal static bool CorsairRegisterKeypressCallback(IntPtr callback, IntPtr context)
240+
{
241+
return _corsairRegisterKeypressCallbackPointer(callback, context);
242+
}
243+
234244
// ReSharper restore EventExceptionNotDocumented
235245

236246
#endregion

0 commit comments

Comments
 (0)