From d7e037385253e9f811fdd29f8fcc81e6f6aeae1e Mon Sep 17 00:00:00 2001 From: thefiddler Date: Fri, 9 May 2014 16:45:45 +0200 Subject: [PATCH] [Platform] Fixed keys getting stuck on focus loss NativeWindowBase will now clear all keyboard keys when losing focus. This prevents keys from getting stuck when refocusing the window. [Win] Also fixed WindowState.Maximized when WindowBorder is Hidden and the window is minimized. --- .../Platform/MacOS/CocoaNativeWindow.cs | 2 ++ Source/OpenTK/Platform/NativeWindowBase.cs | 31 ++++++++++--------- .../OpenTK/Platform/SDL2/Sdl2NativeWindow.cs | 1 + Source/OpenTK/Platform/Windows/WinGLNative.cs | 12 +++---- Source/OpenTK/Platform/X11/X11GLNative.cs | 1 + 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs index a9fb48b2..701b0bb7 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs @@ -398,6 +398,8 @@ namespace OpenTK.Platform.MacOS public override void ProcessEvents() { + base.ProcessEvents(); + while (true) { var e = Cocoa.SendIntPtr(NSApplication.Handle, selNextEventMatchingMask, uint.MaxValue, IntPtr.Zero, NSDefaultRunLoopMode, true); diff --git a/Source/OpenTK/Platform/NativeWindowBase.cs b/Source/OpenTK/Platform/NativeWindowBase.cs index 3b1fcd39..97365f9b 100644 --- a/Source/OpenTK/Platform/NativeWindowBase.cs +++ b/Source/OpenTK/Platform/NativeWindowBase.cs @@ -109,20 +109,6 @@ namespace OpenTK.Platform protected void OnFocusedChanged(EventArgs e) { FocusedChanged(this, e); - - if (!Focused) - { - // Clear keyboard state, otherwise KeyUp - // events may be missed resulting in stuck - // keys. - for (Key key = 0; key < Key.LastKey; key++) - { - if (KeyboardState[key]) - { - OnKeyUp(key); - } - } - } } protected void OnWindowBorderChanged(EventArgs e) @@ -326,7 +312,22 @@ namespace OpenTK.Platform public abstract void Close(); - public abstract void ProcessEvents(); + public virtual void ProcessEvents() + { + if (!Focused) + { + // Clear keyboard state, otherwise KeyUp + // events may be missed resulting in stuck + // keys. + for (Key key = 0; key < Key.LastKey; key++) + { + if (KeyboardState[key]) + { + OnKeyUp(key); + } + } + } + } public abstract Point PointToClient(Point point); diff --git a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index 3f52e425..1e250a52 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -550,6 +550,7 @@ namespace OpenTK.Platform.SDL2 public override void ProcessEvents() { + base.ProcessEvents(); lock (sync) { if (Exists) diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 74ca8c06..fdcc14b4 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -581,7 +581,7 @@ namespace OpenTK.Platform.Windows bool extended = (lParam.ToInt64() & ExtendedBit) != 0; short scancode = (short)((lParam.ToInt64() >> 16) & 0xff); - ushort repeat_count = unchecked((ushort)((ulong)lParam.ToInt64() & 0xffffu)); + //ushort repeat_count = unchecked((ushort)((ulong)lParam.ToInt64() & 0xffffu)); VirtualKeys vkey = (VirtualKeys)wParam; bool is_valid; Key key = WinKeyMap.TranslateKey(scancode, vkey, extended, false, out is_valid); @@ -590,7 +590,8 @@ namespace OpenTK.Platform.Windows { if (pressed) { - OnKeyDown(key, repeat_count > 0); + //OnKeyDown(key, repeat_count > 0); + OnKeyDown(key, KeyboardState[key]); } else { @@ -912,7 +913,6 @@ namespace OpenTK.Platform.Windows { suppress_resize++; WindowBorder = WindowBorder.Hidden; - ProcessEvents(); suppress_resize--; } @@ -923,7 +923,6 @@ namespace OpenTK.Platform.Windows deferred_window_border.HasValue ? deferred_window_border.Value : previous_window_border.HasValue ? previous_window_border.Value : WindowBorder; - ProcessEvents(); suppress_resize--; deferred_window_border = previous_window_border = null; } @@ -932,7 +931,6 @@ namespace OpenTK.Platform.Windows { suppress_resize++; WindowState = WindowState.Normal; - ProcessEvents(); suppress_resize--; } @@ -1247,12 +1245,12 @@ namespace OpenTK.Platform.Windows ShowWindowCommand command = 0; bool exiting_fullscreen = false; - borderless_maximized_window_state = false; switch (value) { case WindowState.Normal: command = ShowWindowCommand.RESTORE; + borderless_maximized_window_state = false; // If we are leaving fullscreen mode we need to restore the border. if (WindowState == WindowState.Fullscreen) @@ -1280,6 +1278,7 @@ namespace OpenTK.Platform.Windows } else { + borderless_maximized_window_state = false; command = ShowWindowCommand.MAXIMIZE; } break; @@ -1457,6 +1456,7 @@ namespace OpenTK.Platform.Windows MSG msg; public override void ProcessEvents() { + base.ProcessEvents(); while (Functions.PeekMessage(ref msg, IntPtr.Zero, 0, 0, PeekMessageFlags.Remove)) { Functions.TranslateMessage(ref msg); diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index e5201638..13b429d7 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -795,6 +795,7 @@ namespace OpenTK.Platform.X11 public override void ProcessEvents() { + base.ProcessEvents(); // Process all pending events while (Exists && window != null) {