← Back to Docs
  • index.html
  • Clayground
  • Clayground.GameController
  • GameController
  • Clayground 2026.14
  • GameController QML Type

    A unified game input system supporting keyboard, gamepad, and touchscreen. More...

    Import Statement: import Clayground.GameController

    Detailed Description

    GameController provides one controller abstraction in two layouts, picked with layout: nes, a stick and two action buttons, and full, two sticks, A/B/X/Y, shoulders, triggers and start/select. It supports multiple input sources that can be switched at runtime. Every source - keyboard, touchscreen, a physical gamepad, the Dojo's agent input - sets only the inputs of the layout; the others stay neutral, 0 or false.

    Each input has a name, listed by inputs: axisX, axisY, axisRightX, axisRightY, triggerL2 and triggerR2 are properties of that name; a button buttonA is the property buttonAPressed.

    Properties

    Which inputs the controller has: "nes" (the default) for axisX, axisY, buttonA and buttonB, or "full" for all fourteen inputs. Any other value warns and acts as nes. Changing it sets the inputs the new layout lacks to neutral; the keyboard and Dojo sources set the ones it gains from what they hold.

    The input names of the layout, in a fixed order: ["axisX", "axisY", "buttonA", "buttonB"] for nes, and for full axisX, axisY, axisRightX, axisRightY, buttonA, buttonB, buttonX, buttonY, buttonL1, buttonR1, triggerL2, triggerR2, buttonStart, buttonSelect.

    Horizontal directional axis value in range [-1, 1]. Negative values indicate left, positive values indicate right.

    Vertical directional axis value in range [-1, 1]. Negative values indicate down, positive values indicate up. With the full layout axisX and axisY are the left stick.

    Horizontal axis of the right stick in range [-1, 1], right positive. full layout only.

    Vertical axis of the right stick in range [-1, 1], up positive. full layout only.

    True when the primary action button (A) is pressed.

    True when the secondary action button (B) is pressed.

    True when button X is pressed. full layout only.

    True when button Y is pressed. full layout only.

    True when the left shoulder button is pressed. full layout only.

    True when the right shoulder button is pressed. full layout only.

    How far the left trigger is pulled, in range [0, 1]; a key or a touch reads 1 while held. full layout only.

    How far the right trigger is pulled, in range [0, 1]; a key or a touch reads 1 while held. full layout only.

    True when start is pressed. full layout only.

    True when select is pressed. full layout only.

    True once the Dojo inspector has sent agent input to this session.

    Shows a visual debug overlay displaying current controller state. Useful during development to verify input is working correctly.

    True if a physical gamepad is the active input source.

    True if the touchscreen virtual gamepad is the active input source.

    True if keyboard is the active input source.

    The physical gamepads connected to the machine, in the order they were connected; empty on Android builds, which read no pads yet. In the browser a pad is listed only once it was pressed with the page in front. Each pad has these properties:

    SDL brings a database of pad models, so a known pad reports the standard buttons whatever its raw button numbers; a pad it does not know takes a mapping line through the environment, SDL_GAMECONTROLLERCONFIG, or a file of them, SDL_GAMECONTROLLERCONFIG_FILE. Under Steam Input a game sees Steam's virtual pad, named after the pad it stands for, instead of the physical one.

    In the browser the pads come from navigator.getGamepads(). A pad the browser maps to its standard gamepad reports its standard buttons and axes; one it does not map is read by the same numbering, and its buttons may come up under other names. The browser names no make and no face button labels, so labels is empty there. Firefox on macOS misreads Nintendo's Switch pads - an untouched Pro Controller read with buttons pressed - and so does Firefox on Windows, except for the Pro Controller from Firefox 155 on; such a pad is listed with type unsupported and no buttons or axes, and drives no input. Every other pad, and every pad in Firefox on Linux, is read (clayground#474). A game in an iframe whose embedding page restricts gamepad in its Permissions-Policy needs allow="gamepad" on the iframe.

    The selected physical gamepad, one of gamepads, or null.

    The deviceId of the selected gamepad, or -1 if none is selected.

    Number of currently connected physical gamepads; it follows pads being plugged in and out.

    Methods

    Selects the physical gamepad at gamePadIdx of gamepads as the input source. The left stick and the d-pad drive axisX and axisY, the d-pad winning while pressed; with useAnalogAxis false only the d-pad does. The sticks have a round dead zone of 0.2 and read 0 to 1 beyond it.

    Buttons count by position: buttonA is the bottom face button (Xbox A, PlayStation Cross), buttonB the right one, buttonX the left one, buttonY the top one. The right stick drives axisRightX/Y, the shoulder buttons buttonL1/R1, the analog triggers triggerL2/R2, start and back buttonStart and buttonSelect. Guide, the stick clicks and paddles have no input in the layouts.

    When the selected pad is unplugged, every input goes neutral and gamepadSelected turns false. An index without a pad logs an error and changes nothing.

    Configures keyboard as the input source with specified key mappings. All parameters are Qt.Key values for the respective directions and buttons; buttonAKey presses B and buttonBKey presses A, as they always have.

    Alternatively upKey is one map from input name to key, which can set every input of the full layout: a button or a trigger takes one key, an axis takes [negative key, positive key], e.g. selectKeyboard({axisX: [Qt.Key_A, Qt.Key_D], buttonL1: Qt.Key_Q}). Inputs the layout lacks are ignored.

    Returns the current value of the input name, as inputs names it: a number for an axis or a trigger, a bool for a button.

    Enables the touchscreen virtual gamepad overlay for touch input. It shows a stick and a pad for every input of the layout; see TouchscreenGamepad.