Osy#betaRun it on Osyrin: osyrin.com

Reference / UI

Device.HasKeyboard — whether to show key hints

Device.HasKeyboard — a bool, in a render block or a client action

`Device.HasKeyboard` is true when the page is used with keys worth hinting at: a mouse or a trackpad (a fine pointer that can hover), or once a physical key has been pressed on the page — so a tablet with a keyboard attached starts showing hints the first time it is used. False on a touch-only phone, and on the server.

stable1 example compiled by CIuikeysresponsive

Summary#

Show which keys move through a list where there are keys to press:

[Route("/inbox")] [AllowAnonymous]
component InboxPage() {
  render {
    Stack(gap: 2) {
      Text("Your messages");
      if (Device.HasKeyboard) { Text("J and K move between messages"); }
    }
  }
}

Signature#

Device.HasKeyboard   // bool — a mouse or a trackpad, or a physical key pressed on this page

Description#

How does it decide?#

No browser says whether a keyboard is attached, so the answer is worked out from two signs:

  • The pointer is a mouse or a trackpad. A precise pointer that can hover almost always comes with keys — a laptop, a desk. This is known the moment the page renders.
  • A physical key was pressed on this page. It settles the case the pointer cannot: a tablet with a keyboard case. From the first such key the answer is true for as long as the page is open. Typing on an on-screen keyboard does not count — a key pressed into a text field, or as part of an input method's composition, could have come from one. A key no on-screen keyboard has (Tab, Escape, the arrows, Home, End, Page Up/Down, F1–F12), a key pressed with Ctrl, Alt or Cmd held, or any key pressed outside a field does count.

Device.HasKeyboard or Viewport.AtLeast?#

You are decidingAsk
whether to show key hints, a shortcut bar, a "press / to search" lineDevice.HasKeyboard
whether the window is wide enough for a layoutViewport.AtLeast(Laptop) (Viewport.AtLeast and Layout.AtLeast — the window, or the room this component has)

A wide window is not a keyboard — a tablet in landscape is 1180px wide — and a narrow one may have one.

Where can I use it?#

In render, where it follows the answer live (the hint appears the moment the first key is pressed), and in a client action, where it is read once when the action runs. It is a bool, so it combines like any other: if (Device.HasKeyboard && Layout.AtLeast(Tablet)) { … }.

What does the server answer?#

false — the touch-first answer, as Viewport.AtLeast answers the narrow one. A server-rendered page paints no key hints, and the browser adds them on its first render where they apply. A server function that reads it also gets false: a server knows nothing about the hardware a page is used on.

Can I name something Device myself?#

Yes — a field, a parameter or a local called Device is yours, and Device.HasKeyboard then reads it, as a local hides a type of the same name in C#.

See also#

Related

Viewport.AtLeast and Layout.AtLeast — the window, or the room this component has

Two questions with one shape. `Layout.AtLeast(Tablet)` asks whether THIS component's container is at least the…

keys

`keys:` declares that an element owns a set of keys: it becomes focusable, those keys stop scrolling the page, and…

component

The one archetype for all UI: a bounded reactive unit — typed props, reactive members (fields, `live`…