input: distinguish ctrl+j from enter (#149)

`\x0A` was being parsed as an enter keypress. Distinguish this explicitly as a ctrl+j press.
This commit is contained in:
kuro337 2025-01-16 21:57:06 -05:00 committed by GitHub
parent 8462b6276e
commit 91a310c933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 8 additions and 9 deletions

View file

@ -62,7 +62,7 @@ pub fn main() !void {
} else { } else {
selected_option.? = selected_option.? -| 1; selected_option.? = selected_option.? -| 1;
} }
} else if (key.matches(vaxis.Key.enter, .{})) { } else if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) {
if (selected_option) |i| { if (selected_option) |i| {
log.err("enter", .{}); log.err("enter", .{});
try text_input.insertSliceAtCursor(options[i]); try text_input.insertSliceAtCursor(options[i]);

View file

@ -167,12 +167,12 @@ pub fn main() !void {
demo_tbl.sel_rows = try rows_list.toOwnedSlice(); demo_tbl.sel_rows = try rows_list.toOwnedSlice();
} }
// See Row Content // See Row Content
if (key.matches(vaxis.Key.enter, .{})) see_content = !see_content; if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) see_content = !see_content;
}, },
.btm => { .btm => {
if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{}) and moving) active = .mid if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{}) and moving) active = .mid
// Run Command and Clear Command Bar // Run Command and Clear Command Bar
else if (key.matchExact(vaxis.Key.enter, .{})) { else if (key.matchExact(vaxis.Key.enter, .{}) or key.matchExact('j', .{ .ctrl = true })) {
const cmd = try cmd_input.toOwnedSlice(); const cmd = try cmd_input.toOwnedSlice();
defer alloc.free(cmd); defer alloc.free(cmd);
if (mem.eql(u8, ":q", cmd) or if (mem.eql(u8, ":q", cmd) or

View file

@ -99,7 +99,7 @@ pub fn main() !void {
try loop.start(); try loop.start();
try vx.enterAltScreen(tty.anyWriter()); try vx.enterAltScreen(tty.anyWriter());
vx.queueRefresh(); vx.queueRefresh();
} else if (key.matches(vaxis.Key.enter, .{})) { } else if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) {
text_input.clearAndFree(); text_input.clearAndFree();
} else { } else {
try text_input.update(.{ .key_press = key }); try text_input.update(.{ .key_press = key });

View file

@ -94,9 +94,8 @@ inline fn parseGround(input: []const u8, data: *const grapheme.GraphemeData) !Re
0x00 => .{ .codepoint = '@', .mods = .{ .ctrl = true } }, 0x00 => .{ .codepoint = '@', .mods = .{ .ctrl = true } },
0x08 => .{ .codepoint = Key.backspace }, 0x08 => .{ .codepoint = Key.backspace },
0x09 => .{ .codepoint = Key.tab }, 0x09 => .{ .codepoint = Key.tab },
0x0A, 0x0A => .{ .codepoint = 'j', .mods = .{ .ctrl = true } },
0x0D, 0x0D => .{ .codepoint = Key.enter },
=> .{ .codepoint = Key.enter },
0x01...0x07, 0x01...0x07,
0x0B...0x0C, 0x0B...0x0C,
0x0E...0x1A, 0x0E...0x1A,

View file

@ -44,7 +44,7 @@ fn typeErasedEventHandler(ptr: *anyopaque, ctx: *vxfw.EventContext, event: vxfw.
pub fn handleEvent(self: *Button, ctx: *vxfw.EventContext, event: vxfw.Event) anyerror!void { pub fn handleEvent(self: *Button, ctx: *vxfw.EventContext, event: vxfw.Event) anyerror!void {
switch (event) { switch (event) {
.key_press => |key| { .key_press => |key| {
if (key.matches(vaxis.Key.enter, .{})) { if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) {
return self.doClick(ctx); return self.doClick(ctx);
} }
}, },

View file

@ -102,7 +102,7 @@ pub fn handleEvent(self: *TextField, ctx: *vxfw.EventContext, event: vxfw.Event)
} else if (key.matches('d', .{ .alt = true })) { } else if (key.matches('d', .{ .alt = true })) {
self.deleteWordAfter(); self.deleteWordAfter();
return self.checkChanged(ctx); return self.checkChanged(ctx);
} else if (key.matches(vaxis.Key.enter, .{})) { } else if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) {
if (self.onSubmit) |onSubmit| { if (self.onSubmit) |onSubmit| {
try onSubmit(self.userdata, ctx, self.previous_val); try onSubmit(self.userdata, ctx, self.previous_val);
return ctx.consumeAndRedraw(); return ctx.consumeAndRedraw();