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;
|
|
|
|
|
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
2007-07-27 01:37:12 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-27 01:20:55 +00:00
|
|
|
|
namespace OpenTK.Platform.Windows
|
|
|
|
|
{
|
|
|
|
|
internal class WinRawKeyboard : OpenTK.Input.IKeyboard
|
|
|
|
|
{
|
|
|
|
|
private bool[] keys = new bool[(int)Keys.MaxKeys];
|
|
|
|
|
|
|
|
|
|
internal WinRawKeyboard()
|
|
|
|
|
: this(IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal WinRawKeyboard(IntPtr windowHandle)
|
|
|
|
|
{
|
|
|
|
|
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-07-31 21:50:29 +00:00
|
|
|
|
|
|
|
|
|
// Set the VirtualKey -> OpenTK.Key map
|
|
|
|
|
keyMap.Add(API.VirtualKeys.ESCAPE, Keys.Escape);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F1, Keys.F1);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F2, Keys.F2);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F3, Keys.F3);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F4, Keys.F4);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F5, Keys.F5);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F6, Keys.F6);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F7, Keys.F7);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F8, Keys.F8);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F9, Keys.F9);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F10, Keys.F10);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F11, Keys.F11);
|
|
|
|
|
keyMap.Add(API.VirtualKeys.F12, Keys.F12);
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool ProcessEvent(API.RawInput rin)
|
|
|
|
|
{
|
2007-07-31 21:50:29 +00:00
|
|
|
|
switch (rin.Header.Type)
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-07-31 21:50:29 +00:00
|
|
|
|
case API.RawInputDeviceType.KEYBOARD:
|
|
|
|
|
this[keyMap[rin.Data.Keyboard.VKey]] =
|
|
|
|
|
rin.Data.Keyboard.Message == API.Constants.WM_KEYDOWN ||
|
|
|
|
|
rin.Data.Keyboard.Message == API.Constants.WM_SYSKEYDOWN;
|
|
|
|
|
break;
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-31 21:50:29 +00:00
|
|
|
|
#region KeyMap
|
|
|
|
|
|
|
|
|
|
internal static Dictionary<API.VirtualKeys, Keys> keyMap =
|
|
|
|
|
new Dictionary<API.VirtualKeys, Keys>((int)API.VirtualKeys.Last);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
internal static List<KeyValuePair<API.VirtualKeys, Keys>> keyMap =
|
|
|
|
|
new List<KeyValuePair<API.VirtualKeys, Keys>>(
|
|
|
|
|
new KeyValuePair<API.VirtualKeys, Keys>[]
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<API.VirtualKeys, Keys>(API.VirtualKeys.ESCAPE, Keys.Escape),
|
|
|
|
|
new KeyValuePair<API.VirtualKeys, Keys>(API.VirtualKeys.F1, Keys.F1)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-27 01:20:55 +00:00
|
|
|
|
#region --- IKeyboard members ---
|
|
|
|
|
|
|
|
|
|
public bool this[Keys k]
|
|
|
|
|
{
|
|
|
|
|
get { return keys[(int)k]; }
|
2007-07-31 21:50:29 +00:00
|
|
|
|
internal set
|
2007-07-27 01:20:55 +00:00
|
|
|
|
{
|
2007-07-31 21:50:29 +00:00
|
|
|
|
keys[(int)k] = value;
|
|
|
|
|
//throw new NotImplementedException();
|
2007-07-27 01:20:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|