From 2664596a34a2c59968f85d74eef45c5c5120e92d Mon Sep 17 00:00:00 2001 From: Cyandev Date: Thu, 6 Nov 2025 07:19:32 +0800 Subject: [PATCH] gpui: Fix incorrect handling of Function key modifier on macOS (#38518) On macOS, the Function key is reserved for system use and should not be used in application code. This commit updated keystroke matching and key event handling to ignore the Function key modifier while users are typing or pressing keybindings. For some keyboards with compact layout (like my 65% keyboard), there is no separated backtick key. Esc and it shares the same physical key. To input backtick, users may press `Fn-Esc`. However, macOS will still deliver events with Fn key modifier to applications. Cocoa framework can handle this correctly, which typically ignore the Fn directly. GPUI should also follow the same rule, otherwise, the backtick key on those keyboards won't work. Release Notes: - Fixed a bug where typing fn-\` on macOS would not insert a `. --- crates/gpui/src/platform/keystroke.rs | 8 ++++++++ crates/gpui/src/platform/mac/window.rs | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/gpui/src/platform/keystroke.rs b/crates/gpui/src/platform/keystroke.rs index 4a2bfc785e3eb7e13a845bb67b4524255affb3bb..e1f1b0c9fbba5367eaabc5ca2de920a3eebdd4ca 100644 --- a/crates/gpui/src/platform/keystroke.rs +++ b/crates/gpui/src/platform/keystroke.rs @@ -572,6 +572,14 @@ impl Modifiers { } } + /// Returns [`Modifiers`] with just function. + pub fn function() -> Modifiers { + Modifiers { + function: true, + ..Default::default() + } + } + /// Returns [`Modifiers`] with command + shift. pub fn command_shift() -> Modifiers { Modifiers { diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 9c56d24e6857ca8fd8cc891f7c4f6657a06f86f0..11ea4fb7e272c0660c7981cabf8f8c74ffa71830 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -1753,9 +1753,9 @@ extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent: } } - // Don't send key equivalents to the input handler, - // or macOS shortcuts like cmd-` will stop working. - if key_equivalent { + // Don't send key equivalents to the input handler if there are key modifiers other + // than Function key, or macOS shortcuts like cmd-` will stop working. + if key_equivalent && key_down_event.keystroke.modifiers != Modifiers::function() { return NO; }