annotate VBoxFrontend/VBoxKeyboardHelper.cs @ 0:e1ec7bf71313

Initial commit
author Ivo Smits
date Wed, 04 May 2011 00:59:43 +0200
parents
children 00fb4879d273
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
1 ???using System;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
2 using VirtualBox;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
3 using System.Windows.Forms;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
4
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
5 namespace ConsoleApplication1 {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
6 public class VBoxKeyboardHelper {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
7 public IKeyboard Keyboard { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
8
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
9 public VBoxKeyboardHelper(IKeyboard kb) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
10 Keyboard = kb;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
11 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
12
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
13 public void SendKeyCode(Keys keycode, bool down) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
14 int code;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
15 //Console.WriteLine("VBoxKeyboardHelper.SendKeyCode({1}={2},{0})", down, keycode, (int)keycode);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
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
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
17 int[] codes_num = {0x0b, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a}; //0123456789
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
18 int[] codes_Fkeys = { 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x57, 0x58 }; //F123456789,10,11,12
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
19 if (keycode >= Keys.A && keycode <= Keys.Z) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
20 code = codes_az[keycode - Keys.A];
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
21 } else if (keycode >= Keys.D0 && keycode <= Keys.D9) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
22 code = codes_num[keycode - Keys.D0];
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
23 } else if (keycode >= Keys.F1 && keycode <= Keys.F12) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
24 code = codes_Fkeys[keycode - Keys.F1];
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
25 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
26 switch (keycode) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
27 case Keys.Space: code = 0x0239; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
28 case Keys.Left: code = 0xe04b; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
29 case Keys.Up: code = 0xe048; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
30 case Keys.Right: code = 0xe04d; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
31 case Keys.Down: code = 0xe050; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
32 case Keys.Enter: code = 0x1c; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
33 case Keys.LShiftKey:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
34 case Keys.RShiftKey:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
35 case Keys.ShiftKey: code = 0x2a; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
36 case Keys.Tab: code = 0x0f; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
37 case Keys.Back: code = 0x0e; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
38 case Keys.Escape: code = 0x01; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
39 case Keys.Home: code = 0xe047; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
40 case Keys.PageUp: code = 0xe049; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
41 case Keys.PageDown: code = 0xe051; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
42 case Keys.End: code = 0xe04f; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
43 case Keys.Insert: code = 0xe052; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
44 case Keys.ControlKey:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
45 case Keys.LControlKey: code = 0x1d; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
46 case Keys.RControlKey: code = 0xe01d; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
47 case Keys.Menu:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
48 case Keys.LMenu: code = 0x38; break; //Left Alt
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
49 case Keys.RMenu: code = 0xe038; break; //Right Alt
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
50 case Keys.LWin: code = 0xe05b; break; //Left windows key
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
51 case Keys.RWin: code = 0xe05c; break; //Right windows key
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
52 case Keys.Delete: code = 0xe053; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
53 case Keys.OemQuotes: code = 0x28; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
54 case Keys.OemQuestion: code = 0x35; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
55 case Keys.OemPeriod: code = 0x34; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
56 case Keys.OemMinus: code = 0x0c; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
57 case Keys.Oemplus: code = 0x0d; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
58 case Keys.Oemcomma: code = 0x33; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
59 case Keys.OemSemicolon: code = 0x27; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
60 case Keys.Oemtilde: code = 0x29; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
61 case Keys.OemCloseBrackets: code = 0x1b; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
62 case Keys.OemBackslash: code = 0x2b; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
63 case Keys.OemOpenBrackets: code = 0x1a; break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
64 default:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
65 Console.Error.WriteLine("VBoxKeyboardHelper.SendKeyCode({1}={2},{0}) unknown key", down, keycode, (int)keycode);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
66 return;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
67 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
68 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
69 if ((code & 0xff00) != 0) kbdPutCode((code >> 8) & 0xff);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
70 kbdPutCode((code & 0xff) | (down ? 0 : 0x80));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
71 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
72 private void kbdPutCode(int code) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
73 try {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
74 Keyboard.PutScancode(code);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
75 } catch (Exception ex) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
76 Console.Error.WriteLine("VBoxKeyboardHelper.PutScancode Exception: {0}", ex.ToString());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
77 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
78 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
79 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
80 }