1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Added method to opt in to the keypress-callback since this prevents reinitializing

This commit is contained in:
Darth Affe 2017-11-25 09:12:57 +01:00
parent 897162926a
commit 811a296867
2 changed files with 34 additions and 3 deletions

View File

@ -104,7 +104,7 @@ namespace CUE.NET
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed); private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed);
private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed; private static OnKeyPressedDelegate _onKeyPressedDelegate;
#endregion #endregion
@ -113,6 +113,8 @@ namespace CUE.NET
/// <summary> /// <summary>
/// Occurs when the SDK reports that a key is pressed. /// Occurs when the SDK reports that a key is pressed.
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported. /// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
///
/// To enable this event <see cref="EnableKeypressCallback"/> needs to be called.
/// </summary> /// </summary>
public static event EventHandler<KeyPressedEventArgs> KeyPressed; public static event EventHandler<KeyPressedEventArgs> KeyPressed;
@ -247,7 +249,6 @@ namespace CUE.NET
Throw(error, true); Throw(error, true);
} }
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
error = LastError; error = LastError;
if (error != CorsairError.Success) if (error != CorsairError.Success)
Throw(error, false); Throw(error, false);
@ -257,6 +258,21 @@ namespace CUE.NET
IsInitialized = true; IsInitialized = true;
} }
/// <summary>
/// Enables the keypress-callback.
/// This method needs to be called to enable the <see cref="KeyPressed"/>-event.
///
/// WARNING: AFTER THIS METHOD IS CALLED IT'S NO LONGER POSSIBLE TO REINITIALIZE THE SDK!
/// </summary>
public static void EnableKeypressCallback()
{
if (!IsInitialized)
throw new WrapperException("CueSDK isn't initialized.");
_onKeyPressedDelegate = OnKeyPressed;
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
}
/// <summary> /// <summary>
/// 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.) /// 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.)
/// </summary> /// </summary>
@ -283,6 +299,9 @@ namespace CUE.NET
if (!IsInitialized) if (!IsInitialized)
throw new WrapperException("CueSDK isn't initialized."); throw new WrapperException("CueSDK isn't initialized.");
if (_onKeyPressedDelegate != null)
throw new WrapperException("Keypress-Callback is enabled.");
KeyboardSDK?.ResetLeds(); KeyboardSDK?.ResetLeds();
MouseSDK?.ResetLeds(); MouseSDK?.ResetLeds();
HeadsetSDK?.ResetLeds(); HeadsetSDK?.ResetLeds();
@ -343,7 +362,6 @@ namespace CUE.NET
|| HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model) || HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
throw new WrapperException("The previously loaded Headset Stand got disconnected."); throw new WrapperException("The previously loaded Headset Stand got disconnected.");
_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
error = LastError; error = LastError;
if (error != CorsairError.Success) if (error != CorsairError.Success)
Throw(error, false); Throw(error, false);

View File

@ -39,6 +39,8 @@ namespace SimpleDevTest
CueSDK.Initialize(); CueSDK.Initialize();
Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK"); Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK");
CueSDK.EnableKeypressCallback();
CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}"); CueSDK.KeyPressed += (sender, eventArgs) => Console.WriteLine($"Key {eventArgs.KeyId} {(eventArgs.IsPressed ? "pressed" : "released")}");
//CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black; //CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black;
@ -76,6 +78,17 @@ namespace SimpleDevTest
CueSDK.UpdateMode = UpdateMode.Continuous; 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()); //IBrush rainbowBrush = new LinearGradientBrush(new RainbowGradient());
//rainbowBrush.AddEffect(new FlashEffect { Attack = 5f, Sustain = 1f, Decay = 0, Release = 5f, Interval = 1f }); //rainbowBrush.AddEffect(new FlashEffect { Attack = 5f, Sustain = 1f, Decay = 0, Release = 5f, Interval = 1f });
//rainbowBrush.AddEffect(new MoveRainbowEffect()); //rainbowBrush.AddEffect(new MoveRainbowEffect());