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
<div class="slot" onpointerdown={e => Grab(e)} onpointerup={e => Drop(e)}></div>
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
| Group | Events |
|---|---|
| Pointer | pointerdown, pointermove, pointerup, pointercancel, pointerover, pointerout, pointerenter, pointerleave, gotpointercapture, lostpointercapture |
| Mouse | click, dblclick, contextmenu, auxclick, wheel, and the mouse events |
| Keyboard | keydown, keyup |
| Focus | focus, blur, focusin, focusout |
| Text | beforeinput, input, change, and the composition events |
| Gamepad | cancel (B or Circle) |
PointerEvent
| Member | Meaning |
|---|---|
int PointerId | Which pointer. The mouse is 1 |
PointerType PointerType | Mouse, touch or pen |
bool IsPrimary | Whether it is the main pointer of its type |
float ClientX, ClientY | The position in the view, in CSS pixels |
int Button | The button that changed: 0 main, 1 middle, 2 secondary |
int Buttons | The buttons held, as bits |
int Detail | The click count |
KeyModifiers Modifiers, ShiftKey… | The modifier keys held |
Element RelatedTarget | For over, out, enter and leave: the other element |
CurrentTarget, Target | The element whose listener runs; the element the event is for |
PreventDefault(), StopPropagation() | As in a browser |
KeyboardEvent and WheelEvent
| Member | Meaning |
|---|---|
KeyboardEvent.Key | The key's value: "a", "Enter", "ArrowLeft" |
KeyboardEvent.Code | The physical key: "KeyA" |
KeyboardEvent.Modifiers, Repeat | Modifier keys; whether it is an auto-repeat |
WheelEvent.DeltaX, DeltaY | The scroll amount |
WheelEvent.ClientX, ClientY, Modifiers | Where, and with which keys |
Pointer capture and focus
Extension methods on Element:
| Member | Meaning |
|---|---|
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
| Member | Meaning |
|---|---|
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), KeyUp | Keys |
TextInput(string | char | ReadOnlySpan<char>) | Typed text for the focused control |
CompositionUpdate, CompositionCommit | IME 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 ArrowKeysNavigate | Whether 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