Input
The engine translates input into DOM interactions. It fires the same events as a browser, and performs the default actions of the page, such as following a link, focusing a text field, toggling a checkbox, or submitting a form.
All positions are in CSS pixels relative to the viewport.
Pointer
| Member | Description |
|---|---|
MovePointer(x, y) |
Moves the pointer without waiting. Moves are coalesced, which is what a UI should use. |
MovePointerAsync(x, y) |
Moves the pointer and waits until the hover state has been updated. |
PointerDownAsync(x, y, modifiers) |
Presses the primary button. |
PointerDownAsync(x, y, clickCount, modifiers) |
Presses the primary button for the given time in a row, e.g., 2 for a double click. |
PointerUpAsync(x, y, modifiers) |
Releases the primary button. |
ClickAsync(x, y, modifiers) |
Presses and releases the primary button. |
HoveredLink, Cursor |
The link below the pointer and the BrowserCursor a host should display. |
Awaiting a click also awaits the navigation it caused:
await engine.ClickAsync(40, 390);
Console.WriteLine(engine.Url);
Holding Control while clicking a link requests a new window, as does a link with target="_blank". See Multiple Windows.
await engine.ClickAsync(40, 390, InputModifiers.Control);
Selecting Text
With BrowserOptions.IsTextSelectionEnabled, the text of a page can be selected as in a browser. It is off by default, as the layouts then also record where their text comes from.
- Pressing the button and moving the pointer with it held selects the text in between; with Shift, a press extends the selection.
- A second press in a row (
clickCount2) selects a word, a third one the paragraph. - Control+A (or Cmd+A) selects all text of the page, unless a text field has the focus.
- Over text that can be selected,
CursorisBrowserCursor.Text, unless the page asks for another cursor. - A page can cancel
mousedownorselectstart, anduser-select: nonekeeps text from being selected. Scripts see the selection throughgetSelection().
CopySelectionAsync fires copy and returns the selected text for the clipboard, or an empty text if nothing is selected or the page canceled the event:
var engine = new BrowserEngine(new BrowserOptions { IsTextSelectionEnabled = true });
await engine.LoadHtmlAsync("<p>Hello world</p>");
await engine.PointerDownAsync(30, 20, 2);
await engine.PointerUpAsync(30, 20);
Console.WriteLine(await engine.CopySelectionAsync());
Keyboard
Keys are named by their DOM key value, e.g., "a", "Enter", "Backspace", "Tab", or "ArrowDown". Keys and text are separate on purpose, just like in most UI frameworks:
KeyDownAsyncandKeyUpAsyncfire the keyboard events and perform the actions of keys, e.g., Tab moving the focus, or Enter submitting a form.TextInputAsyncenters text into the focused text field. It works for whole strings, too.
await engine.ClickAsync(100, 200);
await engine.TextInputAsync("angle brackets");
await engine.KeyDownAsync("Enter");
await engine.KeyUpAsync("Enter");
Selects
A select showing several options (multiple or size) is a list box:
- A click selects a row. With
multiple, Ctrl (or Cmd) toggles it and Shift selects the rows from the last one clicked. - The up and down keys move the selection, with Shift (and
multiple) extending it. - The wheel scrolls it before the page.
Any other select is a dropdown. Pressing it, or Enter, Space or Alt+Down while it has the focus, opens its list:
IsSelectPopupOpentells if the list is open. Frames fromPrepareFrameAsyncshow it on top of the page (the image ofPaintAsyncdoes not), so a host has nothing to do for it. It stays within the viewport, opening above the select without room below.- While it is open, the list takes all keys: they move its highlight (skipping what is disabled), Enter, Space or Tab choose the highlighted row, and Escape closes it without a change. A click on a row chooses it.
- A press elsewhere or scrolling the page closes it. So do the select losing the focus or going away, and a change of the viewport.
- While it is closed, the up and down keys, Home, End, Page Up, Page Down and typing change the value right away.
- The list uses the color scheme of the select: dark if its
color-scheme(or else thecolor-schememeta element of the page) supports dark, and doesn't also support light unlessColorSchemeisdark.ColorSchemealone doesn't make it dark, as in other browsers. Colors of the select and of its options win over those of the scheme.
Choosing a row fires input and change if the value changed, then click.
Scrolling
ScrollAsync scrolls by a distance: what is below the pointer, if that is a scroll container, or else the page. The returned task completes once the scrolling arrived.
await engine.MovePointerAsync(400, 300);
await engine.ScrollAsync(0, 480);
Console.WriteLine(engine.ScrollPosition);
9e2ca98