forked from cschladetsch/LogiNumPad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (96 loc) · 3.84 KB
/
Program.cs
File metadata and controls
116 lines (96 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Linq;
using System.Timers;
using System.Drawing;
using System.Configuration;
using System.Runtime.InteropServices;
using LedCSharp;
namespace LogiNumLock
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
[DllImport("LogitechLedEnginesWrapper ", CallingConvention = CallingConvention.Cdecl)]
public static extern bool LogiLedFlashLighting(int redPercentage, int greenPercentage, int bluePercentage, int milliSecondsDuration, int milliSecondsInterval);
private const int VK_NUMLOCK = 0x90;
private bool _first = true;
private bool _numLocked;
private Color _keyPadOn;
private Color _keyPadOff;
private Color _numLockOn;
private Color _numLockOff;
private readonly keyboardNames[] _numKeys =
{
keyboardNames.NUM_ZERO,
keyboardNames.NUM_ONE,
keyboardNames.NUM_TWO,
keyboardNames.NUM_THREE,
keyboardNames.NUM_FOUR,
keyboardNames.NUM_FIVE,
keyboardNames.NUM_SIX,
keyboardNames.NUM_SEVEN,
keyboardNames.NUM_EIGHT,
keyboardNames.NUM_NINE,
keyboardNames.NUM_PERIOD,
};
static void Main(string[] args)
=> new Program().Run();
private void Run()
{
if (!LogitechGSDK.LogiLedInit())
{
Console.Error.WriteLine("Failed to start LogiTech SDK. Plug in a keyboard or something.");
return;
}
LogitechGSDK.LogiLedSetTargetDevice(LogitechGSDK.LOGI_DEVICETYPE_ALL);
GetAppSettings();
SetKeyState();
const int pollTime = 60; // milliseconds
var timer = new Timer(pollTime) {Enabled = true};
timer.Elapsed += (sender, eventArgs) => { SetKeyState(); };
Console.WriteLine("Press \"ENTER\" to close.");
Console.ReadLine();
LogitechGSDK.LogiLedShutdown();
}
private void GetAppSettings()
{
_keyPadOn = ColorFromAppSettings("keyPadOn");
_keyPadOff = ColorFromAppSettings("keyPadOff");
_numLockOn = ColorFromAppSettings("numLockOn");
_numLockOff = ColorFromAppSettings("numLockOff");
}
public static Color ColorFromAppSettings(string key)
{
var text = ConfigurationManager.AppSettings.Get(key);
var channels = text.Split(new[]{','}).Select(c => int.Parse(c)).ToArray();
return Color.FromArgb(1, channels[0], channels[1], channels[2]);
}
private void SetKeyState()
{
var numLocked = ((ushort) GetKeyState(VK_NUMLOCK) & 0xffff) == 0;
ToggleNumKeys(numLocked);
}
private void ToggleNumKeys(bool numLocked)
{
if (_numLocked == numLocked || _first)
{
_first = false;
return;
}
_numLocked = numLocked;
SetNumKeyColors();
}
private void SetNumKeyColors()
{
Color color = _numLocked ? _keyPadOff : _keyPadOn;
Console.WriteLine($"NumLock = {!_numLocked}");
foreach (var key in _numKeys)
{
LogitechGSDK.LogiLedSetLightingForKeyWithKeyName(key, color.R, color.G, color.B);
}
var numLockColor = _numLocked ? _numLockOff : _numLockOn;
LogitechGSDK.LogiLedSetLightingForKeyWithKeyName(keyboardNames.NUM_LOCK, numLockColor.R, numLockColor.G, numLockColor.B);
}
}
}