pesde goatsoft / lucy

Kbd

Shows which key to press, for the input the player is using right now: the key on a keyboard, the button on a controller, and nothing on a phone. It changes the moment the player picks up a controller or goes back to the mouse.

Kbd exampleKbd example
local function hint(key, button, label)
    return Lucy.Stack.new({
        direction = "horizontal",
        gap = "Space2",
        align = "center",
        children = {
            Lucy.Kbd.new({ keyboard = key, gamepad = button }),
            Lucy.Text.new({ text = label, variant = "muted", autoWidth = true }),
        },
    })
end

Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space6",
    align = "center",
    children = {
        hint(Enum.KeyCode.E, Enum.KeyCode.ButtonX, "Dance"),
        hint(Enum.KeyCode.Q, Enum.KeyCode.ButtonY, "Queue"),
        hint(Enum.KeyCode.B, Enum.KeyCode.DPadRight, "Emote Shop"),
    },
    Parent = parent,
})

Controller buttons show the engine's own picture for the player's controller, so PlayStation players see PlayStation buttons. The face buttons are in their controller's colours: on Xbox A is green, B red, X blue and Y yellow; on PlayStation the cross is blue, the circle red, the square pink and the triangle green. Other buttons, and the mono variant, use the text colour. To put a key on a button, use the Button's hint prop.

Kbd only shows the key. Binding the key to an action is up to your game.

Props

PropTypeDefaultState keyWhat it does
keyboardKeyCode or stringyesShown for keyboard and mouse
gamepadKeyCode or stringyesShown for a controller, as its picture when it has one
touchstringyesShown on touch, such as "Tap". Without it, nothing shows on touch. A device with no keyboard (a phone or tablet on its own) always counts as touch, even after a key press, unless a controller is in use. One with a keyboard paired shows its keys
actionstringyesAn action's name: its keys stand in for keyboard and gamepad, and follow a rebind. Define the action first
variantstring"color"yescolor gives face buttons their controller colours; mono draws every button in the text colour
shapestring"default"yes"default" for a rounded key cap, or "circle" for a circular key cap
sizenumber24Circle diameter in pixels when shape = "circle"
visiblebooleantrueyesShows or hides it

It also takes the props every component takes.

It hides itself when it has no key for the current input. Text can stand in for any key, such as gamepad = "Hold A".

Keyboard keys are named for the player's keyboard layout. Keys with no letter get short names: Esc, Enter, Space, Tab, Shift, Ctrl, Alt, Up, Down, Left and Right.

Its parts are on the handle: kbd.text is the label and kbd.glyph the controller picture. Lucy.Kbd.label(key) gives a key's text, and Lucy.Kbd.glyph(keyCode) its controller picture. Lucy.Kbd.platform() says whether the player's controller is "xbox" or "playstation" style.

Examples

A hint beside a line of text:

Kbd exampleKbd example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space2",
    align = "center",
    children = {
        Lucy.Kbd.new({ keyboard = Enum.KeyCode.Backspace, gamepad = Enum.KeyCode.ButtonB }),
        Lucy.Text.new({ text = "Close", variant = "muted", autoWidth = true }),
    },
    Parent = footer,
})

A key that changes with game state:

Kbd exampleKbd example
local key = Lucy.ref(Enum.KeyCode.One)

Lucy.Kbd.new({ keyboard = key, Parent = emoteSlot })
key(Enum.KeyCode.Two)

A circular key cap (used inside Progress rings and world Prompt hints):

Kbd exampleKbd example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space2",
    align = "center",
    children = {
        Lucy.Kbd.new({ keyboard = Enum.KeyCode.E, shape = "circle" }),
        Lucy.Text.new({ text = "Play a set", variant = "muted", autoWidth = true }),
    },
    Parent = parent,
})

Preview

In Studio, from the command bar: Lucy.Preview.show("Kbd"). Its input control switches between keyboard, gamepad and touch.