From 33527aa2c9057be067cf1f3f2816f02a45ffddba Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 20 Aug 2007 13:45:04 +0000 Subject: [PATCH] Added KeyDown and KeyUp events to IKeyboard and Keyboard. Improved input logger test to hook on the aforementioned events. Removed Debug printing of keys in OpenTK.Input.Keyboard. Beautified X11Keyboard.cs Added experimental code to handle X11 keyboard events (not used yet). --- Source/Examples/Tests/S02_RawInput_Logger.cs | 19 +++++++++++++ Source/OpenTK/Input/IKeyboard.cs | 12 ++++++++ Source/OpenTK/Input/Keyboard.cs | 30 +++++++++++++++++--- Source/OpenTK/Platform/X11/X11Input.cs | 10 +++++++ Source/OpenTK/Platform/X11/X11Keyboard.cs | 29 +++++++++---------- 5 files changed, 81 insertions(+), 19 deletions(-) diff --git a/Source/Examples/Tests/S02_RawInput_Logger.cs b/Source/Examples/Tests/S02_RawInput_Logger.cs index ff307d84..21220940 100644 --- a/Source/Examples/Tests/S02_RawInput_Logger.cs +++ b/Source/Examples/Tests/S02_RawInput_Logger.cs @@ -60,6 +60,24 @@ namespace Examples.Tests public S02_RawInput_Logger() { this.CreateWindow(new OpenTK.Platform.DisplayMode(100, 100)); + + foreach (OpenTK.Input.Keyboard k in this.Keyboard) + { + k.KeyDown += new OpenTK.Input.KeyDownEvent(LogKeyDown); + k.KeyUp += new OpenTK.Input.KeyUpEvent(LogKeyUp); + } + } + + void LogKeyDown(object sender, OpenTK.Input.Key key) + { + Trace.WriteLine(String.Format("OpenTK key {0} pressed on Keyboard: ({1}).", + key, sender as OpenTK.Input.Keyboard)); + } + + void LogKeyUp(object sender, OpenTK.Input.Key key) + { + Trace.WriteLine(String.Format("OpenTK key {0} released on Keyboard: ({1}).", + key, sender as OpenTK.Input.Keyboard)); } public override void OnLoad(EventArgs e) @@ -75,6 +93,7 @@ namespace Examples.Tests GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT); Context.SwapBuffers(); + Thread.Sleep(1); } } } diff --git a/Source/OpenTK/Input/IKeyboard.cs b/Source/OpenTK/Input/IKeyboard.cs index b49ab281..9bfae655 100644 --- a/Source/OpenTK/Input/IKeyboard.cs +++ b/Source/OpenTK/Input/IKeyboard.cs @@ -13,5 +13,17 @@ namespace OpenTK.Input int NumberOfFunctionKeys { get; } int NumberOfLeds { get; } long DeviceID { get; } + + event KeyDownEvent KeyDown; + event KeyUpEvent KeyUp; } + + public delegate void KeyDownEvent(object sender, Key key); + public delegate void KeyUpEvent(object sender, Key key); + + //public class KeyEventArgs : System.EventArgs + //{ + // private Key key; + // public Key Key { get { return key; } } + //} } \ No newline at end of file diff --git a/Source/OpenTK/Input/Keyboard.cs b/Source/OpenTK/Input/Keyboard.cs index b9b070c9..102d5560 100644 --- a/Source/OpenTK/Input/Keyboard.cs +++ b/Source/OpenTK/Input/Keyboard.cs @@ -15,7 +15,7 @@ using System.Diagnostics; namespace OpenTK.Input { - public class Keyboard : IKeyboard + public sealed class Keyboard : IKeyboard { //private IKeyboard keyboard; private bool[] keys = new bool[(int)Key.MaxKeys]; @@ -38,8 +38,16 @@ namespace OpenTK.Input get { return keys[(int)k]; } internal set { - Debug.Print("OpenTK key {0} {1}.", k, value ? "pressed" : "released"); keys[(int)k] = value; + + if (value && KeyDown != null) + { + KeyDown(this, k); + } + else if (!value && KeyUp != null) + { + KeyUp(this, k); + } } } @@ -70,6 +78,16 @@ namespace OpenTK.Input internal set { devID = value; } } + /// + /// Occurs when a key is pressed. + /// + public event KeyDownEvent KeyDown; + + /// + /// Occurs when a key is released. + /// + public event KeyUpEvent KeyUp; + #endregion #region --- IInputDevice Members --- @@ -87,6 +105,8 @@ namespace OpenTK.Input #endregion + #region --- Public Methods --- + public override int GetHashCode() { //return base.GetHashCode(); @@ -96,9 +116,11 @@ namespace OpenTK.Input public override string ToString() { //return base.ToString(); - return String.Format("Keyboard: '{0}', {1} keys, {2} function keys, {3} leds. Device ID: {4}", - description, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds, DeviceID); + return String.Format("ID: {0} (keys: {1}, function keys: {2}, leds: {3}", + DeviceID, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds); } + + #endregion } #region public enum Key : int diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs index b88a8bdf..036446a8 100644 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ b/Source/OpenTK/Platform/X11/X11Input.cs @@ -133,6 +133,16 @@ namespace OpenTK.Platform.X11 { keyboardDriver.ProcessKeyboardEvent(e.KeyEvent); } + /* + if (API.Pending(window.Display) > 0) + { + Functions.XPeekEvent(window.Display, ref e); + if (e.type == XEventName.KeyRelease || e.type == XEventName.KeyPress) + { + keyboardDriver.ProcessKeyboardEvent(e.KeyEvent); + } + } + */ } #endregion diff --git a/Source/OpenTK/Platform/X11/X11Keyboard.cs b/Source/OpenTK/Platform/X11/X11Keyboard.cs index dc551bfa..3f062030 100644 --- a/Source/OpenTK/Platform/X11/X11Keyboard.cs +++ b/Source/OpenTK/Platform/X11/X11Keyboard.cs @@ -204,21 +204,20 @@ namespace OpenTK.Platform.X11 switch (keysym.ToInt64()) { default: - if (keymap.ContainsKey((XKey)keysym)) - { - keyboards[0][keymap[(XKey)keysym]] = pressed; - } - else if (keymap.ContainsKey((XKey)keysym2)) - { - keyboards[0][keymap[(XKey)keysym2]] = pressed; - } - else - { - Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2); - return false; - } - return true; - + if (keymap.ContainsKey((XKey)keysym)) + { + keyboards[0][keymap[(XKey)keysym]] = pressed; + } + else if (keymap.ContainsKey((XKey)keysym2)) + { + keyboards[0][keymap[(XKey)keysym2]] = pressed; + } + else + { + Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2); + return false; + } + return true; } }