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

Commit 811a296

Browse files
committed
Added method to opt in to the keypress-callback since this prevents reinitializing
1 parent 8971629 commit 811a296

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

CueSDK.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static partial class CueSDK
104104

105105
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
106106
private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed);
107-
private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed;
107+
private static OnKeyPressedDelegate _onKeyPressedDelegate;
108108

109109
#endregion
110110

@@ -113,6 +113,8 @@ public static partial class CueSDK
113113
/// <summary>
114114
/// Occurs when the SDK reports that a key is pressed.
115115
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
116+
///
117+
/// To enable this event <see cref="EnableKeypressCallback"/> needs to be called.
116118
/// </summary>
117119
public static event EventHandler<KeyPressedEventArgs> KeyPressed;
118120

@@ -247,7 +249,6 @@ public static void Initialize(bool exclusiveAccess = false)
247249
Throw(error, true);
248250
}
249251

250-
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
251252
error = LastError;
252253
if (error != CorsairError.Success)
253254
Throw(error, false);
@@ -257,6 +258,21 @@ public static void Initialize(bool exclusiveAccess = false)
257258
IsInitialized = true;
258259
}
259260

261+
/// <summary>
262+
/// Enables the keypress-callback.
263+
/// This method needs to be called to enable the <see cref="KeyPressed"/>-event.
264+
///
265+
/// WARNING: AFTER THIS METHOD IS CALLED IT'S NO LONGER POSSIBLE TO REINITIALIZE THE SDK!
266+
/// </summary>
267+
public static void EnableKeypressCallback()
268+
{
269+
if (!IsInitialized)
270+
throw new WrapperException("CueSDK isn't initialized.");
271+
272+
_onKeyPressedDelegate = OnKeyPressed;
273+
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
274+
}
275+
260276
/// <summary>
261277
/// Resets the colors of all devices back to the last saved color-data. (If there wasn't a manual save, that's the data from the time the SDK was initialized.)
262278
/// </summary>
@@ -283,6 +299,9 @@ public static void Reinitialize(bool exclusiveAccess)
283299
if (!IsInitialized)
284300
throw new WrapperException("CueSDK isn't initialized.");
285301

302+
if (_onKeyPressedDelegate != null)
303+
throw new WrapperException("Keypress-Callback is enabled.");
304+
286305
KeyboardSDK?.ResetLeds();
287306
MouseSDK?.ResetLeds();
288307
HeadsetSDK?.ResetLeds();
@@ -343,7 +362,6 @@ public static void Reinitialize(bool exclusiveAccess)
343362
|| HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
344363
throw new WrapperException("The previously loaded Headset Stand got disconnected.");
345364

346-
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
347365
error = LastError;
348366
if (error != CorsairError.Success)
349367
Throw(error, false);

Examples/SimpleDevTest/Program.cs

Lines changed: 13 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.EnableKeypressCallback();
43+
4244
CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}");
4345

4446
//CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black;
@@ -76,6 +78,17 @@ public static void Main(string[] args)
7678

7779
CueSDK.UpdateMode = UpdateMode.Continuous;
7880

81+
Wait(5);
82+
CueSDK.UpdateMode = UpdateMode.Manual;
83+
for (int i = 0; i < 100000; i++)
84+
{
85+
CueSDK.Reinitialize();
86+
Console.WriteLine(i);
87+
}
88+
89+
Console.WriteLine("done!");
90+
Wait(5);
91+
7992
//IBrush rainbowBrush = new LinearGradientBrush(new RainbowGradient());
8093
//rainbowBrush.AddEffect(new FlashEffect { Attack = 5f, Sustain = 1f, Decay = 0, Release = 5f, Interval = 1f });
8194
//rainbowBrush.AddEffect(new MoveRainbowEffect());

0 commit comments

Comments
 (0)