From e5cba11ad082fed600f0746536458857c7053722 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 9 Sep 2023 09:12:25 -0700 Subject: [PATCH] macos: key events for modifier-only keys --- macos/Sources/Ghostty/SurfaceView.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 118213608..4bcc37c22 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -405,6 +405,28 @@ extension Ghostty { keyAction(GHOSTTY_ACTION_RELEASE, event: event) } + override func flagsChanged(with event: NSEvent) { + let mod: UInt32; + switch (event.keyCode) { + case 0x39: mod = GHOSTTY_MODS_CAPS.rawValue + case 0x38, 0x3C: mod = GHOSTTY_MODS_SHIFT.rawValue + case 0x3B, 0x3E: mod = GHOSTTY_MODS_CTRL.rawValue + case 0x3A, 0x3D: mod = GHOSTTY_MODS_ALT.rawValue + case 0x37, 0x36: mod = GHOSTTY_MODS_SUPER.rawValue + default: return + } + + // The keyAction function will do this AGAIN below which sucks to repeat + // but this is super cheap and flagsChanged isn't that common. + let mods = Self.translateFlags(event.modifierFlags) + + // If the key that pressed this is active, its a press, else release + var action = GHOSTTY_ACTION_RELEASE + if (mods.rawValue & mod != 0) { action = GHOSTTY_ACTION_PRESS } + + keyAction(action, 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)