Skip to content

gamepad

stable

Virtual gamepad state management for games and simulations. Track button and axis values, apply dead-zones, and query pressed inputs through a GamepadState handle.

use plugin gamepad::{GamepadState.new, set_button, set_axis, …}
11 functions Game
/ filter jk navigate Esc clear
Functions (11)
  1. GamepadState.new Creates a new empty gamepad state.
  2. set_button setbutton records whether a named button is currently pressed.
  3. set_axis setaxis stores a float axis value (typically -1.0 to 1.0).
  4. axis_value_with_deadzone Returns the axis value, but returns 0.0 if its absolute value is below the dead-zone threshold set via setdeadzone.
  5. set_dead_zone Sets or reads the dead-zone threshold used by axisvaluewith_deadzone.
  6. buttons buttons returns a table of {name: bool} for all registered buttons.
  7. pressed_buttons Returns a list of names for all buttons that are currently pressed (value true).
  8. reset Clears all button and axis state, returning the gamepad to an empty initial state.
  9. button_name Returns the standard name for a button or axis by 0-based index.
  10. apply_deadzone Stateless utility: returns value if its absolute value exceeds threshold, otherwise returns 0.0.
  11. total_button_count Returns the number of standard button or axis names defined by the plugin.

Creates a new empty gamepad state.

Creates a new empty gamepad state. Buttons and axes are populated by calling set_button and set_axis.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_dead_zone(0.1)

setbutton records whether a named button is currently pressed.

set_button records whether a named button is currently pressed. is_pressed reads it back. Button names are arbitrary strings; the standard names are available via button_name(index).

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_button("south", true)
pad.set_button("east", false)

print(pad.is_pressed("south"))  // true
print(pad.is_pressed("east"))   // false

setaxis stores a float axis value (typically -1.0 to 1.0).

set_axis stores a float axis value (typically -1.0 to 1.0). axis_value reads the raw stored value without any dead-zone filtering.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_axis("left_stick_x", 0.8)
pad.set_axis("left_stick_y", -0.3)

print(pad.axis_value("left_stick_x"))  // 0.8

Returns the axis value, but returns 0.0 if its absolute value is below the dead-zone threshold set via setdeadzone.

Returns the axis value, but returns 0.0 if its absolute value is below the dead-zone threshold set via set_dead_zone. Use this for movement inputs to avoid drift from a centred stick.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_dead_zone(0.15)
pad.set_axis("left_stick_x", 0.05)  // below threshold
pad.set_axis("left_stick_y", 0.6)   // above threshold

print(pad.axis_value_with_deadzone("left_stick_x"))  // 0.0
print(pad.axis_value_with_deadzone("left_stick_y"))  // 0.6

Sets or reads the dead-zone threshold used by axisvaluewith_deadzone.

Sets or reads the dead-zone threshold used by axis_value_with_deadzone. The threshold is stored as an absolute value.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_dead_zone(0.12)
print(pad.dead_zone())  // 0.12

buttons returns a table of {name: bool} for all registered buttons.

buttons returns a table of {name: bool} for all registered buttons. axes returns {name: float} for all registered axes. Useful for serialising input state.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_button("south", true)
pad.set_axis("left_stick_x", 0.5)

print(pad.buttons())
print(pad.axes())

Returns a list of names for all buttons that are currently pressed (value true).

Returns a list of names for all buttons that are currently pressed (value true).

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_button("south", true)
pad.set_button("north", true)
pad.set_button("east", false)

print(pad.pressed_buttons())  // ["south", "north"]

Clears all button and axis state, returning the gamepad to an empty initial state.

Clears all button and axis state, returning the gamepad to an empty initial state.

use plugin gamepad::{GamepadState}

let pad = GamepadState.new()
pad.set_button("south", true)
pad.reset()
print(pad.is_pressed("south"))  // false

Returns the standard name for a button or axis by 0-based index.

Returns the standard name for a button or axis by 0-based index. Standard button names include south, east, north, west, left_trigger, right_trigger, start, select, and the d-pad buttons. Standard axis names include the two stick axes and triggers.

use plugin gamepad::{button_name, axis_name, total_button_count, total_axis_count}

let count = total_button_count()
let i = 0
while i < count {
  print(button_name(i))
  let i = i + 1
}

print(axis_name(0))  // left_stick_x
print(axis_name(1))  // left_stick_y

Stateless utility: returns value if its absolute value exceeds threshold, otherwise returns 0.0.

Stateless utility: returns value if its absolute value exceeds threshold, otherwise returns 0.0. Use when you have a raw axis value without a GamepadState instance.

use plugin gamepad::{apply_deadzone}

print(apply_deadzone(0.05, 0.1))   // 0.0
print(apply_deadzone(0.5, 0.1))    // 0.5
print(apply_deadzone(-0.8, 0.1))   // -0.8

Returns the number of standard button or axis names defined by the plugin.

Returns the number of standard button or axis names defined by the plugin.

use plugin gamepad::{total_button_count, total_axis_count}

print(total_button_count())  // 17
print(total_axis_count())    // 6
enespt-br