diff options
| author | Mitchell Hashimoto <m@mitchellh.com> | 2025-02-13 15:03:54 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-13 15:03:54 -0800 |
| commit | ee963f62968264157003d84d579b46b9df2e7806 (patch) | |
| tree | 64eafc1b9508af3c8c688ec76dcb5b65d53005e8 /src/input/KeymapDarwin.zig | |
| parent | 710ea1c8d9a1005a9f7f03fe98297b13a21b8e44 (diff) | |
| parent | b44b1086d355fc269dde5979bc69e85cdf1990b2 (diff) | |
macOS: fix invalid kitty keyboard encoding of control characters (#5747)v1.1.2
Fixes #5743
This fixes a terrible regression where by fixing one issue we introduced
another, and the other is that ctrl keys didn't work with programs with
Kitty keyboard protocol.
The problem is that our fix blindly assumed control was always consumed
for translation, which is obviously wrong but I didn't think there'd be
downstream effects. The reality is that we need to be more accurate.
This PR makes it so that:
* If macOS provides us with UTF-8 text, we assume all mods were involved
except super
* If macOS does not provide us with UTF-8 text, we use whatever
UCKeyTranslate consumed
* We never allow UCKeyTranslate to consume control because it turns it
into the masked ASCII value and we don't want that.
The only _new_ behavior which fixes the bug is point 2 above.
Tested:
1. Dvorak Ctrl characters
2. Ergo-L Ctrl characters
3. US standard Ctrl characters
4. Japanese IME input Ctrl input to modify IME state
5. Ctrl keybindings with Kitty keyboard protocol in both Kitty and
Neovim
Diffstat (limited to 'src/input/KeymapDarwin.zig')
| -rw-r--r-- | src/input/KeymapDarwin.zig | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/input/KeymapDarwin.zig b/src/input/KeymapDarwin.zig index 3d81b0f4b..154f648a6 100644 --- a/src/input/KeymapDarwin.zig +++ b/src/input/KeymapDarwin.zig @@ -50,10 +50,13 @@ pub const State = struct { pub const Translation = struct { /// The translation result. If this is a dead key state, then this will /// be pre-edit text that can be displayed but will ultimately be replaced. - text: []const u8, + text: []const u8 = "", /// Whether the text is still composing, i.e. this is a dead key state. - composing: bool, + composing: bool = false, + + /// The mods that were consumed to produce this translation + mods: Mods = .{}, }; pub fn init() !Keymap { @@ -122,8 +125,18 @@ pub fn translate( out: []u8, state: *State, code: u16, - mods: Mods, + input_mods: Mods, ) !Translation { + // On macOS we strip ctrl because UCKeyTranslate + // converts to the masked values (i.e. ctrl+c becomes 3) + // and we don't want that behavior in Ghostty ever. This makes + // this file not a general-purpose keymap implementation. + const mods: Mods = mods: { + var v = input_mods; + v.ctrl = false; + break :mods v; + }; + // Get the keycode for the space key, using comptime. const code_space: u16 = comptime space: for (codes) |entry| { if (std.mem.eql(u8, entry.code, "Space")) @@ -183,7 +196,11 @@ pub fn translate( // Convert the utf16 to utf8 const len = try std.unicode.utf16leToUtf8(out, char[0..char_count]); - return .{ .text = out[0..len], .composing = composing }; + return .{ + .text = out[0..len], + .composing = composing, + .mods = mods, + }; } /// Map to the modifiers format used by the UCKeyTranslate function. |
