Table of Contents

GetJoystickState

(Information and examples taken from the DroidScript documentation)

Description

The GetJoystickState method gets the state of a button or axis of a HID Joystick or Gamepad (For example, an Xbox 360 controller connected to your device via an OTG cable).

app.GetJoystickState( joyNum, keyName );

GetJoystickState can get the state of multiple HID controllers currently connected to your device. Pass 0 to GetJoystickState for the first controller, 1 for the second, etc.

The state of a button will be 1 if it is currently pressed, or 0 if not currently pressed.

For an axis, the return value will be in the range -1.0 to 1.0. For example, “axis-0” (the X-axis) will return -1.0 (fully left) to 1.0 (fully right).


Example

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
 
  txt = app.CreateText( "", 1.0, -1, "MultiLine" );
  lay.AddChild( txt );
 
  app.AddLayout( lay );
 
  setInterval( ShowState, 200 );
}
 
function ShowState()
{
  var abtn = app.GetJoystickState( 0, "A" );
  var bbtn = app.GetJoystickState( 0, "B" );
  var xaxis = app.GetJoystickState( 0, "axis-0" );
  var yaxis = app.GetJoystickState( 0, "axis-1" );
 
  msg = "A:" + abtn + " B:" + bbtn + "\n";
  msg += " X-Axis:" + xaxis.toFixed(2) + " Y-Axis:" + yaxis.toFixed(2);
  txt.SetText( msg );
} 

Here is a list of the Joystick button names that can be passed to GetJoystickState:

Button name Description
“Up” DPad Up
“Down” DPad Down
“Left” DPad Left
“Right” DPad Right
“Center” DPad Center
“X”, “Y”, “Z” X, Y and Z Buttons
“A”, “B”, “C” A, B and C Buttons
“Start” Start Button
“ThumbLeft”, “ThumbRight” Left and Right Thumb Buttons

The axis names can be “axis-0” to “axis-9”. “axis-0” and “axis-1” will in most cases be the X and Y axes of the HID controller's main analog stick (i.e. the left stick on an Xbox controller). The remaining axes are for other analog sticks and trigger buttons on the HID controller.