From decaee61b27f523eac61036672cf1d0266bbc012 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 25 Mar 2023 15:44:17 -0700 Subject: [PATCH] apprt/embedded: support unmapped keys --- include/ghostty.h | 2 +- macos/Sources/Ghostty/SurfaceView.swift | 26 +++++++++++++------------ src/apprt/embedded.zig | 5 ++++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 96f7de272..572a0eaf5 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -261,7 +261,7 @@ void ghostty_surface_refresh(ghostty_surface_t); void ghostty_surface_set_content_scale(ghostty_surface_t, double, double); void ghostty_surface_set_focus(ghostty_surface_t, bool); void ghostty_surface_set_size(ghostty_surface_t, uint32_t, uint32_t); -void ghostty_surface_key(ghostty_surface_t, ghostty_input_action_e, ghostty_input_key_e, ghostty_input_mods_e); +void ghostty_surface_key(ghostty_surface_t, ghostty_input_action_e, ghostty_input_key_e, ghostty_input_key_e, ghostty_input_mods_e); void ghostty_surface_char(ghostty_surface_t, uint32_t); void ghostty_surface_mouse_button(ghostty_surface_t, ghostty_input_mouse_state_e, ghostty_input_mouse_button_e, ghostty_input_mods_e); void ghostty_surface_mouse_pos(ghostty_surface_t, double, double); diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index daacc8028..1bbf15fe4 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -302,9 +302,20 @@ extension Ghostty { } override func keyDown(with event: NSEvent) { + let action = event.isARepeat ? GHOSTTY_ACTION_REPEAT : GHOSTTY_ACTION_PRESS + keyAction(action, event: event) + + self.interpretKeyEvents([event]) + } + + override func keyUp(with event: NSEvent) { + keyAction(GHOSTTY_ACTION_RELEASE, event: event) + } + + private func keyAction(_ action: ghostty_input_action_e, event: NSEvent) { guard let surface = self.surface else { return } let mods = Self.translateFlags(event.modifierFlags) - let action = event.isARepeat ? GHOSTTY_ACTION_REPEAT : GHOSTTY_ACTION_PRESS + let unmapped_key = Self.keycodes[event.keyCode] ?? GHOSTTY_KEY_INVALID // We translate the key to the localized keyboard layout. However, we only support // ASCII characters to make our translation easier across platforms. This is something @@ -320,19 +331,10 @@ extension Ghostty { } } - return Self.keycodes[event.keyCode] ?? GHOSTTY_KEY_INVALID + return unmapped_key }() - ghostty_surface_key(surface, action, key, mods) - - self.interpretKeyEvents([event]) - } - - override func keyUp(with event: NSEvent) { - guard let surface = self.surface else { return } - let key = Self.keycodes[event.keyCode] ?? GHOSTTY_KEY_INVALID - let mods = Self.translateFlags(event.modifierFlags) - ghostty_surface_key(surface, GHOSTTY_ACTION_RELEASE, key, mods) + ghostty_surface_key(surface, action, key, unmapped_key, mods) } // MARK: NSTextInputClient diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 08fd04a5d..40c159c89 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -324,10 +324,11 @@ pub const Surface = struct { self: *Surface, action: input.Action, key: input.Key, + unmapped_key: input.Key, mods: input.Mods, ) void { // log.warn("key action={} key={} mods={}", .{ action, key, mods }); - self.core_surface.keyCallback(action, key, mods) catch |err| { + self.core_surface.keyCallback(action, key, unmapped_key, mods) catch |err| { log.err("error in key callback err={}", .{err}); return; }; @@ -460,11 +461,13 @@ pub const CAPI = struct { surface: *Surface, action: input.Action, key: input.Key, + unmapped_key: input.Key, mods: c_int, ) void { surface.keyCallback( action, key, + unmapped_key, @bitCast(input.Mods, @truncate(u8, @bitCast(c_uint, mods))), ); }