2010-10-22 14:57:06 +00:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006 - 2010 the Open Toolkit library.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2010-10-29 11:46:57 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2013-10-12 03:15:46 +00:00
|
|
|
|
#if !(ANDROID || IPHONE || MINIMAL)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using Microsoft.Win32;
|
2013-10-12 03:15:46 +00:00
|
|
|
|
#endif
|
2009-02-22 10:43:35 +00:00
|
|
|
|
using OpenTK.Input;
|
2014-01-17 12:57:15 +00:00
|
|
|
|
using OpenTK.Platform.Common;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
2010-10-29 11:46:57 +00:00
|
|
|
|
sealed class WinRawKeyboard : IKeyboardDriver2
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-28 08:42:38 +00:00
|
|
|
|
readonly List<KeyboardState> keyboards = new List<KeyboardState>();
|
2010-10-29 11:27:40 +00:00
|
|
|
|
readonly List<string> names = new List<string>();
|
2010-10-28 08:42:38 +00:00
|
|
|
|
readonly Dictionary<ContextHandle, int> rawids = new Dictionary<ContextHandle, int>();
|
2010-10-29 11:46:57 +00:00
|
|
|
|
readonly IntPtr window;
|
2010-10-28 13:00:21 +00:00
|
|
|
|
readonly object UpdateLock = new object();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
#region Constructors
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
public WinRawKeyboard(IntPtr windowHandle)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-11-08 21:43:29 +00:00
|
|
|
|
Debug.WriteLine("Using WinRawKeyboard.");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
this.window = windowHandle;
|
2010-10-29 11:46:57 +00:00
|
|
|
|
RefreshDevices();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
#region Public Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
public void RefreshDevices()
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:46:57 +00:00
|
|
|
|
lock (UpdateLock)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:46:57 +00:00
|
|
|
|
for (int i = 0; i < keyboards.Count; i++)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:46:57 +00:00
|
|
|
|
KeyboardState state = keyboards[i];
|
|
|
|
|
state.IsConnected = false;
|
|
|
|
|
keyboards[i] = state;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
int count = WinRawInput.DeviceCount;
|
|
|
|
|
RawInputDeviceList[] ridl = new RawInputDeviceList[count];
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
ridl[i] = new RawInputDeviceList();
|
|
|
|
|
Functions.GetRawInputDeviceList(ridl, ref count, API.RawInputDeviceListSize);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
// Discover keyboard devices:
|
|
|
|
|
foreach (RawInputDeviceList dev in ridl)
|
|
|
|
|
{
|
2010-11-09 15:19:58 +00:00
|
|
|
|
ContextHandle id = new ContextHandle(dev.Device);
|
|
|
|
|
if (rawids.ContainsKey(id))
|
|
|
|
|
{
|
|
|
|
|
// Device already registered, mark as connected
|
|
|
|
|
KeyboardState state = keyboards[rawids[id]];
|
|
|
|
|
state.IsConnected = true;
|
|
|
|
|
keyboards[rawids[id]] = state;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
string name = GetDeviceName(dev);
|
|
|
|
|
if (name.ToLower().Contains("root"))
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:46:57 +00:00
|
|
|
|
// This is a terminal services device, skip it.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (dev.Type == RawInputDeviceType.KEYBOARD || dev.Type == RawInputDeviceType.HID)
|
|
|
|
|
{
|
|
|
|
|
// This is a keyboard or USB keyboard device. In the latter case, discover if it really is a
|
|
|
|
|
// keyboard device by qeurying the registry.
|
|
|
|
|
RegistryKey regkey = GetRegistryKey(name);
|
2014-01-08 23:10:41 +00:00
|
|
|
|
if (regkey == null)
|
|
|
|
|
continue;
|
2013-01-14 19:09:55 +00:00
|
|
|
|
|
2014-01-08 23:10:41 +00:00
|
|
|
|
string deviceDesc = (string)regkey.GetValue("DeviceDesc");
|
2010-10-29 11:46:57 +00:00
|
|
|
|
string deviceClass = (string)regkey.GetValue("Class");
|
2013-01-14 19:09:55 +00:00
|
|
|
|
string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); // for windows 8 support via OpenTK issue 3198
|
|
|
|
|
|
|
|
|
|
// making a guess at backwards compatability. Not sure what older windows returns in these cases...
|
2013-01-26 13:27:26 +00:00
|
|
|
|
if(deviceClass == null || deviceClass.Equals(string.Empty)){
|
2013-01-14 19:09:55 +00:00
|
|
|
|
RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID);
|
|
|
|
|
deviceClass = classGUIDKey != null ? (string) classGUIDKey.GetValue("Class") : string.Empty;
|
|
|
|
|
}
|
2010-12-03 12:29:01 +00:00
|
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(deviceDesc))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Warning] Failed to retrieve device description, skipping this device.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1);
|
|
|
|
|
}
|
2010-10-29 11:46:57 +00:00
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard"))
|
|
|
|
|
{
|
|
|
|
|
// Register the keyboard:
|
|
|
|
|
RawInputDeviceInfo info = new RawInputDeviceInfo();
|
|
|
|
|
int devInfoSize = API.RawInputDeviceInfoSize;
|
|
|
|
|
Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICEINFO,
|
|
|
|
|
info, ref devInfoSize);
|
|
|
|
|
|
|
|
|
|
//KeyboardDevice kb = new KeyboardDevice();
|
|
|
|
|
//kb.Description = deviceDesc;
|
|
|
|
|
//kb.NumberOfLeds = info.Device.Keyboard.NumberOfIndicators;
|
|
|
|
|
//kb.NumberOfFunctionKeys = info.Device.Keyboard.NumberOfFunctionKeys;
|
|
|
|
|
//kb.NumberOfKeys = info.Device.Keyboard.NumberOfKeysTotal;
|
|
|
|
|
//kb.DeviceID = dev.Device;
|
|
|
|
|
|
|
|
|
|
RegisterKeyboardDevice(window, deviceDesc);
|
|
|
|
|
KeyboardState state = new KeyboardState();
|
|
|
|
|
state.IsConnected = true;
|
|
|
|
|
keyboards.Add(state);
|
|
|
|
|
names.Add(deviceDesc);
|
|
|
|
|
rawids.Add(new ContextHandle(dev.Device), keyboards.Count - 1);
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
public bool ProcessKeyboardEvent(IntPtr raw)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-28 08:42:38 +00:00
|
|
|
|
bool processed = false;
|
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
RawInput rin;
|
|
|
|
|
if (Functions.GetRawInputData(raw, out rin) > 0)
|
|
|
|
|
{
|
|
|
|
|
bool pressed =
|
|
|
|
|
rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN ||
|
|
|
|
|
rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN;
|
|
|
|
|
var scancode = rin.Data.Keyboard.MakeCode;
|
|
|
|
|
var vkey = rin.Data.Keyboard.VKey;
|
2013-10-03 18:53:34 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0;
|
|
|
|
|
bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0;
|
2013-10-03 18:53:34 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
bool is_valid = true;
|
2010-10-28 08:42:38 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
ContextHandle handle = new ContextHandle(rin.Header.Device);
|
|
|
|
|
KeyboardState keyboard;
|
|
|
|
|
if (!rawids.ContainsKey(handle))
|
|
|
|
|
{
|
|
|
|
|
RefreshDevices();
|
|
|
|
|
}
|
2010-11-09 15:19:58 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
if (keyboards.Count == 0)
|
|
|
|
|
return false;
|
2010-11-09 15:19:58 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
// Note:For some reason, my Microsoft Digital 3000 keyboard reports 0
|
|
|
|
|
// as rin.Header.Device for the "zoom-in/zoom-out" buttons.
|
|
|
|
|
// That's problematic, because no device has a "0" id.
|
|
|
|
|
// As a workaround, we'll add those buttons to the first device (if any).
|
|
|
|
|
int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0;
|
|
|
|
|
keyboard = keyboards[keyboard_handle];
|
2010-10-28 08:42:38 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
Key key = WinKeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid);
|
2013-10-03 18:53:34 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
if (is_valid)
|
|
|
|
|
{
|
|
|
|
|
keyboard.SetKeyState(key, pressed);
|
|
|
|
|
processed = true;
|
|
|
|
|
}
|
2010-10-28 08:42:38 +00:00
|
|
|
|
|
2014-07-31 13:44:11 +00:00
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
|
|
|
|
keyboards[keyboard_handle] = keyboard;
|
|
|
|
|
processed = true;
|
|
|
|
|
}
|
2010-10-28 13:00:21 +00:00
|
|
|
|
}
|
2014-07-31 13:44:11 +00:00
|
|
|
|
|
|
|
|
|
return processed;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
#region Private Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
static RegistryKey GetRegistryKey(string name)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-01-08 23:10:41 +00:00
|
|
|
|
if (name.Length < 4)
|
|
|
|
|
return null;
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
// remove the \??\
|
|
|
|
|
name = name.Substring(4);
|
|
|
|
|
|
|
|
|
|
string[] split = name.Split('#');
|
2014-01-08 23:10:41 +00:00
|
|
|
|
if (split.Length < 3)
|
|
|
|
|
return null;
|
2010-10-29 11:46:57 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return regkey;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
static string GetDeviceName(RawInputDeviceList dev)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-01-18 15:00:27 +00:00
|
|
|
|
int size = 0;
|
2010-10-29 11:46:57 +00:00
|
|
|
|
Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size);
|
|
|
|
|
IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size);
|
|
|
|
|
Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size);
|
|
|
|
|
string name = Marshal.PtrToStringAnsi(name_ptr);
|
|
|
|
|
Marshal.FreeHGlobal(name_ptr);
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void RegisterKeyboardDevice(IntPtr window, string name)
|
|
|
|
|
{
|
2014-01-17 12:57:15 +00:00
|
|
|
|
RawInputDevice[] rid = new RawInputDevice[]
|
|
|
|
|
{
|
|
|
|
|
new RawInputDevice(HIDUsageGD.Keyboard, RawInputDeviceFlags.INPUTSINK, window)
|
|
|
|
|
};
|
2010-10-29 11:46:57 +00:00
|
|
|
|
|
|
|
|
|
if (!Functions.RegisterRawInputDevices(rid, 1, API.RawInputDeviceSize))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("[Warning] Raw input registration failed with error: {0}. Device: {1}",
|
|
|
|
|
Marshal.GetLastWin32Error(), rid[0].ToString());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Registered keyboard {0}", name);
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2010-10-29 11:46:57 +00:00
|
|
|
|
#region IKeyboardDriver2 Members
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2010-10-20 14:33:23 +00:00
|
|
|
|
public KeyboardState GetState()
|
|
|
|
|
{
|
2010-10-28 13:00:21 +00:00
|
|
|
|
lock (UpdateLock)
|
2010-10-28 11:10:57 +00:00
|
|
|
|
{
|
2010-10-28 13:00:21 +00:00
|
|
|
|
KeyboardState master = new KeyboardState();
|
|
|
|
|
foreach (KeyboardState ks in keyboards)
|
|
|
|
|
{
|
|
|
|
|
master.MergeBits(ks);
|
|
|
|
|
}
|
|
|
|
|
return master;
|
2010-10-28 11:10:57 +00:00
|
|
|
|
}
|
2010-10-20 14:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KeyboardState GetState(int index)
|
|
|
|
|
{
|
2010-10-28 13:00:21 +00:00
|
|
|
|
lock (UpdateLock)
|
|
|
|
|
{
|
|
|
|
|
if (keyboards.Count > index)
|
|
|
|
|
return keyboards[index];
|
|
|
|
|
else
|
|
|
|
|
return new KeyboardState();
|
|
|
|
|
}
|
2010-10-20 14:33:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 11:27:40 +00:00
|
|
|
|
public string GetDeviceName(int index)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:27:40 +00:00
|
|
|
|
lock (UpdateLock)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2010-10-29 11:27:40 +00:00
|
|
|
|
if (names.Count > index)
|
|
|
|
|
return names[index];
|
|
|
|
|
else
|
|
|
|
|
return String.Empty;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|