From ce57c747cc0e0992916f47ee25a0c37a4640d201 Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 4 Feb 2022 12:51:01 +0800 Subject: [PATCH] macOS: Fix select next input source not working For global shortcut to switch the next input source in the macOS system preferences, final confirmation is only given when all modifier keys are released. --- glfw/cocoa_init.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m index e3e92410a..6aac09149 100644 --- a/glfw/cocoa_init.m +++ b/glfw/cocoa_init.m @@ -753,9 +753,11 @@ int _glfwPlatformInit(void) [NSApp setDelegate:_glfw.ns.delegate]; static struct { unsigned short virtual_key_code; + NSEventModifierFlags input_source_switch_modifiers; NSTimeInterval timestamp; } last_keydown_shortcut_event; last_keydown_shortcut_event.virtual_key_code = 0xffff; + last_keydown_shortcut_event.input_source_switch_modifiers = 0; NSEvent* (^keydown_block)(NSEvent*) = ^ NSEvent* (NSEvent* event) { @@ -766,6 +768,7 @@ int _glfwPlatformInit(void) if ([[NSApp mainMenu] performKeyEquivalent:event]) { debug_key("keyDown triggerred global menu bar action ignoring\n"); last_keydown_shortcut_event.virtual_key_code = [event keyCode]; + last_keydown_shortcut_event.input_source_switch_modifiers = 0; last_keydown_shortcut_event.timestamp = [event timestamp]; return nil; } @@ -774,6 +777,8 @@ int _glfwPlatformInit(void) if (is_useful_apple_global_shortcut(global_shortcut)) { debug_key("keyDown triggerred global macOS shortcut ignoring\n"); last_keydown_shortcut_event.virtual_key_code = [event keyCode]; + // record the modifier keys if switching to the next input source + last_keydown_shortcut_event.input_source_switch_modifiers = (global_shortcut == kSHKSelectNextSourceInInputMenu) ? USEFUL_MODS([event modifierFlags]) : 0; last_keydown_shortcut_event.timestamp = [event timestamp]; return event; } @@ -806,6 +811,12 @@ int _glfwPlatformInit(void) debug_key("-------------- flags changed -----------------\n"); debug_key("%s\n", [[event description] UTF8String]); last_keydown_shortcut_event.virtual_key_code = 0xffff; + // switching to the next input source is only confirmed when all modifier keys are released + if (last_keydown_shortcut_event.input_source_switch_modifiers) { + if (!([event modifierFlags] & last_keydown_shortcut_event.input_source_switch_modifiers)) + last_keydown_shortcut_event.input_source_switch_modifiers = 0; + return event; + } NSWindow *kw = [NSApp keyWindow]; if (kw && kw.contentView) [kw.contentView flagsChanged:event]; else debug_key("flagsChanged ignored as no keyWindow present\n");