1
0
Fork 0

feat: implement key press events

This commit is contained in:
LordMZTE 2024-01-08 20:13:54 +01:00
parent fa98f472f8
commit ff9c651272
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 303 additions and 0 deletions

272
src/key.zig Normal file
View file

@ -0,0 +1,272 @@
/// Yoinked from SDL. Kinda trash.
pub const Scancode = enum {
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,
@"1",
@"2",
@"3",
@"4",
@"5",
@"6",
@"7",
@"8",
@"9",
@"0",
@"return",
escape,
backspace,
tab,
space,
minus,
equals,
leftbracket,
rightbracket,
backslash,
nonushash,
semicolon,
apostrophe,
grave,
comma,
period,
slash,
capslock,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10,
f11,
f12,
printscreen,
scrolllock,
pause,
insert,
home,
pageup,
delete,
end,
pagedown,
right,
left,
down,
up,
numlockclear,
kp_divide,
kp_multiply,
kp_minus,
kp_plus,
kp_enter,
kp_1,
kp_2,
kp_3,
kp_4,
kp_5,
kp_6,
kp_7,
kp_8,
kp_9,
kp_0,
kp_period,
nonusbackslash,
application,
power,
kp_equals,
f13,
f14,
f15,
f16,
f17,
f18,
f19,
f20,
f21,
f22,
f23,
f24,
execute,
help,
menu,
select,
stop,
again,
undo,
cut,
copy,
paste,
find,
mute,
volumeup,
volumedown,
kp_comma,
kp_equalsas400,
international1,
international2,
international3,
international4,
international5,
international6,
international7,
international8,
international9,
lang1,
lang2,
lang3,
lang4,
lang5,
lang6,
lang7,
lang8,
lang9,
alterase,
sysreq,
cancel,
clear,
prior,
return2,
separator,
out,
oper,
clearagain,
crsel,
exsel,
kp_00,
kp_000,
thousandsseparator,
decimalseparator,
currencyunit,
currencysubunit,
kp_leftparen,
kp_rightparen,
kp_leftbrace,
kp_rightbrace,
kp_tab,
kp_backspace,
kp_a,
kp_b,
kp_c,
kp_d,
kp_e,
kp_f,
kp_xor,
kp_power,
kp_percent,
kp_less,
kp_greater,
kp_ampersand,
kp_dblampersand,
kp_verticalbar,
kp_dblverticalbar,
kp_colon,
kp_hash,
kp_space,
kp_at,
kp_exclam,
kp_memstore,
kp_memrecall,
kp_memclear,
kp_memadd,
kp_memsubtract,
kp_memmultiply,
kp_memdivide,
kp_plusminus,
kp_clear,
kp_clearentry,
kp_binary,
kp_octal,
kp_decimal,
kp_hexadecimal,
lctrl,
lshift,
lalt,
lgui,
rctrl,
rshift,
ralt,
rgui,
mode,
audionext,
audioprev,
audiostop,
audioplay,
audiomute,
mediaselect,
www,
mail,
calculator,
computer,
ac_search,
ac_home,
ac_back,
ac_forward,
ac_stop,
ac_refresh,
ac_bookmarks,
brightnessdown,
brightnessup,
displayswitch,
kbdillumtoggle,
kbdillumdown,
kbdillumup,
eject,
sleep,
app1,
app2,
audiorewind,
audiofastforward,
softleft,
softright,
call,
endcall,
};
/// Modifier keys which may be active at the time of a key event.
pub const Modifiers = struct {
shift: bool = false,
ctrl: bool = false,
alt: bool = false,
meta: bool = false,
mode: bool = false, // AKA AltGr
};

View file

@ -5,6 +5,7 @@ const root = @import("root");
test {
_ = attreebute;
_ = backevent;
_ = key;
_ = layout;
_ = painter;
_ = platform;
@ -19,6 +20,7 @@ test {
pub const attreebute = @import("attreebute.zig");
pub const backevent = @import("backevent.zig");
pub const key = @import("key.zig");
pub const layout = @import("layout.zig");
pub const painter = @import("painter.zig");
pub const platform = @import("platform.zig");

View file

@ -13,6 +13,7 @@ const Widget = @import("widget.zig").Widget;
test {
_ = Click;
_ = Draw;
_ = KeyPress;
_ = LayoutPosition;
_ = LayoutSize;
_ = Link;
@ -21,6 +22,7 @@ test {
pub const Click = @import("treevents/Click.zig");
pub const Draw = @import("treevents/Draw.zig");
pub const KeyPress = @import("treevents/KeyPress.zig");
pub const LayoutPosition = @import("treevents/LayoutPosition.zig");
pub const LayoutSize = @import("treevents/LayoutSize.zig");
pub const Link = @import("treevents/Link.zig");

View file

@ -0,0 +1,27 @@
//! This treevent is fired when a key is typed, pressed or released on the keyboard.
//! It is a low-level event, which isn't suited for text input, but instead for raw key handling.
//! When a key is pressed BOTH an event with the .down action and one with the .press action (afterwards)
//! will be fired. The latter may be repeated if the key is held down afterwards.
const Widget = @import("../widget.zig").Widget;
const key = @import("../key.zig");
pub const Action = enum {
down,
up,
/// Fired for a key being "pressed". This may be repeated when a key is held down.
press,
};
action: Action,
scancode: key.Scancode,
modifiers: key.Modifiers,
const KeyPress = @This();
pub fn dispatch(self: KeyPress, widget: *Widget) !void {
for (widget.children()) |child| {
try child.treevent(self);
}
}