Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
457 lines (388 loc) · 16.4 KB
/
Copy pathNativeMethods.cs
File metadata and controls
457 lines (388 loc) · 16.4 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using Microsoft.Win32;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace KeyboardLayoutSwitcher;
internal static class NativeMethods
{
public const int WM_INPUT = 0x00FF;
public const int WM_INPUT_DEVICE_CHANGE = 0x00FE;
private const int WM_INPUTLANGCHANGEREQUEST = 0x0050;
private const uint RID_INPUT = 0x10000003;
private const uint RIDI_DEVICENAME = 0x20000007;
private const uint RIDEV_INPUTSINK = 0x00000100;
private const uint RIDEV_DEVNOTIFY = 0x00002000;
private const uint RIM_TYPEKEYBOARD = 1;
private const ushort HID_USAGE_PAGE_GENERIC = 0x01;
private const ushort HID_USAGE_GENERIC_KEYBOARD = 0x06;
private const ushort RI_KEY_BREAK = 0x01;
private const uint KLF_ACTIVATE = 0x00000001;
private const uint KLF_SUBSTITUTE_OK = 0x00000002;
private const int CR_SUCCESS = 0;
private const uint CM_DRP_DEVICEDESC = 0x00000001;
private const uint CM_DRP_FRIENDLYNAME = 0x0000000D;
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTDEVICE
{
public ushort UsagePage;
public ushort Usage;
public uint Flags;
public IntPtr Target;
}
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTDEVICELIST
{
public IntPtr Device;
public uint Type;
}
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTHEADER
{
public uint Type;
public uint Size;
public IntPtr Device;
public IntPtr WParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct RAWKEYBOARD
{
public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VKey;
public uint Message;
public uint ExtraInformation;
}
internal readonly record struct RawKeyboardEvent(IntPtr DeviceHandle, bool IsKeyDown);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterRawInputDevices(RAWINPUTDEVICE[] devices, uint deviceCount, uint deviceSize);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetRawInputDeviceList([Out] RAWINPUTDEVICELIST[]? devices, ref uint deviceCount, uint deviceSize);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern uint GetRawInputDeviceInfo(IntPtr device, uint command, StringBuilder? data, ref uint dataSize);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetRawInputData(IntPtr rawInput, uint command, [Out] byte[]? data, ref uint dataSize, uint headerSize);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr LoadKeyboardLayout(string keyboardLayoutId, uint flags);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr window, out uint processId);
[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint threadId);
[DllImport("user32.dll")]
private static extern IntPtr PostMessage(IntPtr window, int message, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern IntPtr ActivateKeyboardLayout(IntPtr keyboardLayout, uint flags);
[DllImport("cfgmgr32.dll", CharSet = CharSet.Unicode)]
private static extern int CM_Locate_DevNodeW(out uint deviceInstance, string deviceId, uint flags);
[DllImport("cfgmgr32.dll")]
private static extern int CM_Get_Parent(out uint parentDeviceInstance, uint deviceInstance, uint flags);
[DllImport("cfgmgr32.dll", CharSet = CharSet.Unicode)]
private static extern int CM_Get_DevNode_Registry_PropertyW(
uint deviceInstance,
uint property,
out uint registryDataType,
StringBuilder? buffer,
ref uint bufferSize,
uint flags);
public static void RegisterKeyboardInput(IntPtr target)
{
var devices = new[]
{
new RAWINPUTDEVICE
{
UsagePage = HID_USAGE_PAGE_GENERIC,
Usage = HID_USAGE_GENERIC_KEYBOARD,
Flags = RIDEV_INPUTSINK | RIDEV_DEVNOTIFY,
Target = target
}
};
if (!RegisterRawInputDevices(devices, (uint)devices.Length, (uint)Marshal.SizeOf<RAWINPUTDEVICE>()))
{
throw new InvalidOperationException($"RegisterRawInputDevices failed: {Marshal.GetLastWin32Error()}");
}
}
public static IReadOnlyList<KeyboardDevice> EnumerateKeyboards()
{
uint count = 0;
_ = GetRawInputDeviceList(null, ref count, (uint)Marshal.SizeOf<RAWINPUTDEVICELIST>());
if (count == 0)
{
return Array.Empty<KeyboardDevice>();
}
var devices = new RAWINPUTDEVICELIST[count];
var result = GetRawInputDeviceList(devices, ref count, (uint)Marshal.SizeOf<RAWINPUTDEVICELIST>());
if (result == uint.MaxValue)
{
return Array.Empty<KeyboardDevice>();
}
return devices
.Take((int)result)
.Where(device => device.Type == RIM_TYPEKEYBOARD)
.Select(device => CreateKeyboardDevice(device.Device))
.GroupBy(device => device.DevicePath, StringComparer.OrdinalIgnoreCase)
.Select(group => group.First())
.OrderBy(device => device.DisplayName, StringComparer.CurrentCultureIgnoreCase)
.ToList();
}
public static IReadOnlyList<KeyboardLayoutInfo> EnumerateInstalledLayouts()
{
var substitutes = ReadKeyboardLayoutSubstitutes();
var layouts = new List<KeyboardLayoutInfo>();
foreach (var configuredId in ReadPreloadedLayoutIds())
{
var effectiveId = substitutes.TryGetValue(configuredId, out var substituteId) ? substituteId : configuredId;
var handle = LoadKeyboardLayout(configuredId, KLF_SUBSTITUTE_OK);
var layoutName = ReadLayoutDisplayName(effectiveId) ?? ReadLayoutDisplayName(configuredId);
var languageName = ReadLanguageDisplayName(configuredId);
layouts.Add(new KeyboardLayoutInfo
{
Id = configuredId,
ActiveHklId = ToHexId(handle),
DisplayName = CreateLayoutDisplayName(languageName, layoutName, configuredId),
Handle = handle
});
}
return layouts
.GroupBy(layout => layout.Id, StringComparer.OrdinalIgnoreCase)
.Select(group => group.First())
.OrderBy(layout => layout.DisplayName, StringComparer.CurrentCultureIgnoreCase)
.ToList();
}
public static RawKeyboardEvent? ReadKeyboardEvent(IntPtr rawInput)
{
uint size = 0;
var headerSize = (uint)Marshal.SizeOf<RAWINPUTHEADER>();
if (GetRawInputData(rawInput, RID_INPUT, null, ref size, headerSize) == uint.MaxValue || size < headerSize)
{
return null;
}
var data = new byte[size];
if (GetRawInputData(rawInput, RID_INPUT, data, ref size, headerSize) == uint.MaxValue)
{
return null;
}
var pinned = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var pointer = pinned.AddrOfPinnedObject();
var header = Marshal.PtrToStructure<RAWINPUTHEADER>(pointer);
if (header.Type != RIM_TYPEKEYBOARD)
{
return null;
}
var keyboard = Marshal.PtrToStructure<RAWKEYBOARD>(IntPtr.Add(pointer, (int)headerSize));
return new RawKeyboardEvent(header.Device, (keyboard.Flags & RI_KEY_BREAK) == 0);
}
finally
{
pinned.Free();
}
}
public static bool IsForegroundKeyboardLayout(string layoutId)
{
var targetLayout = LoadKeyboardLayout(layoutId, KLF_SUBSTITUTE_OK);
if (targetLayout == IntPtr.Zero)
{
return false;
}
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return false;
}
var threadId = GetWindowThreadProcessId(foregroundWindow, out _);
return ToHexId(GetKeyboardLayout(threadId)).Equals(ToHexId(targetLayout), StringComparison.OrdinalIgnoreCase);
}
public static bool SetForegroundKeyboardLayout(string layoutId)
{
var handle = LoadKeyboardLayout(layoutId, KLF_ACTIVATE | KLF_SUBSTITUTE_OK);
if (handle == IntPtr.Zero)
{
return false;
}
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return ActivateKeyboardLayout(handle, 0) != IntPtr.Zero;
}
return PostMessage(foregroundWindow, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, handle) != IntPtr.Zero;
}
private static KeyboardDevice CreateKeyboardDevice(IntPtr handle)
{
var path = GetDeviceName(handle);
var pnpInstanceId = TryCreatePnpInstanceId(path);
var pnpDisplayName = pnpInstanceId is null ? null : ReadBestPnpDisplayName(pnpInstanceId);
var isInternal = path.Contains("ACPI", StringComparison.OrdinalIgnoreCase) ||
path.Contains("PNP030", StringComparison.OrdinalIgnoreCase);
var hardwareIdentifier = ExtractHardwareIdentifier(path);
return new KeyboardDevice
{
Handle = handle,
DevicePath = path,
DisplayName = CreateKeyboardDisplayName(pnpDisplayName, isInternal, hardwareIdentifier),
Detail = pnpInstanceId is null ? path : $"{pnpInstanceId} · {hardwareIdentifier ?? "identificador PnP"}"
};
}
private static string GetDeviceName(IntPtr device)
{
uint size = 0;
_ = GetRawInputDeviceInfo(device, RIDI_DEVICENAME, null, ref size);
if (size == 0)
{
return device.ToString();
}
var builder = new StringBuilder((int)size + 1);
_ = GetRawInputDeviceInfo(device, RIDI_DEVICENAME, builder, ref size);
return builder.ToString();
}
private static IReadOnlyList<string> ReadPreloadedLayoutIds()
{
using var key = Registry.CurrentUser.OpenSubKey(@"Keyboard Layout\Preload");
if (key is null)
{
return Array.Empty<string>();
}
return key.GetValueNames()
.OrderBy(name => int.TryParse(name, out var order) ? order : int.MaxValue)
.Select(name => key.GetValue(name) as string)
.Where(value => TryNormalizeLayoutId(value, out _))
.Select(value => NormalizeLayoutId(value!))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
}
private static Dictionary<string, string> ReadKeyboardLayoutSubstitutes()
{
using var key = Registry.CurrentUser.OpenSubKey(@"Keyboard Layout\Substitutes");
if (key is null)
{
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
return key.GetValueNames()
.Select(name => new { Name = name, Value = key.GetValue(name) as string })
.Where(entry => TryNormalizeLayoutId(entry.Name, out _) && TryNormalizeLayoutId(entry.Value, out _))
.ToDictionary(
entry => NormalizeLayoutId(entry.Name),
entry => NormalizeLayoutId(entry.Value!),
StringComparer.OrdinalIgnoreCase);
}
private static string? ReadLayoutDisplayName(string layoutId)
{
using var key = Registry.LocalMachine.OpenSubKey($@"SYSTEM\CurrentControlSet\Control\Keyboard Layouts\{layoutId}");
return key?.GetValue("Layout Text") as string;
}
private static string ReadLanguageDisplayName(string layoutId)
{
if (!uint.TryParse(layoutId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var numericId))
{
return layoutId;
}
try
{
return CultureInfo.GetCultureInfo((int)(numericId & 0xFFFF)).DisplayName;
}
catch (CultureNotFoundException)
{
return $"Idioma 0x{numericId & 0xFFFF:X4}";
}
}
private static string CreateLayoutDisplayName(string languageName, string? layoutName, string layoutId)
{
return string.IsNullOrWhiteSpace(layoutName) || layoutName.Equals(languageName, StringComparison.CurrentCultureIgnoreCase)
? $"{languageName} [{layoutId}]"
: $"{languageName} — {layoutName} [{layoutId}]";
}
private static bool TryNormalizeLayoutId(string? value, out uint numericId)
{
return uint.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out numericId);
}
private static string NormalizeLayoutId(string value)
{
_ = TryNormalizeLayoutId(value, out var numericId);
return numericId.ToString("X8");
}
private static string ToHexId(IntPtr handle)
{
return unchecked((uint)handle.ToInt64()).ToString("X8");
}
private static string? TryCreatePnpInstanceId(string rawInputPath)
{
var path = rawInputPath.TrimStart('\\', '?');
var segments = path.Split('#');
return segments.Length >= 3 ? string.Join("\\", segments.Take(3)) : null;
}
private static string? ReadBestPnpDisplayName(string pnpInstanceId)
{
if (CM_Locate_DevNodeW(out var deviceInstance, pnpInstanceId, 0) != CR_SUCCESS)
{
return null;
}
string? fallback = null;
for (var level = 0; level < 5; level++)
{
var friendlyName = ReadDeviceNodeProperty(deviceInstance, CM_DRP_FRIENDLYNAME);
var deviceDescription = ReadDeviceNodeProperty(deviceInstance, CM_DRP_DEVICEDESC);
var candidate = friendlyName ?? deviceDescription;
if (!string.IsNullOrWhiteSpace(candidate))
{
fallback ??= candidate;
if (!IsGenericKeyboardName(candidate))
{
return candidate;
}
}
if (CM_Get_Parent(out deviceInstance, deviceInstance, 0) != CR_SUCCESS)
{
break;
}
}
return fallback;
}
private static string? ReadDeviceNodeProperty(uint deviceInstance, uint property)
{
uint size = 0;
if (CM_Get_DevNode_Registry_PropertyW(deviceInstance, property, out _, null, ref size, 0) != CR_SUCCESS || size == 0)
{
return null;
}
var buffer = new StringBuilder((int)(size / sizeof(char)) + 1);
if (CM_Get_DevNode_Registry_PropertyW(deviceInstance, property, out _, buffer, ref size, 0) != CR_SUCCESS)
{
return null;
}
var value = buffer.ToString().Trim();
var separator = value.LastIndexOf(';');
return separator >= 0 ? value[(separator + 1)..].Trim() : value;
}
private static bool IsGenericKeyboardName(string value)
{
return value.Contains("HID Keyboard Device", StringComparison.OrdinalIgnoreCase) ||
value.Contains("HID-compliant keyboard", StringComparison.OrdinalIgnoreCase) ||
value.Contains("USB Input Device", StringComparison.OrdinalIgnoreCase) ||
value.Contains("USB Composite Device", StringComparison.OrdinalIgnoreCase) ||
value.Equals("Keyboard Device", StringComparison.OrdinalIgnoreCase);
}
private static string CreateKeyboardDisplayName(string? pnpDisplayName, bool isInternal, string? hardwareIdentifier)
{
var kind = isInternal ? "Teclado interno" : "Teclado externo";
if (string.IsNullOrWhiteSpace(pnpDisplayName) || IsGenericKeyboardName(pnpDisplayName))
{
return hardwareIdentifier is null ? kind : $"{kind} — {hardwareIdentifier}";
}
return isInternal ? $"{kind} — {pnpDisplayName}" : pnpDisplayName;
}
private static string? ExtractHardwareIdentifier(string path)
{
var upper = path.ToUpperInvariant();
var vidIndex = upper.IndexOf("VID_", StringComparison.Ordinal);
var pidIndex = upper.IndexOf("PID_", StringComparison.Ordinal);
if (vidIndex < 0 || pidIndex < 0)
{
return null;
}
var vid = path.Substring(vidIndex, Math.Min(8, path.Length - vidIndex));
var pid = path.Substring(pidIndex, Math.Min(8, path.Length - pidIndex));
return $"{vid}, {pid}";
}
}