mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
initial keyboard event
This commit is contained in:
@@ -33,11 +33,20 @@ const AbortSignal = @import("../html/AbortController.zig").AbortSignal;
|
||||
const CustomEvent = @import("custom_event.zig").CustomEvent;
|
||||
const ProgressEvent = @import("../xhr/progress_event.zig").ProgressEvent;
|
||||
const MouseEvent = @import("mouse_event.zig").MouseEvent;
|
||||
const KeyboardEvent = @import("keyboard_event.zig").KeyboardEvent;
|
||||
const ErrorEvent = @import("../html/error_event.zig").ErrorEvent;
|
||||
const MessageEvent = @import("../dom/MessageChannel.zig").MessageEvent;
|
||||
|
||||
// Event interfaces
|
||||
pub const Interfaces = .{ Event, CustomEvent, ProgressEvent, MouseEvent, ErrorEvent, MessageEvent };
|
||||
pub const Interfaces = .{
|
||||
Event,
|
||||
CustomEvent,
|
||||
ProgressEvent,
|
||||
MouseEvent,
|
||||
KeyboardEvent,
|
||||
ErrorEvent,
|
||||
MessageEvent,
|
||||
};
|
||||
|
||||
pub const Union = generate.Union(Interfaces);
|
||||
|
||||
|
||||
80
src/browser/events/keyboard_event.zig
Normal file
80
src/browser/events/keyboard_event.zig
Normal file
@@ -0,0 +1,80 @@
|
||||
const std = @import("std");
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
const netsurf = @import("../netsurf.zig");
|
||||
const Event = @import("event.zig").Event;
|
||||
const JsObject = @import("../env.zig").JsObject;
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("dom/dom.h");
|
||||
@cInclude("core/pi.h");
|
||||
@cInclude("dom/bindings/hubbub/parser.h");
|
||||
@cInclude("events/event_target.h");
|
||||
@cInclude("events/event.h");
|
||||
@cInclude("events/mouse_event.h");
|
||||
@cInclude("events/keyboard_event.h");
|
||||
@cInclude("utils/validate.h");
|
||||
@cInclude("html/html_element.h");
|
||||
@cInclude("html/html_document.h");
|
||||
});
|
||||
|
||||
// TODO: We currently don't have a UIEvent interface so we skip it in the prototype chain.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent
|
||||
const UIEvent = Event;
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
|
||||
pub const KeyboardEvent = struct {
|
||||
pub const Self = netsurf.KeyboardEvent;
|
||||
pub const prototype = *UIEvent;
|
||||
|
||||
pub const KeyLocationCode = enum(u16) {
|
||||
standard = 0x00,
|
||||
left = 0x01,
|
||||
right = 0x02,
|
||||
numpad = 0x03,
|
||||
mobile = 0x04, // Non-standard, deprecated.
|
||||
joystick = 0x05, // Non-standard, deprecated.
|
||||
};
|
||||
|
||||
pub const ConstructorOptions = struct {
|
||||
key: []const u8 = "",
|
||||
code: []const u8 = "",
|
||||
location: KeyLocationCode = .standard,
|
||||
char_code: u32 = 0,
|
||||
key_code: u32 = 0,
|
||||
which: u32 = 0,
|
||||
repeat: bool = false,
|
||||
ctrl_key: bool = false,
|
||||
shift_key: bool = false,
|
||||
alt_key: bool = false,
|
||||
meta_key: bool = false,
|
||||
is_composing: bool = false,
|
||||
};
|
||||
|
||||
pub fn constructor(event_type: []const u8, maybe_options: ?ConstructorOptions) !*netsurf.KeyboardEvent {
|
||||
const options = maybe_options orelse ConstructorOptions{};
|
||||
|
||||
const event = try netsurf.keyboardEventCreate();
|
||||
try netsurf.keyboardEventInit(
|
||||
event,
|
||||
event_type,
|
||||
.{
|
||||
.bubbles = false,
|
||||
.cancelable = false,
|
||||
.key = options.key,
|
||||
.code = options.code,
|
||||
.alt = options.alt_key,
|
||||
.ctrl = options.ctrl_key,
|
||||
.meta = options.meta_key,
|
||||
.shift = options.shift_key,
|
||||
},
|
||||
);
|
||||
|
||||
return event;
|
||||
}
|
||||
};
|
||||
|
||||
const testing = @import("../../testing.zig");
|
||||
test "Browser: Events.Keyboard" {
|
||||
try testing.htmlRunner("events/keyboard.html");
|
||||
}
|
||||
@@ -388,7 +388,7 @@ pub const DOMError = error{
|
||||
|
||||
const DOMException = c.dom_exception;
|
||||
|
||||
fn DOMErr(except: DOMException) DOMError!void {
|
||||
pub fn DOMErr(except: DOMException) DOMError!void {
|
||||
return switch (except) {
|
||||
c.DOM_NO_ERR => return,
|
||||
c.DOM_INDEX_SIZE_ERR => DOMError.IndexSize,
|
||||
|
||||
22
src/tests/events/keyboard.html
Normal file
22
src/tests/events/keyboard.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<script src="../testing.js"></script>
|
||||
<script id=default>
|
||||
const button = document.createElement("button");
|
||||
|
||||
document.addEventListener("keydown", (event) => {
|
||||
console.log(event.target);
|
||||
});
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.body.appendChild(button);
|
||||
button.focus();
|
||||
});
|
||||
|
||||
testing.expectEqual(true, true);
|
||||
</script>
|
||||
|
||||
<script id=keyboardEvent>
|
||||
const keyboardEvent = new KeyboardEvent("a");
|
||||
console.lp(keyboardEvent.type);
|
||||
|
||||
testing.expectEqual(true, true);
|
||||
</script>
|
||||
Reference in New Issue
Block a user