2007-07-27 01:37:12 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Using directives ---
|
|
|
|
|
|
|
|
|
|
using System;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
using System.Diagnostics;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using OpenTK.Input;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-07-27 01:37:12 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-27 01:20:55 +00:00
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
internal class WinRawKeyboard : IKeyboardDriver
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
private List<Keyboard> keyboards = new List<Keyboard>();
|
|
|
|
|
private IntPtr windowHandle;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-05 16:04:39 +00:00
|
|
|
|
#region internal static Dictionary<API.VirtualKeys, Input.Key> KeyMap
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-05 16:04:39 +00:00
|
|
|
|
internal static Dictionary<API.VirtualKeys, Input.Key> KeyMap =
|
|
|
|
|
new Dictionary<API.VirtualKeys, Input.Key>((int)API.VirtualKeys.Last);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
|
|
|
|
private static bool keyMapExists;
|
|
|
|
|
/// <summary>
|
2007-08-05 16:04:39 +00:00
|
|
|
|
/// Initializes the map between VirtualKeys and OpenTK.Key
|
2007-08-03 00:14:31 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
private static void InitKeyMap()
|
|
|
|
|
{
|
|
|
|
|
if (!keyMapExists)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add(API.VirtualKeys.ESCAPE, Input.Key.Escape);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
|
|
|
|
// Function keys
|
|
|
|
|
for (int i = 0; i < 24; i++)
|
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add((API.VirtualKeys)((int)API.VirtualKeys.F1 + i), Input.Key.F1 + i);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Number keys (0-9)
|
|
|
|
|
for (int i = 0; i <= 9; i++)
|
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add((API.VirtualKeys)(0x30 + i), Input.Key.Number0 + i);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Letters (A-Z)
|
|
|
|
|
for (int i = 0; i < 26; i++)
|
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add((API.VirtualKeys)(0x41 + i), Input.Key.A + i);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add(API.VirtualKeys.TAB, Input.Key.Tab);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.CAPITAL, Input.Key.CapsLock);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.LCONTROL, Input.Key.ControlLeft);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.LSHIFT, Input.Key.ShiftLeft);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.LWIN, Input.Key.WinLeft);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.LMENU, Input.Key.AltLeft);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.SPACE, Input.Key.Space);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RMENU, Input.Key.AltRight);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RWIN, Input.Key.WinRight);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.APPS, Input.Key.Menu);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RCONTROL, Input.Key.ControlRight);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RSHIFT, Input.Key.ShiftRight);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RETURN, Input.Key.Enter);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.BACK, Input.Key.Backspace);
|
|
|
|
|
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_1, Input.Key.Semicolon); // Varies by keyboard, ;: on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_2, Input.Key.Slash); // Varies by keyboard, /? on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_3, Input.Key.Tilde); // Varies by keyboard, `~ on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_4, Input.Key.LeftBracket); // Varies by keyboard, [{ on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_5, Input.Key.BackSlash); // Varies by keyboard, \| on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_6, Input.Key.RightBracket); // Varies by keyboard, ]} on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_7, Input.Key.Quote); // Varies by keyboard, '" on Win2K/US
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_PLUS, Input.Key.Plus); // Invariant: +
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_COMMA, Input.Key.Comma); // Invariant: ,
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_MINUS, Input.Key.Minus); // Invariant: -
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.OEM_PERIOD, Input.Key.Period); // Invariant: .
|
|
|
|
|
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.HOME, Input.Key.Home);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.END, Input.Key.End);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.DELETE, Input.Key.Delete);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.PRIOR, Input.Key.PageUp);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.NEXT, Input.Key.PageDown);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.PRINT, Input.Key.PrintScreen);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.PAUSE, Input.Key.Pause);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.NUMLOCK, Input.Key.NumLock);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add(API.VirtualKeys.SLEEP, Input.Key.Sleep);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
|
|
|
|
// Keypad
|
|
|
|
|
for (int i = 0; i <= 9; i++)
|
|
|
|
|
{
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add((API.VirtualKeys)((int)API.VirtualKeys.NUMPAD0 + i), Input.Key.Keypad0 + i);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
|
|
|
|
}
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add(API.VirtualKeys.DECIMAL, Input.Key.KeypadDecimal);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.ADD, Input.Key.KeypadAdd);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.SUBTRACT, Input.Key.KeypadSubtract);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.DIVIDE, Input.Key.KeypadDivide);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.MULTIPLY, Input.Key.KeypadMultiply);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
|
|
|
|
|
// Navigation
|
2007-08-05 16:04:39 +00:00
|
|
|
|
KeyMap.Add(API.VirtualKeys.UP, Input.Key.Up);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.DOWN, Input.Key.Down);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.LEFT, Input.Key.Left);
|
|
|
|
|
KeyMap.Add(API.VirtualKeys.RIGHT, Input.Key.Right);
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Exception while creating keymap: '{0}'.", e.ToString());
|
|
|
|
|
System.Windows.Forms.MessageBox.Show(
|
|
|
|
|
String.Format("Exception while creating keymap: '{0}'.", e.ToString()));
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
keyMapExists = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
|
|
|
|
internal WinRawKeyboard()
|
|
|
|
|
: this(IntPtr.Zero)
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
internal WinRawKeyboard(IntPtr windowHandle)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Initializing keyboard driver.");
|
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
this.windowHandle = windowHandle;
|
|
|
|
|
InitKeyMap();
|
|
|
|
|
|
|
|
|
|
UpdateKeyboardList();
|
|
|
|
|
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region internal static void UpdateKeyboardList()
|
|
|
|
|
|
|
|
|
|
internal void UpdateKeyboardList()
|
2007-08-03 00:14:31 +00:00
|
|
|
|
{
|
2007-08-05 00:09:42 +00:00
|
|
|
|
int count = WinRawInput.DeviceCount;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
API.RawInputDeviceList[] ridl = new API.RawInputDeviceList[count];
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
ridl[i] = new API.RawInputDeviceList();
|
|
|
|
|
API.GetRawInputDeviceList(ridl, ref count, API.RawInputDeviceListSize);
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
// Discover keyboard devices:
|
2007-08-03 00:14:31 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
uint size = 0;
|
|
|
|
|
API.GetRawInputDeviceInfo(ridl[i].Device, API.RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size);
|
|
|
|
|
IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size);
|
|
|
|
|
API.GetRawInputDeviceInfo(ridl[i].Device, API.RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size);
|
|
|
|
|
string name = Marshal.PtrToStringAnsi(name_ptr);
|
|
|
|
|
Marshal.FreeHGlobal(name_ptr);
|
|
|
|
|
if (name.ToLower().Contains("root"))
|
|
|
|
|
{
|
|
|
|
|
// This is a terminal services devices, skip it.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (
|
|
|
|
|
ridl[i].Type == API.RawInputDeviceType.KEYBOARD ||
|
|
|
|
|
ridl[i].Type == API.RawInputDeviceType.HID)
|
|
|
|
|
{
|
|
|
|
|
//It's a keyboard – or a USB device that could be a keyboard
|
|
|
|
|
|
|
|
|
|
// remove the \??\
|
|
|
|
|
name = name.Substring(4);
|
|
|
|
|
|
|
|
|
|
string[] split = name.Split('#');
|
|
|
|
|
|
|
|
|
|
string id_01 = split[0]; // ACPI (Class code)
|
|
|
|
|
string id_02 = split[1]; // PNP0303 (SubClass code)
|
|
|
|
|
string id_03 = split[2]; // 3&13c0b0c5&0 (Protocol code)
|
|
|
|
|
// The final part is the class GUID and is not needed here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string findme = string.Format(
|
|
|
|
|
@"System\CurrentControlSet\Enum\{0}\{1}\{2}",
|
|
|
|
|
id_01, id_02, id_03);
|
|
|
|
|
|
|
|
|
|
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(findme);
|
|
|
|
|
|
|
|
|
|
string deviceDesc =
|
|
|
|
|
(string)regkey.GetValue("DeviceDesc");
|
|
|
|
|
string deviceClass =
|
|
|
|
|
(string)regkey.GetValue("Class");
|
|
|
|
|
if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard"))
|
|
|
|
|
{
|
|
|
|
|
Keyboard kb = new Keyboard();
|
|
|
|
|
kb.Description = deviceDesc;
|
|
|
|
|
|
|
|
|
|
// Register the keyboard:
|
|
|
|
|
API.RawInputDeviceInfo info = new API.RawInputDeviceInfo();
|
2007-08-05 00:09:42 +00:00
|
|
|
|
int devInfoSize = API.RawInputDeviceInfoSize;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
API.GetRawInputDeviceInfo(ridl[i].Device, API.RawInputDeviceInfoEnum.DEVICEINFO,
|
|
|
|
|
info, ref devInfoSize);
|
|
|
|
|
|
|
|
|
|
kb.NumberOfLeds = info.Device.Keyboard.NumberOfIndicators;
|
|
|
|
|
kb.NumberOfFunctionKeys = info.Device.Keyboard.NumberOfFunctionKeys;
|
|
|
|
|
kb.NumberOfKeys = info.Device.Keyboard.NumberOfKeysTotal;
|
|
|
|
|
kb.DeviceID = (info.Device.Keyboard.Type << 32) + info.Device.Keyboard.SubType;
|
|
|
|
|
|
|
|
|
|
if (!keyboards.Contains(kb))
|
|
|
|
|
{
|
|
|
|
|
this.RegisterKeyboardDevice(kb);
|
|
|
|
|
keyboards.Add(kb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#endregion
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region internal void RegisterKeyboardDevice(Keyboard kb)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
internal void RegisterKeyboardDevice(Keyboard kb)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
|
|
|
|
API.RawInputDevice[] rid = new API.RawInputDevice[1];
|
|
|
|
|
// Keyboard is 1/6 (page/id). See http://www.microsoft.com/whdc/device/input/HID_HWID.mspx
|
|
|
|
|
rid[0] = new API.RawInputDevice();
|
|
|
|
|
rid[0].UsagePage = 1;
|
|
|
|
|
rid[0].Usage = 6;
|
2007-07-27 01:37:12 +00:00
|
|
|
|
rid[0].Flags = API.RawInputDeviceFlags.INPUTSINK;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
rid[0].Target = windowHandle;
|
|
|
|
|
|
2007-07-27 01:37:12 +00:00
|
|
|
|
if (!API.RegisterRawInputDevices(rid, 1, API.RawInputDeviceSize))
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-07-27 01:37:12 +00:00
|
|
|
|
throw new ApplicationException(
|
|
|
|
|
String.Format(
|
|
|
|
|
"Raw input registration failed with error: {0}. Device: {1}",
|
|
|
|
|
Marshal.GetLastWin32Error(),
|
|
|
|
|
rid[0].ToString())
|
|
|
|
|
);
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Registered keyboard {0}", kb.ToString());
|
|
|
|
|
}
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-07-31 21:50:29 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
#region internal bool ProcessKeyboardEvent(API.RawInput rin)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-08-04 12:09:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes raw input events.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rin"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal bool ProcessKeyboardEvent(API.RawInput rin)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
//Keyboard key = keyboards[0];
|
|
|
|
|
//rin.Header.Device;
|
|
|
|
|
switch (rin.Header.Type)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-08-04 12:09:58 +00:00
|
|
|
|
case API.RawInputDeviceType.KEYBOARD:
|
|
|
|
|
bool pressed =
|
|
|
|
|
rin.Data.Keyboard.Message == API.Constants.WM_KEYDOWN ||
|
|
|
|
|
rin.Data.Keyboard.Message == API.Constants.WM_SYSKEYDOWN;
|
|
|
|
|
|
|
|
|
|
// Generic control, shift, alt keys may be sent instead of left/right.
|
|
|
|
|
// It seems you have to explicitly register left/right events.
|
|
|
|
|
switch (rin.Data.Keyboard.VKey)
|
|
|
|
|
{
|
|
|
|
|
case API.VirtualKeys.SHIFT:
|
2007-08-05 16:04:39 +00:00
|
|
|
|
keyboards[0][Input.Key.ShiftLeft] = keyboards[0][Input.Key.ShiftRight] = pressed;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case API.VirtualKeys.CONTROL:
|
2007-08-05 16:04:39 +00:00
|
|
|
|
keyboards[0][Input.Key.ControlLeft] = keyboards[0][Input.Key.ControlRight] = pressed;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case API.VirtualKeys.MENU:
|
2007-08-05 16:04:39 +00:00
|
|
|
|
keyboards[0][Input.Key.AltLeft] = keyboards[0][Input.Key.AltRight] = pressed;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (!WinRawKeyboard.KeyMap.ContainsKey(rin.Data.Keyboard.VKey))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Virtual key {0} not mapped.", rin.Data.Keyboard.VKey);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
keyboards[0][WinRawKeyboard.KeyMap[rin.Data.Keyboard.VKey]] = pressed;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Windows raw keyboard driver received invalid data.");
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
2007-08-04 12:09:58 +00:00
|
|
|
|
return false;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#region --- IInputDevice Members ---
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
|
|
|
|
get { throw new Exception("The method or operation is not implemented."); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Input.InputDeviceType DeviceType
|
|
|
|
|
{
|
|
|
|
|
get { return Input.InputDeviceType.Keyboard; }
|
|
|
|
|
}
|
2007-07-27 01:20:55 +00:00
|
|
|
|
|
2007-08-03 00:14:31 +00:00
|
|
|
|
#endregion
|
2007-08-04 12:09:58 +00:00
|
|
|
|
|
|
|
|
|
#region --- IKeyboardDriver Members ---
|
|
|
|
|
|
2007-08-04 13:28:16 +00:00
|
|
|
|
public IList<Keyboard> Keyboard
|
2007-08-04 12:09:58 +00:00
|
|
|
|
{
|
|
|
|
|
get { return keyboards; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-08-03 00:14:31 +00:00
|
|
|
|
}
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|