1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.Runtime.InteropServices; |
5 |
using libWin32.Win32.Exceptions; |
6 |
|
7 |
namespace libWin32.Win32.Input |
8 |
{ |
9 |
public class KeyTranslator |
10 |
{ |
11 |
const uint MAPVK_VK_TO_VSC = 0x00; |
12 |
const uint MAPVK_VSC_TO_VK = 0x01; |
13 |
const uint MAPVK_VK_TO_CHAR = 0x02; |
14 |
const uint MAPVK_VSC_TO_VK_EX = 0x03; |
15 |
const uint MAPVK_VK_TO_VSC_EX = 0x04; |
16 |
[DllImport("user32.dll")] |
17 |
static extern uint MapVirtualKey(uint uCode, uint uMapType); |
18 |
|
19 |
public static void MapVirtualKeyToScanCode(short VKKey, out short ScanCode) |
20 |
{ |
21 |
ScanCode = 0; |
22 |
short scanCode = (short)KeyTranslator.MapVirtualKey((uint)VKKey, MAPVK_VK_TO_VSC); |
23 |
try |
24 |
{ |
25 |
ScanCode = scanCode; |
26 |
} |
27 |
catch (InvalidCastException ex) |
28 |
{ |
29 |
ScanCode = scanCode; |
30 |
StringBuilder builder = new StringBuilder(); |
31 |
builder.AppendFormat("VirtualKey: 0x{0:x2} translates to ScanCode: 0x{1:x8} which is not implemented.", VKKey, scanCode); |
32 |
throw new UnimplimentedDirectXScanCodeException(builder.ToString(), ex); |
33 |
} |
34 |
} |
35 |
public static void MapVirtualKeyToExtendedScanCode(short VKKey, out short ScanCode) |
36 |
{ |
37 |
ScanCode = 0; |
38 |
short scanCode = (short)KeyTranslator.MapVirtualKey((uint)VKKey, MAPVK_VK_TO_VSC_EX); |
39 |
try |
40 |
{ |
41 |
ScanCode = scanCode; |
42 |
} |
43 |
catch (InvalidCastException ex) |
44 |
{ |
45 |
ScanCode = scanCode; |
46 |
StringBuilder builder = new StringBuilder(); |
47 |
builder.AppendFormat("VirtualKey: 0x{0:x2} translates to ScanCode: 0x{1:x8} which is not implemented.", VKKey, scanCode); |
48 |
throw new UnimplimentedDirectXScanCodeException(builder.ToString(), ex); |
49 |
} |
50 |
} |
51 |
|
52 |
public static void MapScanCodeToVirtualKey(short ScanCode, out uint VKKey) |
53 |
{ |
54 |
uint vkKey = KeyTranslator.MapVirtualKey((uint)ScanCode, MAPVK_VSC_TO_VK); |
55 |
VKKey = vkKey; |
56 |
} |
57 |
public static void MapScanCodeToExtendedVirtualKey(short ScanCode, out uint VKKey) |
58 |
{ |
59 |
uint vkKey = KeyTranslator.MapVirtualKey((uint)ScanCode, MAPVK_VSC_TO_VK_EX); |
60 |
VKKey = vkKey; |
61 |
} |
62 |
|
63 |
public static void MapVirtualKeyToChar(byte VKKey, out char ScanCode) |
64 |
{ |
65 |
uint scanCode = KeyTranslator.MapVirtualKey((uint)VKKey, MAPVK_VK_TO_CHAR); |
66 |
ScanCode = (char)scanCode; |
67 |
} |
68 |
} |
69 |
} |