mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2025-12-13 00:58:31 +00:00
Added some extensions for KeyGroups
This commit is contained in:
parent
4e1a1d11bd
commit
ced6c299b0
@ -85,6 +85,7 @@
|
|||||||
<Compile Include="Devices\Keyboard\CorsairKeyboard.cs" />
|
<Compile Include="Devices\Keyboard\CorsairKeyboard.cs" />
|
||||||
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
|
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
|
||||||
<Compile Include="Devices\Mouse\CorsairMouse.cs" />
|
<Compile Include="Devices\Mouse\CorsairMouse.cs" />
|
||||||
|
<Compile Include="Devices\Keyboard\Extensions\KeyGroupExtension.cs" />
|
||||||
<Compile Include="Helper\RectangleHelper.cs" />
|
<Compile Include="Helper\RectangleHelper.cs" />
|
||||||
<Compile Include="Native\_CorsairDeviceInfo.cs" />
|
<Compile Include="Native\_CorsairDeviceInfo.cs" />
|
||||||
<Compile Include="Native\_CorsairLedColor.cs" />
|
<Compile Include="Native\_CorsairLedColor.cs" />
|
||||||
|
|||||||
@ -51,7 +51,7 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(_keys.Values.ToList());
|
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(_keys.Values.ToList());
|
||||||
|
|
||||||
public Color Color { get; set; } = Color.Transparent;
|
public Color Color { get; set; } = Color.Transparent;
|
||||||
|
|
||||||
private readonly IList<IKeyGroup> _keyGroups = new List<IKeyGroup>();
|
private readonly IList<IKeyGroup> _keyGroups = new List<IKeyGroup>();
|
||||||
@ -92,19 +92,20 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
base.UpdateLeds(forceUpdate);
|
base.UpdateLeds(forceUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AttachKeyGroup(IKeyGroup keyGroup)
|
public bool AttachKeyGroup(IKeyGroup keyGroup)
|
||||||
{
|
{
|
||||||
if (keyGroup == null) return;
|
if (keyGroup == null || _keyGroups.Contains(keyGroup)) return false;
|
||||||
|
|
||||||
if (!_keyGroups.Contains(keyGroup))
|
_keyGroups.Add(keyGroup);
|
||||||
_keyGroups.Add(keyGroup);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DetachKeyGroup(IKeyGroup keyGroup)
|
public bool DetachKeyGroup(IKeyGroup keyGroup)
|
||||||
{
|
{
|
||||||
if (keyGroup == null) return;
|
if (keyGroup == null || !_keyGroups.Contains(keyGroup)) return false;
|
||||||
|
|
||||||
_keyGroups.Remove(keyGroup);
|
_keyGroups.Remove(keyGroup);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeKeys()
|
private void InitializeKeys()
|
||||||
|
|||||||
46
Devices/Keyboard/Extensions/KeyGroupExtension.cs
Normal file
46
Devices/Keyboard/Extensions/KeyGroupExtension.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using CUE.NET.Devices.Keyboard.Enums;
|
||||||
|
using CUE.NET.Devices.Keyboard.Keys;
|
||||||
|
|
||||||
|
namespace CUE.NET.Devices.Keyboard.Extensions
|
||||||
|
{
|
||||||
|
public static class KeyGroupExtension
|
||||||
|
{
|
||||||
|
public static SimpleKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup)
|
||||||
|
{
|
||||||
|
SimpleKeyGroup simpleKeyGroup = keyGroup as SimpleKeyGroup;
|
||||||
|
if (simpleKeyGroup == null)
|
||||||
|
{
|
||||||
|
bool wasAttached = keyGroup.Detach();
|
||||||
|
simpleKeyGroup = new SimpleKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Color = keyGroup.Color };
|
||||||
|
}
|
||||||
|
return simpleKeyGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
|
||||||
|
{
|
||||||
|
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
|
||||||
|
foreach (CorsairKeyboardKeyId keyId in keyIds)
|
||||||
|
simpleKeyGroup.RemoveKey(keyId);
|
||||||
|
return simpleKeyGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds)
|
||||||
|
{
|
||||||
|
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
|
||||||
|
foreach (CorsairKey key in keyIds)
|
||||||
|
simpleKeyGroup.RemoveKey(key);
|
||||||
|
return simpleKeyGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool Attach(this BaseKeyGroup keyGroup)
|
||||||
|
{
|
||||||
|
return keyGroup.Keyboard?.AttachKeyGroup(keyGroup) ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool Detach(this BaseKeyGroup keyGroup)
|
||||||
|
{
|
||||||
|
return keyGroup.Keyboard?.DetachKeyGroup(keyGroup) ?? false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using CUE.NET.Devices.Keyboard.Extensions;
|
||||||
|
|
||||||
namespace CUE.NET.Devices.Keyboard.Keys
|
namespace CUE.NET.Devices.Keyboard.Keys
|
||||||
{
|
{
|
||||||
@ -8,7 +9,7 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
|||||||
{
|
{
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
protected CorsairKeyboard Keyboard { get; }
|
internal CorsairKeyboard Keyboard { get; }
|
||||||
|
|
||||||
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GroupKeys);
|
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GroupKeys);
|
||||||
protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();
|
protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();
|
||||||
@ -24,18 +25,7 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
|||||||
this.Keyboard = keyboard;
|
this.Keyboard = keyboard;
|
||||||
|
|
||||||
if (autoAttach)
|
if (autoAttach)
|
||||||
keyboard.AttachKeyGroup(this);
|
this.Attach();
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
|
|
||||||
public void MergeKeys(IKeyGroup groupToMerge)
|
|
||||||
{
|
|
||||||
foreach (CorsairKey key in groupToMerge.Keys)
|
|
||||||
if (!GroupKeys.Contains(key))
|
|
||||||
GroupKeys.Add(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@ -6,18 +6,26 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
|||||||
{
|
{
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public SimpleKeyGroup(CorsairKeyboard keyboard)
|
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
|
||||||
: base(keyboard)
|
: base(keyboard, autoAttach)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
|
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
|
||||||
: base(keyboard)
|
: this(keyboard, true, keys)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKey[] keys)
|
||||||
|
: base(keyboard, autoAttach)
|
||||||
{
|
{
|
||||||
AddKey(keys);
|
AddKey(keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
|
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
|
||||||
: base(keyboard)
|
: this(keyboard, true, keys)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKeyboardKeyId[] keys)
|
||||||
|
: base(keyboard, autoAttach)
|
||||||
{
|
{
|
||||||
AddKey(keys);
|
AddKey(keys);
|
||||||
}
|
}
|
||||||
@ -67,6 +75,13 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
|||||||
return ContainsKey(Keyboard[keyId]);
|
return ContainsKey(Keyboard[keyId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void MergeKeys(IKeyGroup groupToMerge)
|
||||||
|
{
|
||||||
|
foreach (CorsairKey key in groupToMerge.Keys)
|
||||||
|
if (!GroupKeys.Contains(key))
|
||||||
|
GroupKeys.Add(key);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using CUE.NET;
|
|||||||
using CUE.NET.Devices.Generic.Enums;
|
using CUE.NET.Devices.Generic.Enums;
|
||||||
using CUE.NET.Devices.Keyboard;
|
using CUE.NET.Devices.Keyboard;
|
||||||
using CUE.NET.Devices.Keyboard.Enums;
|
using CUE.NET.Devices.Keyboard.Enums;
|
||||||
|
using CUE.NET.Devices.Keyboard.Extensions;
|
||||||
using CUE.NET.Devices.Keyboard.Keys;
|
using CUE.NET.Devices.Keyboard.Keys;
|
||||||
using CUE.NET.Exceptions;
|
using CUE.NET.Exceptions;
|
||||||
|
|
||||||
@ -34,9 +35,11 @@ namespace SimpleDevTest
|
|||||||
if (keyboard == null)
|
if (keyboard == null)
|
||||||
throw new WrapperException("No keyboard found");
|
throw new WrapperException("No keyboard found");
|
||||||
|
|
||||||
//Ink all numbers on the keypad purple
|
//Ink all numbers on the keypad except the '5' purple, we want that to be gray
|
||||||
RectangleKeyGroup purpleGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3)
|
SimpleKeyGroup purpleGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3)
|
||||||
{ Color = Color.Purple };
|
{ Color = Color.Purple }
|
||||||
|
.Exclude(CorsairKeyboardKeyId.Keypad5);
|
||||||
|
keyboard[CorsairKeyboardKeyId.Keypad5].Led.Color = Color.Gray;
|
||||||
|
|
||||||
// Ink the Keys 'r', 'g', 'b' in their respective color
|
// Ink the Keys 'r', 'g', 'b' in their respective color
|
||||||
// The char access seems to fail for everything except letters (SDK doesn't return a valid keyId)
|
// The char access seems to fail for everything except letters (SDK doesn't return a valid keyId)
|
||||||
@ -77,9 +80,9 @@ namespace SimpleDevTest
|
|||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
|
||||||
// Remove all the groups we created above to clear the keyboard
|
// Remove all the groups we created above to clear the keyboard
|
||||||
keyboard.DetachKeyGroup(purpleGroup);
|
purpleGroup.Detach();
|
||||||
keyboard.DetachKeyGroup(whiteGroup);
|
whiteGroup.Detach();
|
||||||
keyboard.DetachKeyGroup(yellowGroup);
|
yellowGroup.Detach();
|
||||||
|
|
||||||
// Flash whole keyboard three times to ... well ... just to make it happen
|
// Flash whole keyboard three times to ... well ... just to make it happen
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user