input: key enum is now aligned with W3C keyboard codes

This commit is contained in:
Mitchell Hashimoto
2025-05-08 08:21:31 -07:00
parent 7f9bb3c0e5
commit 91d15c89bc

View File

@@ -255,95 +255,144 @@ pub const Action = enum(c_int) {
repeat,
};
/// The set of keys that can map to keybindings. These have no fixed enum
/// values because we map platform-specific keys to this set. Note that
/// this only needs to accommodate what maps to a key. If a key is not bound
/// to anything and the key can be mapped to a printable character, then that
/// unicode character is sent directly to the pty.
/// The set of key codes that Ghostty is aware of. These represent
/// physical keys on the keyboard. The logical key (or key string)
/// is the string that is generated by the key event and that is up
/// to the apprt to provide.
///
/// This is backed by a c_int so we can use this as-is for our embedding API.
/// Note that these are layout-independent. For example, the "a"
/// key on a US keyboard is the same as the "ф" key on a Russian
/// keyboard, but both will report the "a" enum value in the key
/// event. These values are based on the W3C standard. See:
/// https://www.w3.org/TR/uievents-code
///
/// Layout-dependent strings are provided in the KeyEvent struct as
/// UTF-8 and are produced by the associated apprt. Ghostty core has
/// no mechanism to map input events to strings without the apprt.
///
/// IMPORTANT: Any changes here update include/ghostty.h
pub const Key = enum(c_int) {
invalid,
unidentified,
// a-z
a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p,
q,
r,
s,
t,
u,
v,
w,
x,
y,
z,
// numbers
zero,
one,
two,
three,
four,
five,
six,
seven,
eight,
nine,
// punctuation
semicolon,
space,
apostrophe,
// "Writing System Keys" § 3.1.1
backquote,
backslash,
bracket_left,
bracket_right,
comma,
grave_accent, // `
period,
slash,
minus,
plus,
digit_0,
digit_1,
digit_2,
digit_3,
digit_4,
digit_5,
digit_6,
digit_7,
digit_8,
digit_9,
equal,
left_bracket, // [
right_bracket, // ]
backslash, // \
intl_backslash,
intl_ro,
intl_yen,
key_a,
key_b,
key_c,
key_d,
key_e,
key_f,
key_g,
key_h,
key_i,
key_j,
key_k,
key_l,
key_m,
key_n,
key_o,
key_p,
key_q,
key_r,
key_s,
key_t,
key_u,
key_v,
key_w,
key_x,
key_y,
key_z,
minus,
period,
quote,
semicolon,
slash,
// control
up,
down,
right,
left,
home,
end,
insert,
delete,
caps_lock,
scroll_lock,
num_lock,
page_up,
page_down,
escape,
enter,
tab,
// "Functional Keys" § 3.1.2
alt_left,
alt_right,
backspace,
print_screen,
pause,
caps_lock,
context_menu,
control_left,
control_right,
enter,
meta_left,
meta_right,
shift_left,
shift_right,
space,
tab,
convert,
kana_mode,
non_convert,
// function keys
// "Control Pad Section" § 3.2
delete,
end,
help,
home,
insert,
page_down,
page_up,
// "Arrow Pad Section" § 3.3
arrow_down,
arrow_left,
arrow_right,
arrow_up,
// "Numpad Section" § 3.4
num_lock,
numpad_0,
numpad_1,
numpad_2,
numpad_3,
numpad_4,
numpad_5,
numpad_6,
numpad_7,
numpad_8,
numpad_9,
numpad_add,
numpad_backspace,
numpad_clear,
numpad_clear_entry,
numpad_comma,
numpad_decimal,
numpad_divide,
numpad_enter,
numpad_equal,
numpad_memory_add,
numpad_memory_clear,
numpad_memory_recall,
numpad_memory_store,
numpad_memory_subtract,
numpad_multiply,
numpad_paren_left,
numpad_paren_right,
numpad_subtract,
// "Function Section" § 3.5
escape,
f1,
f2,
f3,
@@ -356,66 +405,119 @@ pub const Key = enum(c_int) {
f10,
f11,
f12,
f13,
f14,
f15,
f16,
f17,
f18,
f19,
f20,
f21,
f22,
f23,
f24,
f25,
@"fn",
fn_lock,
print_screen,
scroll_lock,
pause,
// keypad
kp_0,
kp_1,
kp_2,
kp_3,
kp_4,
kp_5,
kp_6,
kp_7,
kp_8,
kp_9,
kp_decimal,
kp_divide,
kp_multiply,
kp_subtract,
kp_add,
kp_enter,
kp_equal,
kp_separator,
kp_left,
kp_right,
kp_up,
kp_down,
kp_page_up,
kp_page_down,
kp_home,
kp_end,
kp_insert,
kp_delete,
kp_begin,
// "Media Keys" § 3.6
browser_back,
browser_favorites,
browser_forward,
browser_home,
browser_refresh,
browser_search,
browser_stop,
eject,
launch_app_1,
launch_app_2,
launch_mail,
media_play_pause,
media_select,
media_stop,
media_track_next,
media_track_previous,
power,
sleep,
audio_volume_down,
audio_volume_mute,
audio_volume_up,
wake_up,
// special keys
context_menu,
// modifiers
left_shift,
left_control,
left_alt,
left_super,
right_shift,
right_control,
right_alt,
right_super,
// To support more keys (there are obviously more!) add them here
// and ensure the mapping is up to date in the Window key handler.
// Backwards compatibility for Ghostty 1.1.x and earlier, we don't
// want to force people to rewrite their configs.
pub const a = .key_a;
pub const b = .key_b;
pub const c = .key_c;
pub const d = .key_d;
pub const e = .key_e;
pub const f = .key_f;
pub const g = .key_g;
pub const h = .key_h;
pub const i = .key_i;
pub const j = .key_j;
pub const k = .key_k;
pub const l = .key_l;
pub const m = .key_m;
pub const n = .key_n;
pub const o = .key_o;
pub const p = .key_p;
pub const q = .key_q;
pub const r = .key_r;
pub const s = .key_s;
pub const t = .key_t;
pub const u = .key_u;
pub const v = .key_v;
pub const w = .key_w;
pub const x = .key_x;
pub const y = .key_y;
pub const z = .key_z;
pub const zero = .digit_0;
pub const one = .digit_1;
pub const two = .digit_2;
pub const three = .digit_3;
pub const four = .digit_4;
pub const five = .digit_5;
pub const six = .digit_6;
pub const seven = .digit_7;
pub const eight = .digit_8;
pub const nine = .digit_9;
pub const apostrophe = .quote;
pub const grave_accent = .backquote;
pub const left_bracket = .bracket_left;
pub const right_bracket = .bracket_right;
pub const up = .arrow_up;
pub const down = .arrow_down;
pub const left = .arrow_left;
pub const right = .arrow_right;
pub const kp_0 = .numpad_0;
pub const kp_1 = .numpad_1;
pub const kp_2 = .numpad_2;
pub const kp_3 = .numpad_3;
pub const kp_4 = .numpad_4;
pub const kp_5 = .numpad_5;
pub const kp_6 = .numpad_6;
pub const kp_7 = .numpad_7;
pub const kp_8 = .numpad_8;
pub const kp_9 = .numpad_9;
pub const kp_decimal = .numpad_decimal;
pub const kp_divide = .numpad_divide;
pub const kp_multiply = .numpad_multiply;
pub const kp_subtract = .numpad_subtract;
pub const kp_add = .numpad_add;
pub const kp_enter = .numpad_enter;
pub const kp_equal = .numpad_equal;
pub const kp_separator = .numpad_separator;
pub const kp_left = .numpad_left;
pub const kp_right = .numpad_right;
pub const kp_up = .numpad_up;
pub const kp_down = .numpad_down;
pub const kp_page_up = .numpad_page_up;
pub const kp_page_down = .numpad_page_down;
pub const kp_home = .numpad_home;
pub const kp_end = .numpad_end;
pub const kp_insert = .numpad_insert;
pub const kp_delete = .numpad_delete;
pub const kp_begin = .numpad_begin;
pub const left_shift = .shift_left;
pub const right_shift = .shift_right;
pub const left_control = .control_left;
pub const right_control = .control_right;
pub const left_alt = .alt_left;
pub const right_alt = .alt_right;
pub const left_super = .meta_left;
pub const right_super = .meta_right;
/// Converts an ASCII character to a key, if possible. This returns
/// null if the character is unknown.