Backends: OSX: Fix keys remaining stuck in pressed state when CMD-tabbing to a different application. (#3832)

This commit is contained in:
rokups 2021-04-19 13:47:31 +03:00 committed by ocornut
parent 25fbff2156
commit 6d53884487
2 changed files with 37 additions and 7 deletions

View File

@ -18,6 +18,7 @@
// CHANGELOG // CHANGELOG
// (minor and older changes stripped away, please see git history for details) // (minor and older changes stripped away, please see git history for details)
// 2021-04-19: Inputs: Added a fix for keys remaining stuck in pressed state when CMD-tabbing into different application.
// 2021-01-27: Inputs: Added a fix for mouse position not being reported when mouse buttons other than left one are down. // 2021-01-27: Inputs: Added a fix for mouse position not being reported when mouse buttons other than left one are down.
// 2020-10-28: Inputs: Added a fix for handling keypad-enter key. // 2020-10-28: Inputs: Added a fix for handling keypad-enter key.
// 2020-05-25: Inputs: Added a fix for missing trackpad clicks when done with "soft tap". // 2020-05-25: Inputs: Added a fix for missing trackpad clicks when done with "soft tap".
@ -30,12 +31,15 @@
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
// 2018-07-07: Initial version. // 2018-07-07: Initial version.
@class ImFocusObserver;
// Data // Data
static CFAbsoluteTime g_Time = 0.0; static CFAbsoluteTime g_Time = 0.0;
static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; static NSCursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
static bool g_MouseCursorHidden = false; static bool g_MouseCursorHidden = false;
static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {}; static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
static bool g_MouseDown[ImGuiMouseButton_COUNT] = {}; static bool g_MouseDown[ImGuiMouseButton_COUNT] = {};
static ImFocusObserver* g_FocusObserver = NULL;
// Undocumented methods for creating cursors. // Undocumented methods for creating cursors.
@interface NSCursor() @interface NSCursor()
@ -45,6 +49,31 @@ static bool g_MouseDown[ImGuiMouseButton_COUNT] = {};
+ (id)_windowResizeEastWestCursor; + (id)_windowResizeEastWestCursor;
@end @end
static void resetKeys()
{
ImGuiIO& io = ImGui::GetIO();
memset(io.KeysDown, 0, sizeof(io.KeysDown));
io.KeyCtrl = io.KeyShift = io.KeyAlt = io.KeySuper = false;
}
@interface ImFocusObserver : NSObject
- (void)onApplicationBecomeInactive:(NSNotification*)aNotification;
@end
@implementation ImFocusObserver
- (void)onApplicationBecomeInactive:(NSNotification*)aNotification
{
// Unfocused applications do not receive input events, therefore we must manually
// release any pressed keys when application loses focus, otherwise they would remain
// stuck in a pressed state. https://github.com/ocornut/imgui/issues/3832
resetKeys();
}
@end
// Functions // Functions
bool ImGui_ImplOSX_Init() bool ImGui_ImplOSX_Init()
{ {
@ -123,11 +152,18 @@ bool ImGui_ImplOSX_Init()
return s_clipboard.Data; return s_clipboard.Data;
}; };
g_FocusObserver = [[ImFocusObserver alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:g_FocusObserver
selector:@selector(onApplicationBecomeInactive:)
name:NSApplicationDidResignActiveNotification
object:nil];
return true; return true;
} }
void ImGui_ImplOSX_Shutdown() void ImGui_ImplOSX_Shutdown()
{ {
g_FocusObserver = NULL;
} }
static void ImGui_ImplOSX_UpdateMouseCursorAndButtons() static void ImGui_ImplOSX_UpdateMouseCursorAndButtons()
@ -200,13 +236,6 @@ static int mapCharacterToKey(int c)
return -1; return -1;
} }
static void resetKeys()
{
ImGuiIO& io = ImGui::GetIO();
for (int n = 0; n < IM_ARRAYSIZE(io.KeysDown); n++)
io.KeysDown[n] = false;
}
bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view) bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();

View File

@ -64,6 +64,7 @@ Other Changes:
effectively fixing mouse access on Raspberry Pi. (#2837, #3950) [@lethal-guitar, @hinxx] effectively fixing mouse access on Raspberry Pi. (#2837, #3950) [@lethal-guitar, @hinxx]
- Backends: Win32: Clearing keyboard down array when losing focus (WM_KILLFOCUS). (#2062, #3532, #3961) - Backends: Win32: Clearing keyboard down array when losing focus (WM_KILLFOCUS). (#2062, #3532, #3961)
[@1025798851] [@1025798851]
- Backends: OSX: Fix keys remaining stuck when CMD-tabbing to a different application. (#3832) [@rokups]
- Backends: DirectX9: calling IDirect3DStateBlock9::Capture() after CreateStateBlock() which appears to - Backends: DirectX9: calling IDirect3DStateBlock9::Capture() after CreateStateBlock() which appears to
workaround/fix state restoring issues. Unknown exactly why so, but bit of a cargo-cult fix. (#3857) workaround/fix state restoring issues. Unknown exactly why so, but bit of a cargo-cult fix. (#3857)
- Backends: DirectX10, DirectX11: fixed a crash when backing/restoring state if nothing is bound when - Backends: DirectX10, DirectX11: fixed a crash when backing/restoring state if nothing is bound when