From b71519eac586c4aefe7e79056804e315ecd58b0b Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 6 Jan 2008 02:28:51 +0000 Subject: [PATCH] Implemented mouse and keyboard driver. --- Source/OpenTK/Platform/Windows/WMInput.cs | 107 ++++++++++++++++++++-- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git a/Source/OpenTK/Platform/Windows/WMInput.cs b/Source/OpenTK/Platform/Windows/WMInput.cs index da8a8c7f..a3dfa504 100644 --- a/Source/OpenTK/Platform/Windows/WMInput.cs +++ b/Source/OpenTK/Platform/Windows/WMInput.cs @@ -1,4 +1,10 @@ -using System; +#region --- License --- +/* Copyright (c) 2007 Stefanos Apostolopoulos + * See license.txt for license information + */ +#endregion + +using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; @@ -18,6 +24,7 @@ namespace OpenTK.Platform.Windows MouseDevice mouse = new MouseDevice(); IList keyboards = new List(1); IList mice = new List(1); + internal static readonly KeyMap KeyMap = new KeyMap(); #region --- Constructor --- @@ -50,16 +57,83 @@ namespace OpenTK.Platform.Windows { switch ((WindowMessage)msg.Msg) { + // Mouse events: case WindowMessage.MOUSEMOVE: - break; + //case WindowMessage.NCMOUSEMOVE: + mouse.X = msg.LParam.ToInt32() & 0x0000FFFF; + mouse.Y = (int)(msg.LParam.ToInt32() & 0xFFFF0000) >> 16; + + return; case WindowMessage.MOUSEWHEEL: - break; + mouse.Wheel += (int)(msg.WParam.ToInt32() >> 16) / 120; + return; + case WindowMessage.LBUTTONDOWN: + mouse[MouseButton.Left] = true; + return; + + case WindowMessage.MBUTTONDOWN: + mouse[MouseButton.Middle] = true; + return; + + case WindowMessage.RBUTTONDOWN: + mouse[MouseButton.Right] = true; + return; + + case WindowMessage.XBUTTONDOWN: + mouse[((msg.WParam.ToInt32() & 0xFFFF0000) >> 16) != (int)MouseKeys.XButton1 ? MouseButton.Button1 : MouseButton.Button2] = true; + return; + + case WindowMessage.LBUTTONUP: + mouse[MouseButton.Left] = false; + return; + + case WindowMessage.MBUTTONUP: + mouse[MouseButton.Middle] = false; + return; + + case WindowMessage.RBUTTONUP: + mouse[MouseButton.Right] = false; + return; + + case WindowMessage.XBUTTONUP: + mouse[((msg.WParam.ToInt32() & 0xFFFF0000) >> 16) != (int)MouseKeys.XButton1 ? MouseButton.Button1 : MouseButton.Button2] = false; + return; + + // Keyboard events: case WindowMessage.KEYDOWN: - break; - case WindowMessage.KEYUP: + case WindowMessage.SYSKEYDOWN: + case WindowMessage.SYSKEYUP: + bool pressed = (WindowMessage)msg.Msg == WindowMessage.KEYDOWN || + (WindowMessage)msg.Msg == WindowMessage.SYSKEYDOWN; + switch ((VirtualKeys)msg.WParam) + { + case VirtualKeys.SHIFT: + keyboard[Input.Key.ShiftLeft] = keyboard[Input.Key.ShiftRight] = pressed; + return; + + case VirtualKeys.CONTROL: + keyboard[Input.Key.ControlLeft] = keyboard[Input.Key.ControlRight] = pressed; + return; + + case VirtualKeys.MENU: + keyboard[Input.Key.AltLeft] = keyboard[Input.Key.AltRight] = pressed; + return; + + default: + if (!WMInput.KeyMap.ContainsKey((VirtualKeys)msg.WParam)) + { + Debug.Print("Virtual key {0} ({1}) not mapped.", (VirtualKeys)msg.WParam, (int)msg.WParam); + break; + } + else + { + keyboard[WMInput.KeyMap[(VirtualKeys)msg.WParam]] = pressed; + return; + } + } break; } @@ -93,11 +167,30 @@ namespace OpenTK.Platform.Windows #endregion - #region IDisposable Members + #region --- IDisposable Members --- + + private bool disposed; public void Dispose() { - + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool manual) + { + if (!disposed) + { + if (manual) + this.ReleaseHandle(); + + disposed = true; + } + } + + ~WMInput() + { + Dispose(false); } #endregion