-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardManager.cs
148 lines (118 loc) · 4.75 KB
/
KeyboardManager.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using MsiHid;
using Mystic_Tools.ActiveWindow;
using Mystic_Tools.Utils;
using System.Diagnostics;
using System.Drawing;
using NAudio.Wave;
namespace Mystic_Tools
{
/// <summary>
/// キーボードの管理を行うクラスです。
/// </summary>
internal class KeyboardManager
{
/// <summary>
/// KeyboardManagerクラスの新しいインスタンスを初期化します。
/// </summary>
public KeyboardManager()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit);
watcher = new Watcher();
watcher.WindowChanged += (sender, e) =>
{
WhenActiveWindowChanged();
};
}
/// <summary>
/// ウィンドウの監視を開始します。
/// </summary>
public void StartWindowWatcher()
{
watcher.Start();
foreach (string file in Directory.EnumerateFiles(configDirectory, "*.ini"))
{
configs.Add(new Config(file));
Console.WriteLine("Setting Loaded : {0}", configs[^1].SettingName);
}
}
/// <summary>
/// ウィンドウの監視を停止します。
/// </summary>
public void StopWindowWatcher()
{
watcher.Stop();
}
private void WhenActiveWindowChanged()
{
string activeWindowPath = ActiveWindowPath.GetPath();
if (activeWindowPath == oldActiveWindowPath)
{
return;
}
Console.WriteLine(activeWindowPath);
Config? config = configs.FirstOrDefault(c => c.ExecutablePath == activeWindowPath);
if (config == null)
{
return;
}
Console.WriteLine(HID_Basic.IsDeviceConnect(3504, 2908, 0, 0));
keyboard = new GK50LP_TKL();
Console.WriteLine(keyboard.Init());
keyboard.ResetCustomize();
foreach (var key in config.color)
{
Console.WriteLine(key.Key + ": " + key.Value[0] + ", " + key.Value[1] + ", " + key.Value[2]);
keyboard.SetCustomizeRGBColor(keyboard.GetKeyIndexByKey(key.Key), key.Value[0], key.Value[1], key.Value[2]);
}
keyboard.SetCustomize(config.Brightness);
Console.WriteLine(keyboard.CloseDevice());
oldActiveWindowPath = ActiveWindowPath.GetPath();
}
public void VideoPlay(string video_path)
{
var player = new WasapiOut();
var reader = new MediaFoundationReader(video_path);
player.Init(reader);
Stopwatch stopwatch = new();
VideoFrameAnalyzer videoFrameAnalyzer = new(video_path);
Console.WriteLine(HID_Basic.IsDeviceConnect(3504, 2908, 0, 0));
GK50LP_TKL keyboard = new();
Console.WriteLine(keyboard.Init());
int currentFrame = 0;
Bitmap frameBitmap = videoFrameAnalyzer.GetFrameColorAtFrame(currentFrame);
CoordinateConverter coordinateConverter = new CoordinateConverter(frameBitmap.Width, frameBitmap.Height);
Process.Start("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", video_path);
Thread.Sleep(500);
//player.Play();
stopwatch.Start();
while (true)
{
long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
currentFrame = (int)(elapsedMilliseconds / 1000.0 * videoFrameAnalyzer.FrameRate);
if (currentFrame >= videoFrameAnalyzer.FrameCount)
{
break;
}
frameBitmap = videoFrameAnalyzer.GetFrameColorAtFrame(currentFrame);
for (int i = 0; i < 92; i ++)
{
int[] keyCoordinate = coordinateConverter.KeyIndexToCoordinate(i);
keyboard.SetCustomizeRGBColor(i, frameBitmap.GetPixel(keyCoordinate[0], keyCoordinate[1]));
}
keyboard.SetCustomize(255);
Thread.Sleep(100);
}
Console.WriteLine(keyboard.CloseDevice());
}
private void ProcessExit(object? sender, EventArgs? e)
{
keyboard?.CloseDevice();
watcher?.Stop();
}
private static GK50LP_TKL? keyboard;
public string configDirectory = "config";
public List<Config> configs = [];
private readonly Watcher watcher;
private string oldActiveWindowPath = string.Empty;
}
}