DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


built_in:get_joystick_state

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

built_in:get_joystick_state [2015/11/02 09:44] (current)
octazid created
Line 1: Line 1:
 +=====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).
 +
 +<​code>​app.GetJoystickState( joyNum, keyName );</​code>​
 +
 +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====
 +
 +<code javascript>​
 +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 );
 +
 +</​code>​
 +
 +----
 +
 +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.
built_in/get_joystick_state.txt ยท Last modified: 2015/11/02 09:44 by octazid