From 811a296867c1c81baa07a780eac8e85f226ecf05 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 25 Nov 2017 09:12:57 +0100 Subject: [PATCH] Added method to opt in to the keypress-callback since this prevents reinitializing --- CueSDK.cs | 24 +++++++++++++++++++++--- Examples/SimpleDevTest/Program.cs | 13 +++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CueSDK.cs b/CueSDK.cs index 205a401..d783db0 100644 --- a/CueSDK.cs +++ b/CueSDK.cs @@ -104,7 +104,7 @@ namespace CUE.NET [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed); - private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed; + private static OnKeyPressedDelegate _onKeyPressedDelegate; #endregion @@ -113,6 +113,8 @@ namespace CUE.NET /// /// Occurs when the SDK reports that a key is pressed. /// Notice that right now only G- (keyboard) and M- (mouse) keys are supported. + /// + /// To enable this event needs to be called. /// public static event EventHandler KeyPressed; @@ -247,7 +249,6 @@ namespace CUE.NET Throw(error, true); } - _CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero); error = LastError; if (error != CorsairError.Success) Throw(error, false); @@ -257,6 +258,21 @@ namespace CUE.NET IsInitialized = true; } + /// + /// Enables the keypress-callback. + /// This method needs to be called to enable the -event. + /// + /// WARNING: AFTER THIS METHOD IS CALLED IT'S NO LONGER POSSIBLE TO REINITIALIZE THE SDK! + /// + public static void EnableKeypressCallback() + { + if (!IsInitialized) + throw new WrapperException("CueSDK isn't initialized."); + + _onKeyPressedDelegate = OnKeyPressed; + _CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero); + } + /// /// 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.) /// @@ -283,6 +299,9 @@ namespace CUE.NET if (!IsInitialized) throw new WrapperException("CueSDK isn't initialized."); + if (_onKeyPressedDelegate != null) + throw new WrapperException("Keypress-Callback is enabled."); + KeyboardSDK?.ResetLeds(); MouseSDK?.ResetLeds(); HeadsetSDK?.ResetLeds(); @@ -343,7 +362,6 @@ 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); diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index 8780c87..251b29b 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.EnableKeypressCallback(); + CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}"); //CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black; @@ -76,6 +78,17 @@ namespace SimpleDevTest CueSDK.UpdateMode = UpdateMode.Continuous; + Wait(5); + CueSDK.UpdateMode = UpdateMode.Manual; + for (int i = 0; i < 100000; i++) + { + CueSDK.Reinitialize(); + Console.WriteLine(i); + } + + Console.WriteLine("done!"); + Wait(5); + //IBrush rainbowBrush = new LinearGradientBrush(new RainbowGradient()); //rainbowBrush.AddEffect(new FlashEffect { Attack = 5f, Sustain = 1f, Decay = 0, Release = 5f, Interval = 1f }); //rainbowBrush.AddEffect(new MoveRainbowEffect());