Use async input to poll the keyboard/mouse.

This commit is contained in:
the_fiddler 2010-11-08 22:19:44 +00:00
parent 2e0a6c8a96
commit e8373def1b

View file

@ -38,18 +38,14 @@ using OpenTK.Input;
namespace OpenTK.Platform.Windows namespace OpenTK.Platform.Windows
{ {
// Input driver for legacy (pre XP) Windows platforms. // Input driver for legacy (pre XP) Windows platforms.
// Supports a single mouse and keyboard through WM_MOUSE* and WM_KEY* events. // Supports a single mouse and keyboard through async input.
// Supports multiple joysticks through WinMM. // Supports multiple joysticks through WinMM.
sealed class WMInput : sealed class WMInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2, IGamePadDriver
#if !ASYNC_INPUT
WinInputBase,
#else
IInputDriver2,
#endif
IMouseDriver2, IKeyboardDriver2, IGamePadDriver
{ {
#region Fields #region Fields
readonly object MouseLock = new object();
readonly object KeyboardLock = new object();
readonly WinMMJoystick gamepad_driver = new WinMMJoystick(); readonly WinMMJoystick gamepad_driver = new WinMMJoystick();
KeyboardState keyboard = new KeyboardState(); KeyboardState keyboard = new KeyboardState();
MouseState mouse = new MouseState(); MouseState mouse = new MouseState();
@ -69,7 +65,7 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
#region Private Members #region Private Members
#if ASYNC_INPUT
void UpdateKeyboard() void UpdateKeyboard()
{ {
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
@ -87,6 +83,8 @@ namespace OpenTK.Platform.Windows
{ {
POINT p = new POINT(); POINT p = new POINT();
Functions.GetCursorPos(ref p); Functions.GetCursorPos(ref p);
mouse.X = p.X;
mouse.Y = p.Y;
// Note: we cannot poll the mouse wheel // Note: we cannot poll the mouse wheel
mouse[MouseButton.Left] = (Functions.GetAsyncKeyState(VirtualKeys.LBUTTON) >> 8) != 0; mouse[MouseButton.Left] = (Functions.GetAsyncKeyState(VirtualKeys.LBUTTON) >> 8) != 0;
mouse[MouseButton.Middle] = (Functions.GetAsyncKeyState(VirtualKeys.RBUTTON) >> 8) != 0; mouse[MouseButton.Middle] = (Functions.GetAsyncKeyState(VirtualKeys.RBUTTON) >> 8) != 0;
@ -94,50 +92,22 @@ namespace OpenTK.Platform.Windows
mouse[MouseButton.Button1] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON1) >> 8) != 0; mouse[MouseButton.Button1] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON1) >> 8) != 0;
mouse[MouseButton.Button2] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON2) >> 8) != 0; mouse[MouseButton.Button2] = (Functions.GetAsyncKeyState(VirtualKeys.XBUTTON2) >> 8) != 0;
} }
#endif
#endregion
#region Protected Members
protected override IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
return base.WindowProcedure(handle, message, wParam, lParam);
}
protected override void CreateDrivers()
{
//keyboard.IsConnected = true;
//Native.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
//{
// keyboard.EnableBit((int)e.Key);
//};
//Native.KeyUp += delegate(object sender, KeyboardKeyEventArgs e)
//{
// keyboard.DisableBit((int)e.Key);
//};
//mouse.IsConnected = false;
// Todo: implement and hook INativeWindow.Mouse* events.
//Native.MouseMove += delegate(object sender, MouseMoveEventArgs e)
//{
//};
}
#endregion #endregion
#region IInputDriver2 Members #region IInputDriver2 Members
public override IKeyboardDriver2 KeyboardDriver public IKeyboardDriver2 KeyboardDriver
{ {
get { return this; } get { return this; }
} }
public override IMouseDriver2 MouseDriver public IMouseDriver2 MouseDriver
{ {
get { return this; } get { return this; }
} }
public override IGamePadDriver GamePadDriver public IGamePadDriver GamePadDriver
{ {
get { return this; } get { return this; }
} }
@ -148,15 +118,23 @@ namespace OpenTK.Platform.Windows
public MouseState GetState() public MouseState GetState()
{ {
return mouse; lock (MouseLock)
{
UpdateMouse();
return mouse;
}
} }
public MouseState GetState(int index) public MouseState GetState(int index)
{ {
if (index == 0) lock (MouseLock)
return mouse; {
else UpdateMouse();
return new MouseState(); if (index == 0)
return mouse;
else
return new MouseState();
}
} }
#endregion #endregion
@ -165,15 +143,23 @@ namespace OpenTK.Platform.Windows
KeyboardState IKeyboardDriver2.GetState() KeyboardState IKeyboardDriver2.GetState()
{ {
return keyboard; lock (KeyboardLock)
{
UpdateKeyboard();
return keyboard;
}
} }
KeyboardState IKeyboardDriver2.GetState(int index) KeyboardState IKeyboardDriver2.GetState(int index)
{ {
if (index == 0) lock (KeyboardLock)
return keyboard; {
else UpdateKeyboard();
return new KeyboardState(); if (index == 0)
return keyboard;
else
return new KeyboardState();
}
} }
string IKeyboardDriver2.GetDeviceName(int index) string IKeyboardDriver2.GetDeviceName(int index)