Skip to content

Commit 3964f86

Browse files
committed
initial keyboard event
1 parent 66e403c commit 3964f86

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

src/browser/events/event.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ const AbortSignal = @import("../html/AbortController.zig").AbortSignal;
3333
const CustomEvent = @import("custom_event.zig").CustomEvent;
3434
const ProgressEvent = @import("../xhr/progress_event.zig").ProgressEvent;
3535
const MouseEvent = @import("mouse_event.zig").MouseEvent;
36+
const KeyboardEvent = @import("keyboard_event.zig").KeyboardEvent;
3637
const ErrorEvent = @import("../html/error_event.zig").ErrorEvent;
3738
const MessageEvent = @import("../dom/MessageChannel.zig").MessageEvent;
3839

3940
// Event interfaces
40-
pub const Interfaces = .{ Event, CustomEvent, ProgressEvent, MouseEvent, ErrorEvent, MessageEvent };
41+
pub const Interfaces = .{
42+
Event,
43+
CustomEvent,
44+
ProgressEvent,
45+
MouseEvent,
46+
KeyboardEvent,
47+
ErrorEvent,
48+
MessageEvent,
49+
};
4150

4251
pub const Union = generate.Union(Interfaces);
4352

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const std = @import("std");
2+
const log = @import("../../log.zig");
3+
4+
const netsurf = @import("../netsurf.zig");
5+
const Event = @import("event.zig").Event;
6+
const JsObject = @import("../env.zig").JsObject;
7+
8+
const c = @cImport({
9+
@cInclude("dom/dom.h");
10+
@cInclude("core/pi.h");
11+
@cInclude("dom/bindings/hubbub/parser.h");
12+
@cInclude("events/event_target.h");
13+
@cInclude("events/event.h");
14+
@cInclude("events/mouse_event.h");
15+
@cInclude("events/keyboard_event.h");
16+
@cInclude("utils/validate.h");
17+
@cInclude("html/html_element.h");
18+
@cInclude("html/html_document.h");
19+
});
20+
21+
// TODO: We currently don't have a UIEvent interface so we skip it in the prototype chain.
22+
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent
23+
const UIEvent = Event;
24+
25+
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
26+
pub const KeyboardEvent = struct {
27+
pub const Self = netsurf.KeyboardEvent;
28+
pub const prototype = *UIEvent;
29+
30+
pub const KeyLocationCode = enum(u16) {
31+
standard = 0x00,
32+
left = 0x01,
33+
right = 0x02,
34+
numpad = 0x03,
35+
mobile = 0x04, // Non-standard, deprecated.
36+
joystick = 0x05, // Non-standard, deprecated.
37+
};
38+
39+
pub const ConstructorOptions = struct {
40+
key: []const u8 = "",
41+
code: []const u8 = "",
42+
location: KeyLocationCode = .standard,
43+
char_code: u32 = 0,
44+
key_code: u32 = 0,
45+
which: u32 = 0,
46+
repeat: bool = false,
47+
ctrl_key: bool = false,
48+
shift_key: bool = false,
49+
alt_key: bool = false,
50+
meta_key: bool = false,
51+
is_composing: bool = false,
52+
};
53+
54+
pub fn constructor(event_type: []const u8, maybe_options: ?ConstructorOptions) !*netsurf.KeyboardEvent {
55+
const options = maybe_options orelse ConstructorOptions{};
56+
57+
const event = try netsurf.keyboardEventCreate();
58+
try netsurf.keyboardEventInit(
59+
event,
60+
event_type,
61+
.{
62+
.bubbles = false,
63+
.cancelable = false,
64+
.key = options.key,
65+
.code = options.code,
66+
.alt = options.alt_key,
67+
.ctrl = options.ctrl_key,
68+
.meta = options.meta_key,
69+
.shift = options.shift_key,
70+
},
71+
);
72+
73+
return event;
74+
}
75+
};
76+
77+
const testing = @import("../../testing.zig");
78+
test "Browser: Events.Keyboard" {
79+
try testing.htmlRunner("events/keyboard.html");
80+
}

src/browser/netsurf.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub const DOMError = error{
388388

389389
const DOMException = c.dom_exception;
390390

391-
fn DOMErr(except: DOMException) DOMError!void {
391+
pub fn DOMErr(except: DOMException) DOMError!void {
392392
return switch (except) {
393393
c.DOM_NO_ERR => return,
394394
c.DOM_INDEX_SIZE_ERR => DOMError.IndexSize,

src/tests/events/keyboard.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script src="../testing.js"></script>
2+
<script id=default>
3+
const button = document.createElement("button");
4+
5+
document.addEventListener("keydown", (event) => {
6+
console.log(event.target);
7+
});
8+
9+
document.addEventListener("DOMContentLoaded", () => {
10+
document.body.appendChild(button);
11+
button.focus();
12+
});
13+
14+
testing.expectEqual(true, true);
15+
</script>
16+
17+
<script id=keyboardEvent>
18+
const keyboardEvent = new KeyboardEvent("a");
19+
console.lp(keyboardEvent.type);
20+
21+
testing.expectEqual(true, true);
22+
</script>

0 commit comments

Comments
 (0)