API reference

Events and input

Events are dispatched as in a browser: capture, target, bubble, default actions. The event classes are in DevCore.UiEngine.Input.

Listening

In a component
<div class="slot" onpointerdown={e => Grab(e)} onpointerup={e => Drop(e)}></div>
From game code
using DevCore.UiEngine.Dom.Events;
using DevCore.UiEngine.Input;

button.AddEventListener(EventType.Click, e => StartGame());

Event objects are pooled: an event is valid only while its handler runs. Copy the values you need; do not keep the object.

Event types

GroupEvents
Pointerpointerdown, pointermove, pointerup, pointercancel, pointerover, pointerout, pointerenter, pointerleave, gotpointercapture, lostpointercapture
Mouseclick, dblclick, contextmenu, auxclick, wheel, and the mouse events
Keyboardkeydown, keyup
Focusfocus, blur, focusin, focusout
Textbeforeinput, input, change, and the composition events
Gamepadcancel (B or Circle)

PointerEvent

MemberMeaning
int PointerIdWhich pointer. The mouse is 1
PointerType PointerTypeMouse, touch or pen
bool IsPrimaryWhether it is the main pointer of its type
float ClientX, ClientYThe position in the view, in CSS pixels
int ButtonThe button that changed: 0 main, 1 middle, 2 secondary
int ButtonsThe buttons held, as bits
int DetailThe click count
KeyModifiers Modifiers, ShiftKey…The modifier keys held
Element RelatedTargetFor over, out, enter and leave: the other element
CurrentTarget, TargetThe element whose listener runs; the element the event is for
PreventDefault(), StopPropagation()As in a browser

KeyboardEvent and WheelEvent

MemberMeaning
KeyboardEvent.KeyThe key's value: "a", "Enter", "ArrowLeft"
KeyboardEvent.CodeThe physical key: "KeyA"
KeyboardEvent.Modifiers, RepeatModifier keys; whether it is an auto-repeat
WheelEvent.DeltaX, DeltaYThe scroll amount
WheelEvent.ClientX, ClientY, ModifiersWhere, and with which keys

Pointer capture and focus

Extension methods on Element:

MemberMeaning
bool SetPointerCapture(int pointerId)Sends that pointer's events to this element. Only a pointer with a button down can be captured
void ReleasePointerCapture(int pointerId)Ends it. It also ends by itself after the last button's pointerup, and on cancel
bool HasPointerCapture(int pointerId)Whether this element holds the pointer
bool Focus(), void Blur()Moves focus to the element; removes it

A captured element gets the events even if it has pointer-events: none.

Input from code

view.View.Input is the page's DocumentInput. Use it for tests, tutorials, replays and custom devices.

var input = view.View.Input;

input.PointerMove(PointerInput.Mouse(120, 80));
input.PointerDown(PointerInput.Mouse(120, 80));
input.PointerUp(PointerInput.Mouse(120, 80));          // a click at (120, 80)

input.KeyDown("Enter");
input.KeyUp("Enter");
input.TextInput("hello");

input.Navigate(NavigationDirection.Down);              // as the d-pad does
input.Confirm();                                       // as A does
input.Cancel();                                        // as B does
MemberMeaning
PointerMove, PointerDown, PointerUp(in PointerInput input)Pointer input at a position
PointerCancel(int pointerId), PointerExit(int pointerId)The pointer was cancelled; it left the view
Wheel(in PointerInput input, float deltaX, float deltaY)A wheel turn
KeyDown(string key, string code = null, KeyModifiers modifiers = None, bool repeat = false), KeyUpKeys
TextInput(string | char | ReadOnlySpan<char>)Typed text for the focused control
CompositionUpdate, CompositionCommitIME composition
bool Focus(Element element), Blur(), bool MoveFocus(bool forward = true)Focus, and Tab or Shift+Tab
bool Navigate(NavigationDirection direction)Spatial navigation: Up, Right, Down, Left
Confirm(), bool Cancel()Activate the focused element; send cancel
bool ScrollBy(float deltaX, float deltaY)Scroll what is under focus or the pointer
Element HoveredElement(int pointerId = 1)What a pointer is over
bool ArrowKeysNavigateWhether arrow keys move focus like the d-pad

PointerInput.Mouse(float x, float y, int button = 0, double time = 0, KeyModifiers modifiers = None) makes the input of a mouse; new PointerInput(id, PointerType.Touch, x, y) that of another pointer.

Did the UI take the mouse?

using DevCore.UiEngine.Unity.Hosting;

if (UiSystem.Input.TryGetMouseView(out UIView view, out Vector2 position))
    return;     // the pointer is over a page: do not shoot, do not move the camera