fix(web): encode shifted characters correctly in the terminal (#7485)
This commit is contained in:
1 parent
6d5c6c4a67
commit
18f6d03489
3 files changed
+28
-2
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
type GhosttyKeyboardLayoutMap,
|
||||
ghosttyConsumedMods,
|
||||
ghosttyKeyForCode,
|
||||
ghosttyUnshiftedCodepoint,
|
||||
loadGhosttyKeyboardLayoutMap,
|
||||
@@ -464,7 +465,11 @@ export class GhosttyTerminalCore {
|
||||
(event.getModifierState("CapsLock") ? 1 << 4 : 0) |
|
||||
(event.getModifierState("NumLock") ? 1 << 5 : 0);
|
||||
this.runtime.call("ghostty_key_event_set_mods", this.keyEvent, mods);
|
||||
this.runtime.call("ghostty_key_event_set_consumed_mods", this.keyEvent, 0);
|
||||
this.runtime.call(
|
||||
"ghostty_key_event_set_consumed_mods",
|
||||
this.keyEvent,
|
||||
ghosttyConsumedMods(event),
|
||||
);
|
||||
this.runtime.call("ghostty_key_event_set_composing", this.keyEvent, event.isComposing ? 1 : 0);
|
||||
this.runtime.call(
|
||||
"ghostty_key_event_set_unshifted_codepoint",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { ghosttyKeyForCode, ghosttyUnshiftedCodepoint } from "./keyCodes";
|
||||
import { ghosttyConsumedMods, ghosttyKeyForCode, ghosttyUnshiftedCodepoint } from "./keyCodes";
|
||||
|
||||
describe("ghosttyKeyForCode", () => {
|
||||
it("keeps the tail of the pinned Ghostty key enum in order", () => {
|
||||
@@ -11,6 +11,18 @@ describe("ghosttyKeyForCode", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("ghosttyConsumedMods", () => {
|
||||
const shifted = { altKey: false, ctrlKey: false, key: "@", metaKey: false, shiftKey: true };
|
||||
|
||||
it("only consumes a lone Shift producing a character", () => {
|
||||
expect(ghosttyConsumedMods(shifted)).toBe(1);
|
||||
expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
|
||||
expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
|
||||
// Deliberate: Shift+Space collapses to Space so it still types one.
|
||||
expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ghosttyUnshiftedCodepoint", () => {
|
||||
it("provides the logical base character for Kitty keyboard encoding", () => {
|
||||
expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "c", shiftKey: false })).toBe(
|
||||
|
||||
@@ -233,6 +233,15 @@ export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap
|
||||
return promise;
|
||||
}
|
||||
|
||||
// Browsers do not expose consumed modifiers; treat Shift as consumed for
|
||||
// unchorded character input.
|
||||
export function ghosttyConsumedMods(
|
||||
event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
|
||||
): number {
|
||||
if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
|
||||
return [...event.key].length === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
export function ghosttyUnshiftedCodepoint(
|
||||
event: Pick<KeyboardEvent, "code" | "key" | "shiftKey">,
|
||||
layoutMap?: GhosttyKeyboardLayoutMap,
|
||||
|
||||
Reference in new issue
Block a user