/
WinRawKeyboard.cs
305 lines (260 loc) · 11.6 KB
/
WinRawKeyboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#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.
//
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
#if !(ANDROID || IPHONE || MINIMAL)
using Microsoft.Win32;
#endif
using OpenTK.Input;
namespace OpenTK.Platform.Windows
{
sealed class WinRawKeyboard : IKeyboardDriver2
{
static readonly WinKeyMap KeyMap = new WinKeyMap();
readonly List<KeyboardState> keyboards = new List<KeyboardState>();
readonly List<string> names = new List<string>();
readonly Dictionary<ContextHandle, int> rawids = new Dictionary<ContextHandle, int>();
readonly IntPtr window;
readonly object UpdateLock = new object();
#region Constructors
public WinRawKeyboard(IntPtr windowHandle)
{
Debug.WriteLine("Using WinRawKeyboard.");
Debug.Indent();
this.window = windowHandle;
RefreshDevices();
Debug.Unindent();
}
#endregion
#region Public Members
public void RefreshDevices()
{
lock (UpdateLock)
{
for (int i = 0; i < keyboards.Count; i++)
{
KeyboardState state = keyboards[i];
state.IsConnected = false;
keyboards[i] = state;
}
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);
// Discover keyboard devices:
foreach (RawInputDeviceList dev in ridl)
{
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;
}
string name = GetDeviceName(dev);
if (name.ToLower().Contains("root"))
{
// 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);
if (regkey == null)
continue;
string deviceDesc = (string)regkey.GetValue("DeviceDesc");
string deviceClass = (string)regkey.GetValue("Class");
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...
if(deviceClass == null || deviceClass.Equals(string.Empty)){
RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID);
deviceClass = classGUIDKey != null ? (string) classGUIDKey.GetValue("Class") : string.Empty;
}
if (String.IsNullOrEmpty(deviceDesc))
{
Debug.Print("[Warning] Failed to retrieve device description, skipping this device.");
continue;
}
else
{
deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1);
}
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);
}
}
}
}
}
public bool ProcessKeyboardEvent(RawInput rin)
{
bool processed = false;
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;
bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0;
bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0;
bool is_valid = true;
ContextHandle handle = new ContextHandle(rin.Header.Device);
KeyboardState keyboard;
if (!rawids.ContainsKey(handle))
{
RefreshDevices();
}
if (keyboards.Count == 0)
return false;
// 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];
Key key = KeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid);
if (is_valid)
{
keyboard.SetKeyState(key, (byte)scancode, pressed);
processed = true;
}
lock (UpdateLock)
{
keyboards[keyboard_handle] = keyboard;
return processed;
}
}
#endregion
#region Private Members
static RegistryKey GetRegistryKey(string name)
{
if (name.Length < 4)
return null;
// remove the \??\
name = name.Substring(4);
string[] split = name.Split('#');
if (split.Length < 3)
return null;
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;
}
static string GetDeviceName(RawInputDeviceList dev)
{
uint size = 0;
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)
{
RawInputDevice[] rid = new RawInputDevice[1];
// Keyboard is 1/6 (page/id). See http://www.microsoft.com/whdc/device/input/HID_HWID.mspx
rid[0] = new RawInputDevice();
rid[0].UsagePage = 1;
rid[0].Usage = 6;
rid[0].Flags = RawInputDeviceFlags.INPUTSINK;
rid[0].Target = window;
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);
}
}
#endregion
#region IKeyboardDriver2 Members
public KeyboardState GetState()
{
lock (UpdateLock)
{
KeyboardState master = new KeyboardState();
foreach (KeyboardState ks in keyboards)
{
master.MergeBits(ks);
}
return master;
}
}
public KeyboardState GetState(int index)
{
lock (UpdateLock)
{
if (keyboards.Count > index)
return keyboards[index];
else
return new KeyboardState();
}
}
public string GetDeviceName(int index)
{
lock (UpdateLock)
{
if (names.Count > index)
return names[index];
else
return String.Empty;
}
}
#endregion
}
}