Configuration & querying - s76/libgdx GitHub Wiki

Sometimes it is necessary to know which input devices are supported. It is also often the case that your game does not need the full range of input devices supported, e.g. you might not need the accelerometer or compass. It is good practice to disable those input devices in that case to preserve battery on Android. The following sections will show you how to perform these actions.

Disabling Accelerometer & Compass (Android only)

Configuring input devices only makes sense on Android at the moment. The AndroidApplicationConfiguration class has a couple of public fields you can set before you hand it of to the AndroidApplication.initialize() method.

Assuming our game doesn't need the accelerometer and compass, we can disable this input devices as follows:

public class MyGameActivity extends AndroidApplication {
   @Override
   public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
      config.useAccelerometer = false;
      config.useCompass = false;
      initialize(new MyGame(), config);
   }
}

Both the accelerometer and the compass are enabled by default. The above code disables them and will thus preserve some precious battery.

Querying Available Input Devices

To check whether a specific input device is available on the platform the application currently runs, you can use the Input.isPeripheralAvailable() method.

   boolean hardwareKeyboard = Gdx.input.isPeripheralAvailable(Peripheral.HardwareKeyboard);
   boolean multiTouch = Gdx.input.isPeripheralAvailable(Peripheral.MultitouchScreen);

Please refer to the Peripheral enumeration to see the rest of the available constants.

Note that only a few Android devices have a hardware keyboard. Even if the keyboard is physically present, the user might not have slid it out. The method shown above will return false in this case.