Libs/VKeyUtil/VKeyUtil.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
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
public static class VKeyUtil
{
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern short VkKeyScanW(char ch);
 
    [DllImport("user32.dll")]
    private static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags);
 
    public static short GetKeyCodeFromChar(char ch)
    {
        return VkKeyScanW(ch);
    }
 
    public static string GetCharsFromKeys(Keys keys)
    {
        StringBuilder buf = new StringBuilder(10);
        byte[] keyboardState = new byte[256];
 
        ToUnicode((uint)keys, 0, keyboardState, buf, buf.Capacity, 0);
        return buf.ToString();
    }
}