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

Commit

Permalink
Added an event to get the key-change of special keys (G and M)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthAffe committed Nov 18, 2017
1 parent c4ac6f0 commit 5091b33
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CUE.NET.csproj
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Generic\Enums\CorsairKeyId.cs" />
<Compile Include="Devices\Generic\Enums\CorsairLedId.cs" />
<Compile Include="Devices\Generic\EventArgs\ExceptionEventArgs.cs" />
<Compile Include="Brushes\AbstractBrush.cs" />
Expand All @@ -71,6 +72,8 @@
<Compile Include="Devices\HeadsetStand\CorsairHeadsetStandDeviceInfo.cs" />
<Compile Include="Devices\HeadsetStand\Enums\CorsairHeadsetStandLedId.cs" />
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairMouseKeyId.cs" />
<Compile Include="Effects\AbstractLedGroupEffect.cs" />
<Compile Include="Effects\AbstractBrushEffect.cs" />
<Compile Include="Effects\AbstractEffectTarget.cs" />
Expand All @@ -79,6 +82,7 @@
<Compile Include="Devices\Mousemat\CorsairMousematDeviceInfo.cs" />
<Compile Include="Devices\Mousemat\Enums\CorsairMousematLedId.cs" />
<Compile Include="Effects\MoveGradientEffect.cs" />
<Compile Include="EventArgs\KeyPressedEventArgs.cs" />
<Compile Include="Gradients\AbstractGradient.cs" />
<Compile Include="Gradients\GradientStop.cs" />
<Compile Include="Gradients\IGradient.cs" />
Expand Down
29 changes: 29 additions & 0 deletions 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;
Expand All @@ -12,6 +13,7 @@
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;

Expand Down Expand Up @@ -100,6 +102,20 @@ public static partial class CueSDK

// 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

/// <summary>
/// Occurs when the SDK reports that a key is pressed.
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
/// </summary>
public static event EventHandler<KeyPressedEventArgs> KeyPressed;

#endregion

#region Methods
Expand Down Expand Up @@ -231,6 +247,11 @@ public static void Initialize(bool exclusiveAccess = false)
Throw(error, true);
}

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

InitializedDevices = new ReadOnlyCollection<ICueDevice>(devices);

IsInitialized = true;
Expand Down Expand Up @@ -322,6 +343,11 @@ public static void Reinitialize(bool exclusiveAccess)
|| 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;
}

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

private static void OnKeyPressed(IntPtr context, CorsairKeyId keyId, bool pressed)
=> KeyPressed?.Invoke(null, new KeyPressedEventArgs(keyId, pressed));

#endregion
}
}
46 changes: 46 additions & 0 deletions 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
{
/// <summary>
/// Contains list of all KeyIds available for all corsair devices.
/// </summary>
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,
}
}
36 changes: 36 additions & 0 deletions 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
{
/// <summary>
/// Contains list of all LEDs available for corsair keyboards.
/// </summary>
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;
}
}
30 changes: 30 additions & 0 deletions 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
{
/// <summary>
/// Contains list of all LEDs available for corsair mice.
/// </summary>
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;
}
}
41 changes: 41 additions & 0 deletions EventArgs/KeyPressedEventArgs.cs
@@ -0,0 +1,41 @@
// ReSharper disable MemberCanBePrivate.Global

using CUE.NET.Devices.Generic.Enums;

namespace CUE.NET.EventArgs
{
/// <summary>
/// Represents the data provided by the <see cref="CueSDK.KeyPressed"/>-event.
/// </summary>
public class KeyPressedEventArgs : System.EventArgs
{
#region Properties & Fields

/// <summary>
/// Gets the id of the key.
/// </summary>
public CorsairKeyId KeyId { get; }

/// <summary>
/// Gets the current status of the key (true = pressed, flase = released).
/// </summary>
public bool IsPressed { get; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="KeyPressedEventArgs"/> class.
/// </summary>
/// <param name="keyId">The id of the key.</param>
/// <param name="isPressed">The current status of the key (true = pressed, flase = released).</param>
public KeyPressedEventArgs(CorsairKeyId keyId, bool isPressed)
{
this.KeyId = keyId;
this.IsPressed = isPressed;
}

#endregion
}
}
2 changes: 2 additions & 0 deletions Examples/SimpleDevTest/Program.cs
Expand Up @@ -39,6 +39,8 @@ public static void Main(string[] args)
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;
Expand Down
10 changes: 10 additions & 0 deletions Native/_CUESDK.cs
Expand Up @@ -63,6 +63,7 @@ private static void LoadCUESDK()
_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()
Expand Down Expand Up @@ -100,6 +101,7 @@ private static void UnloadCUESDK()
private static CorsairReleaseControlPointer _corsairReleaseControlPointer;
private static CorsairPerformProtocolHandshakePointer _corsairPerformProtocolHandshakePointer;
private static CorsairGetLastErrorPointer _corsairGetLastErrorPointer;
private static CorsairRegisterKeypressCallbackPointer _corsairRegisterKeypressCallbackPointer;

#endregion

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

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairRegisterKeypressCallbackPointer(IntPtr callback, IntPtr context);

#endregion

// ReSharper disable EventExceptionNotDocumented
Expand Down Expand Up @@ -231,6 +236,11 @@ internal static CorsairError CorsairGetLastError()
return _corsairGetLastErrorPointer();
}

internal static bool CorsairRegisterKeypressCallback(IntPtr callback, IntPtr context)
{
return _corsairRegisterKeypressCallbackPointer(callback, context);
}

// ReSharper restore EventExceptionNotDocumented

#endregion
Expand Down

0 comments on commit 5091b33

Please sign in to comment.