Focus and keyboard navigation
Let someone use a custom control without pointing at it. This pad becomes focusable, draws a visible focus border, and changes its number with the Left and Right keys.
using Xui.Core.Abstract.Events;
using Xui.Core.Canvas;
using Xui.Core.UI;
using Xui.Core.UI.Input;
public sealed class KeyboardPad : View
{
private int count;
public override bool Focusable => true;
public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
{
if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
Focus();
}
public override void OnKeyDown(ref KeyEventRef e)
{
if (e.Key == VirtualKey.Right)
count++;
else if (e.Key == VirtualKey.Left)
count--;
else if (e.Key == VirtualKey.Escape)
{
Blur();
e.Handled = true;
return;
}
else
return;
e.Handled = true;
InvalidateRender();
}
protected override void OnFocus() => InvalidateRender();
protected override void OnBlur() => InvalidateRender();
protected override void OnDeactivate()
{
Blur();
base.OnDeactivate();
}
protected override void RenderCore(IContext context)
{
context.Save();
try
{
context.SetFill(new Color(IsFocused ? 0xEEE2FAFFu : 0xFAF7FFFFu));
context.FillRect(Frame);
if (IsFocused)
{
context.SetStroke(new Color(0x8538CEFF));
context.LineWidth = 4;
context.StrokeRect(new(Frame.X + 4, Frame.Y + 4, Frame.Width - 8, Frame.Height - 8));
}
context.SetFill(new Color(0x492571FF));
context.SetFont(new Font(48, "Inter"));
context.TextAlign = TextAlign.Center;
context.TextBaseline = TextBaseline.Middle;
context.FillText(count.ToString(), (Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2));
context.SetFont(new Font(15, "Inter"));
context.FillText(IsFocused ? "Left / Right to change; Esc to leave" : "Click here or press Tab to focus", (Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2 + 72));
}
finally
{
context.Restore();
}
}
}
Click the live pad or press Tab after the embedded app has browser focus. A purple border indicates that the pad owns Xui keyboard focus. Left decreases the number, Right increases it, and Escape releases focus. The example requires a hardware keyboard; it does not request the phone's software keyboard.
Opt in, then show feedback
View.Focusable is false by default. Override it to participate in Tab traversal. Focus() requests focus through the nearest IFocus service and returns false if the view has no such service, as can happen before attachment.
IsFocused reports whether that service names this view. OnFocus and OnBlur invalidate the border's appearance. Setting Focusable alone does not draw a focus ring or define Enter/Space behavior; those remain the control's responsibility.
The pad's pointer handler requests focus. Other custom controls must also decide whether a pointer action should move focus. Avoid forcing focus on every frame or every hover: that steals typing from the field the user chose.
Keep traversal predictable
RootView handles Tab and Shift+Tab before forwarding ordinary keys to the focused view. Its traversal follows the view tree and considers views whose Focusable returns true. IFocus.Next() and Previous() use the same order and wrap; GetNext() and GetPrevious() inspect neighbors without wrapping by default.
Arrange the tree in a sensible reading order. Do not assume CSS tab indices or a platform accessibility tree controls this traversal. A view's focusability and keyboard behavior must be implemented explicitly, including in custom widgets.
Release and restore focus
Call Blur() when a focus-owning custom view deactivates. Core TextBox already does this. A popup flow should remember a valid previous focus target and restore it only if that target still belongs to the active tree; a stale reference to a removed page is not a useful destination.
Software-keyboard requests follow RequiresKeyboard, which defaults to true for ITextInputClient. A non-text control can accept hardware keys without opening a software keyboard. Continue with Keys and shortcuts to distinguish commands from text entry.