Skip to content

useFocusZone

Lucy.useFocusZone makes a group of controls a zone the gamepad steps into with a key. Use it for controls that must not hold the selection while the player plays (a HUD card, a column of buttons beside the play area): while anything is selected, the left stick moves the selection instead of the character, so nothing is selected until the player asks.

With nothing selected, a key in keys steps in and focus lands on the zone’s first or last control. Inside, the D-pad and keys stay on the zone’s controls and A presses. B (or Backspace) steps back out and leaves nothing selected, so the stick walks again. Focus going anywhere else (a click, a menu opening, the zone hiding) leaves the zone too. The mouse and touch are not affected.

Usage

local zone = Lucy.useFocusZone(app?, root, options)
ArgumentTypeDefaultWhat it is
appLucy.App?Lucy.App.current()The app managing input and focus
rootMaybeRef<GuiObject?>requiredThe frame holding the zone’s controls
optionstablerequiredSee below

Options

OptionTypeDefaultWhat it does
namestringrequiredUnique in the app; the keys are the action set zone:<name>
keys{ [Enum.KeyCode]: "first" | "last" | (list) -> GuiObject? }{}The keys that step in, and where each lands (reading order)
enabledMaybeRef<boolean>trueWhen stepping in is allowed (the zone is shown, nothing else has the key)
prioritynumberthe actions defaultThe action set’s priority
trapbooleantruefalse lets the D-pad move on from the zone’s edges to the controls around it (which leaves the zone), for a zone that sits among other HUD buttons. B still steps out

The keys work only while enabled is true, nothing is selected and no modal is open. The rest of the time they reach whatever else binds them.

Returns

FieldTypeWhat it is
inside() -> booleanTracked: focus is in the zone
enter(where?) -> ()Steps in ("first", "last" or a function), as a key does
leave() -> ()Steps out

Example: a player card at the top of the HUD

D-pad up steps onto the card’s top button; D-pad up and down then move between its rows, A presses, B steps out:

local card = PlayerCard.new({ Parent = scene.gui })

Lucy.useFocusZone(app, card, {
    name = "hudCard",
    keys = { [Enum.KeyCode.DPadUp] = "first" },
    enabled = function()
        return not onStage() and not showHasTheDPad()
    end,
})

Example: cheers beside the stage

D-pad up steps in at the bottom cheer, D-pad down at the top one, and a hint shows while the player is in them:

local zone = Lucy.useFocusZone(app, cheers, {
    name = "cheers",
    keys = { [Enum.KeyCode.DPadUp] = "last", [Enum.KeyCode.DPadDown] = "first" },
    enabled = cheersShown,
})

Lucy.Text.new({
    text = "B to leave the cheers",
    visible = zone.inside,
})

Zone, tile or scope?

You wantUse
A list row with several controls to be one stopItem with focus = "tile"
A group nothing selects until a key steps inuseFocusZone
A dialog or panel that keeps focus while it is openA Dialog or Sheet, or useFocusScope

How input and focus are routed is in Architecture.