-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardLayoutNative.cs
More file actions
50 lines (41 loc) · 1.35 KB
/
Copy pathKeyboardLayoutNative.cs
File metadata and controls
50 lines (41 loc) · 1.35 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
using System.Runtime.InteropServices;
using System.Text;
namespace Flow.Launcher.Plugin.LayoutFallback;
internal static class KeyboardLayoutNative
{
private const int VkShift = 0x10;
private const uint MapVkVkToVsc = 0;
private const uint ToUnicodeNoStateChange = 0x4;
[DllImport("user32.dll")]
private static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int ToUnicodeEx(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[Out] StringBuilder pwszBuff,
int cchBuff,
uint wFlags,
IntPtr dwhkl);
public static char? TryGetCharacter(IntPtr layoutHandle, int virtualKey, bool shift)
{
var keyState = new byte[256];
if (shift)
keyState[VkShift] = 0x80;
var scanCode = MapVirtualKeyEx((uint)virtualKey, MapVkVkToVsc, layoutHandle);
if (scanCode == 0)
return null;
var buffer = new StringBuilder(8);
var result = ToUnicodeEx(
(uint)virtualKey,
scanCode,
keyState,
buffer,
buffer.Capacity,
ToUnicodeNoStateChange,
layoutHandle);
if (result <= 0 || buffer.Length == 0)
return null;
return buffer[0];
}
}