LostBeard / SpawnDev.BlazorJS

Full Blazor WebAssembly and Javascript Interop with multithreading via WebWorkers
https://blazorjs.spawndev.com
MIT License
78 stars 6 forks source link

Added keyboard events to Window #29

Closed RonPeters closed 2 months ago

RonPeters commented 2 months ago

Adds keyboard events and fixes #28

LostBeard commented 2 months ago

Nice job! Thank you.

Small change to KeyboardEvent. KeyboardEvent inherits from UIEvent.

public class KeyboardEvent : Event

To

public class KeyboardEvent : UIEvent

Looks like I also need to finish the Element interface which also uses KeyboardEvent for keydown and keyup events.

A minor quibble about adding these events to Window. Window does not actually have these events. They are bubbled up from the Element the event happened on, the event.target. However handling these events on the Window is useful.

An alternative is to use AddEventListener and RemoveEventListener when listening for bubbled events. For instance, after the KeyboardEvent class is added, you can use the code like below to listen for those bubbled events.

void AddWindowEventHandlers(Window window)
{
    window.AddEventListener<KeyboardEvent>("keydown", Window_OnKeyDown);
    window.AddEventListener<KeyboardEvent>("keyup", Window_OnKeyUp);
}
void RemoveWindowEventHandlers(Window window)
{
    window.RemoveEventListener<KeyboardEvent>("keydown", Window_OnKeyDown);
    window.RemoveEventListener<KeyboardEvent>("keyup", Window_OnKeyUp);
}
void Window_OnKeyDown(KeyboardEvent keyboardEvent)
{
    Console.WriteLine("Window_OnKeyDown");
}
void Window_OnKeyUp(KeyboardEvent keyboardEvent)
{
    Console.WriteLine("Window_OnKeyUp");
}
RonPeters commented 2 months ago

Yes, that makes sense with the event bubbling. Thanks for the merge!

RonPeters commented 2 months ago

I see you included my changes to Window. Was that intentional, or did you intend to leave them out per the event bubbling issue above?

LostBeard commented 2 months ago

Window is sort of a special case as it it a top level object, and while I don't want event handlers (JSEventCallbacks) for all bubbleable events I do see the usefulness of having some of the more common ones implemented. Besides, other than me, you have contributed more than anyone else to this project.