From c96a632bab72d5c05fd9dc341180aba529ef0398 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 8 Nov 2010 21:48:00 +0000 Subject: [PATCH] Added KeyDown and KeyUp events. --- Source/OpenTK/INativeWindow.cs | 13 +++++++-- Source/OpenTK/NativeWindow.cs | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Source/OpenTK/INativeWindow.cs b/Source/OpenTK/INativeWindow.cs index 106ae60c..137a4a8d 100644 --- a/Source/OpenTK/INativeWindow.cs +++ b/Source/OpenTK/INativeWindow.cs @@ -229,10 +229,20 @@ namespace OpenTK /// event EventHandler WindowStateChanged; + /// + /// Occurs whenever a keybord key is pressed. + /// + event EventHandler KeyDown; + /// /// Occurs whenever a character is typed. /// event EventHandler KeyPress; + + /// + /// Occurs whenever a keyboard key is released. + /// + event EventHandler KeyUp; /// /// Occurs whenever the mouse cursor leaves the window . @@ -251,9 +261,6 @@ namespace OpenTK //event EventHandler MouseClick; //event EventHandler MouseDoubleClick; - //event EventHandler KeyDown; - //event EventHandler KeyUp; - //event EventHandler DragDrop; //event EventHandler DragEnter; //event EventHandler DragOver; diff --git a/Source/OpenTK/NativeWindow.cs b/Source/OpenTK/NativeWindow.cs index dd7467f9..5fc2f418 100644 --- a/Source/OpenTK/NativeWindow.cs +++ b/Source/OpenTK/NativeWindow.cs @@ -50,6 +50,7 @@ namespace OpenTK private bool disposed, events; private bool cursor_visible = true; + private bool previous_cursor_visible = true; #endregion @@ -588,11 +589,21 @@ namespace OpenTK /// public event EventHandler IconChanged = delegate { }; + /// + /// Occurs whenever a keybord key is pressed. + /// + public event EventHandler KeyDown = delegate { }; + /// /// Occurs whenever a character is typed. /// public event EventHandler KeyPress = delegate { }; + /// + /// Occurs whenever a keyboard key is released. + /// + public event EventHandler KeyUp = delegate { }; + /// /// Occurs whenever the window is moved. /// @@ -746,6 +757,20 @@ namespace OpenTK /// Not used. protected virtual void OnFocusedChanged(EventArgs e) { + if (!Focused) + { + // Release cursor when losing focus, to ensure + // IDEs continue working as expected. + previous_cursor_visible = CursorVisible; + CursorVisible = true; + } + else if (!previous_cursor_visible) + { + // Make cursor invisible when focus is regained + // if cursor was invisible on previous focus loss. + previous_cursor_visible = true; + CursorVisible = false; + } FocusedChanged(this, e); } @@ -764,6 +789,18 @@ namespace OpenTK #endregion + #region OnKeyDown + + /// + /// Occurs whenever a keybord key is pressed. + /// + protected virtual void OnKeyDown(KeyboardKeyEventArgs e) + { + KeyDown(this, e); + } + + #endregion + #region OnKeyPress /// @@ -777,6 +814,19 @@ namespace OpenTK #endregion + #region OnKeyUp + + /// + /// Called when a keybord key is released. + /// + /// The for this event. + protected virtual void OnKeyUp(KeyboardKeyEventArgs e) + { + KeyUp(this, e); + } + + #endregion + #region OnMove ///