bd44b5aa08
Test / build-libghostty-vt (x86_64-windows-gnu) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-ios) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt-macos (x86_64-macos) (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-dist (push) Has been cancelled
Test / GTK x11=true wayland=true (push) Has been cancelled
Test / Build -Dsimd=false (push) Has been cancelled
Test / Build -Dsimd=true (push) Has been cancelled
Test / Build -Dsentry=false (push) Has been cancelled
Test / Build -Dsentry=true (push) Has been cancelled
Test / zig-fmt (push) Has been cancelled
Test / GitHub Actions Pins (push) Has been cancelled
Test / prettier (push) Has been cancelled
Test / swiftlint (push) Has been cancelled
Test / alejandra (push) Has been cancelled
Test / typos (push) Has been cancelled
Nix / Required Checks: Nix (push) Has been cancelled
Nix / check-zig-cache-hash (push) Has been cancelled
Test / skip (push) Has been cancelled
Test / Required Checks: Test (push) Has been cancelled
Test / build-bench (push) Has been cancelled
Test / list-examples (push) Has been cancelled
Test / Example ${{ matrix.dir }} (Windows) (push) Has been cancelled
Test / build-cmake (push) Has been cancelled
Test / test-lib-vt-pkgconfig (push) Has been cancelled
Test / build-flatpak (push) Has been cancelled
Test / build-snap (push) Has been cancelled
Test / build-libghostty-vt (aarch64-linux) (push) Has been cancelled
Test / build-libghostty-vt (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt (wasm32-freestanding) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux-musl) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-macos) (push) Has been cancelled
Test / build-libghostty-vt-android (aarch64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-android (arm-linux-androideabi) (push) Has been cancelled
Test / build-libghostty-vt-android (x86_64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-windows (push) Has been cancelled
Test / build-libghostty-windows-gnu (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-linux-libghostty (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-dist-lib-vt (push) Has been cancelled
Test / trigger-snap (push) Has been cancelled
Test / trigger-flatpak (push) Has been cancelled
Test / build-macos (push) Has been cancelled
Test / build-macos-freetype (push) Has been cancelled
Test / test (push) Has been cancelled
Test / test-lib-vt (push) Has been cancelled
Test / GTK x11=false wayland=false (push) Has been cancelled
Test / GTK x11=true wayland=false (push) Has been cancelled
Test / GTK x11=false wayland=true (push) Has been cancelled
Test / test-macos (push) Has been cancelled
Test / test-windows (push) Has been cancelled
Test / Build -Di18n=false (push) Has been cancelled
Test / Build -Di18n=true (push) Has been cancelled
Test / Build test/fuzz-libghostty (push) Has been cancelled
Test / shellcheck (push) Has been cancelled
Test / translations (push) Has been cancelled
Test / blueprint-compiler (push) Has been cancelled
Test / Test pkg/wuffs (push) Has been cancelled
Test / Test build on Debian 13 (push) Has been cancelled
Test / valgrind (push) Has been cancelled
Test / Example ${{ matrix.dir }} (push) Has been cancelled
77 lines
3.1 KiB
Swift
77 lines
3.1 KiB
Swift
import Cocoa
|
|
import GhosttyKit
|
|
|
|
extension NSEvent {
|
|
/// Create a Ghostty key event for a given keyboard action.
|
|
///
|
|
/// This will not set the "text" or "composing" fields since these can't safely be set
|
|
/// with the information or lifetimes given.
|
|
///
|
|
/// The translationMods should be set to the modifiers used for actual character
|
|
/// translation if available.
|
|
func ghosttyKeyEvent(
|
|
_ action: ghostty_input_action_e,
|
|
translationMods: NSEvent.ModifierFlags? = nil
|
|
) -> ghostty_input_key_s {
|
|
var key_ev: ghostty_input_key_s = .init()
|
|
key_ev.action = action
|
|
key_ev.keycode = UInt32(keyCode)
|
|
|
|
// We can't infer or set these safely from this method. Since text is
|
|
// a cString, we can't use self.characters because of garbage collection.
|
|
// We have to let the caller handle this.
|
|
key_ev.text = nil
|
|
key_ev.composing = false
|
|
|
|
// macOS provides no easy way to determine the consumed modifiers for
|
|
// producing text. We apply a simple heuristic here that has worked for years
|
|
// so far: control and command never contribute to the translation of text,
|
|
// assume everything else did.
|
|
key_ev.mods = Ghostty.ghosttyMods(modifierFlags)
|
|
key_ev.consumed_mods = Ghostty.ghosttyMods(
|
|
(translationMods ?? modifierFlags)
|
|
.subtracting([.control, .command]))
|
|
|
|
// Our unshifted codepoint is the codepoint with no modifiers. We
|
|
// ignore multi-codepoint values. We have to use `byApplyingModifiers`
|
|
// instead of `charactersIgnoringModifiers` because the latter changes
|
|
// behavior with ctrl pressed and we don't want any of that.
|
|
key_ev.unshifted_codepoint = 0
|
|
if type == .keyDown || type == .keyUp {
|
|
if let chars = characters(byApplyingModifiers: []),
|
|
let codepoint = chars.unicodeScalars.first {
|
|
key_ev.unshifted_codepoint = codepoint.value
|
|
}
|
|
}
|
|
|
|
return key_ev
|
|
}
|
|
|
|
/// Returns the text to set for a key event for Ghostty.
|
|
///
|
|
/// This namely contains logic to avoid control characters, since we handle control character
|
|
/// mapping manually within Ghostty.
|
|
var ghosttyCharacters: String? {
|
|
// If we have no characters associated with this event we do nothing.
|
|
guard let characters else { return nil }
|
|
|
|
if characters.count == 1,
|
|
let scalar = characters.unicodeScalars.first {
|
|
// If we have a single control character, then we return the characters
|
|
// without control pressed. We do this because we handle control character
|
|
// encoding directly within Ghostty's KeyEncoder.
|
|
if scalar.value < 0x20 {
|
|
return self.characters(byApplyingModifiers: modifierFlags.subtracting(.control))
|
|
}
|
|
|
|
// If we have a single value in the PUA, then it's a function key and
|
|
// we don't want to send PUA ranges down to Ghostty.
|
|
if scalar.value >= 0xF700 && scalar.value <= 0xF8FF {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return characters
|
|
}
|
|
}
|