0
|
1 ???using System; |
|
2 using VirtualBox; |
|
3 using System.Windows.Forms; |
|
4 |
|
5 namespace ConsoleApplication1 { |
|
6 public class VBoxKeyboardHelper { |
|
7 public IKeyboard Keyboard { get; private set; } |
|
8 |
|
9 public VBoxKeyboardHelper(IKeyboard kb) { |
|
10 Keyboard = kb; |
|
11 } |
|
12 |
|
13 public void SendKeyCode(Keys keycode, bool down) { |
|
14 int code; |
|
15 //Console.WriteLine("VBoxKeyboardHelper.SendKeyCode({1}={2},{0})", down, keycode, (int)keycode); |
|
16 int[] codes_az = { 0x1e, 0x30, 0x2e, 0x20, 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c }; //a-z |
|
17 int[] codes_num = {0x0b, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a}; //0123456789 |
|
18 int[] codes_Fkeys = { 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x57, 0x58 }; //F123456789,10,11,12 |
|
19 if (keycode >= Keys.A && keycode <= Keys.Z) { |
|
20 code = codes_az[keycode - Keys.A]; |
|
21 } else if (keycode >= Keys.D0 && keycode <= Keys.D9) { |
|
22 code = codes_num[keycode - Keys.D0]; |
|
23 } else if (keycode >= Keys.F1 && keycode <= Keys.F12) { |
|
24 code = codes_Fkeys[keycode - Keys.F1]; |
|
25 } else { |
|
26 switch (keycode) { |
|
27 case Keys.Space: code = 0x0239; break; |
|
28 case Keys.Left: code = 0xe04b; break; |
|
29 case Keys.Up: code = 0xe048; break; |
|
30 case Keys.Right: code = 0xe04d; break; |
|
31 case Keys.Down: code = 0xe050; break; |
|
32 case Keys.Enter: code = 0x1c; break; |
|
33 case Keys.LShiftKey: |
|
34 case Keys.RShiftKey: |
|
35 case Keys.ShiftKey: code = 0x2a; break; |
|
36 case Keys.Tab: code = 0x0f; break; |
|
37 case Keys.Back: code = 0x0e; break; |
|
38 case Keys.Escape: code = 0x01; break; |
|
39 case Keys.Home: code = 0xe047; break; |
|
40 case Keys.PageUp: code = 0xe049; break; |
|
41 case Keys.PageDown: code = 0xe051; break; |
|
42 case Keys.End: code = 0xe04f; break; |
|
43 case Keys.Insert: code = 0xe052; break; |
|
44 case Keys.ControlKey: |
|
45 case Keys.LControlKey: code = 0x1d; break; |
|
46 case Keys.RControlKey: code = 0xe01d; break; |
|
47 case Keys.Menu: |
|
48 case Keys.LMenu: code = 0x38; break; //Left Alt |
|
49 case Keys.RMenu: code = 0xe038; break; //Right Alt |
|
50 case Keys.LWin: code = 0xe05b; break; //Left windows key |
|
51 case Keys.RWin: code = 0xe05c; break; //Right windows key |
|
52 case Keys.Delete: code = 0xe053; break; |
|
53 case Keys.OemQuotes: code = 0x28; break; |
|
54 case Keys.OemQuestion: code = 0x35; break; |
|
55 case Keys.OemPeriod: code = 0x34; break; |
|
56 case Keys.OemMinus: code = 0x0c; break; |
|
57 case Keys.Oemplus: code = 0x0d; break; |
|
58 case Keys.Oemcomma: code = 0x33; break; |
|
59 case Keys.OemSemicolon: code = 0x27; break; |
|
60 case Keys.Oemtilde: code = 0x29; break; |
|
61 case Keys.OemCloseBrackets: code = 0x1b; break; |
|
62 case Keys.OemBackslash: code = 0x2b; break; |
|
63 case Keys.OemOpenBrackets: code = 0x1a; break; |
|
64 default: |
|
65 Console.Error.WriteLine("VBoxKeyboardHelper.SendKeyCode({1}={2},{0}) unknown key", down, keycode, (int)keycode); |
|
66 return; |
|
67 } |
|
68 } |
|
69 if ((code & 0xff00) != 0) kbdPutCode((code >> 8) & 0xff); |
|
70 kbdPutCode((code & 0xff) | (down ? 0 : 0x80)); |
|
71 } |
|
72 private void kbdPutCode(int code) { |
|
73 try { |
|
74 Keyboard.PutScancode(code); |
|
75 } catch (Exception ex) { |
|
76 Console.Error.WriteLine("VBoxKeyboardHelper.PutScancode Exception: {0}", ex.ToString()); |
|
77 } |
|
78 } |
|
79 } |
|
80 } |