From 148ff80572c960b9e86e504f57224f5f9cc43dcd Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Sun, 21 Jun 2026 16:29:59 +0900 Subject: [PATCH] Cocoa: Bypass text input system when unfocused When explicit text input focus is inactive, skip interpretKeyEvents and send plain event characters through GLFW text input instead. This prevents routing key events through NSTextInput composition while preserving normal key input. Applications that never call glfwSetTextInputFocus keep the previous interpretKeyEvents behavior. On focus out, discard marked text through the existing NSTextInputContext reset path. Do not use TIS input source switching for this API. This builds on the Cocoa input method work from clear-code/glfw#7. --- CONTRIBUTORS.md | 1 + src/cocoa_window.m | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index e5fcf416..1b494c0a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -89,6 +89,7 @@ video tutorials. - GeO4d - Marcus Geelnard - Gegy + - ghostflyby - ghuser404 - Charles Giessen - Ryan C. Gordon diff --git a/src/cocoa_window.m b/src/cocoa_window.m index f7fb59c3..ea21398c 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -573,7 +573,14 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; if (![self hasMarkedText]) _glfwInputKey(window, key, [event keyCode], GLFW_PRESS, mods); - [self interpretKeyEvents:@[event]]; + if (!window->textInputFocusInitialized || window->textInputFocus) + [self interpretKeyEvents:@[event]]; + else + { + NSString* characters = [event characters]; + if (characters) + [self insertText:characters replacementRange:[self selectedRange]]; + } } - (void)flagsChanged:(NSEvent *)event @@ -2039,7 +2046,8 @@ void _glfwResetPreeditTextCocoa(_GLFWwindow* window) void _glfwSetTextInputFocusCocoa(_GLFWwindow* window, GLFWbool focused) { - // TODO: Add a safe NSTextInputContext mapping without changing TIS behavior. + if (!focused) + _glfwResetPreeditTextCocoa(window); } void _glfwSetIMEStatusCocoa(_GLFWwindow* window, int active)