Using the Gear VR Controller

The Carmel Developer Preview exposes the Gear VR touch panel as a gamepad with the id “Gear VR”. You can use window.navigator.getGamepads() to query for the connected Gear VR gamepad as well as any other gamepads, such as a Bluetooth gamepad.

var state = {
  lastButtons: {},
  lastAxes: {}
};
Array.prototype.forEach.call(navigator.getGamepads(), function (activePad, padIndex) {
  if (activePad.connected) {
    if (activePad.id.includes("Gear VR")) {
      // Process buttons and axes for the Gear VR touch panel
      activePad.buttons.forEach(function (gamepadButton, buttonIndex) {
        if (buttonIndex === 0 && gamepadButton.pressed && !lastButtons[buttonIndex]) {
          // Handle tap
        }
        state.lastButtons[buttonIndex] = gamepadButton.pressed;
      });

      activePad.axes.forEach(function (axisValue, axisIndex) {
        if (axisIndex === 0 && axisValue < 0 && lastAxes[axisIndex] >= 0) {
          // Handle swipe right
        } else if (axisIndex === 0 && axisValue > 0 && lastAxes[axisIndex] <= 0) {
          // Handle swipe left
        } else if (axisIndex === 1 && axisValue < 0 && lastAxes[axisIndex] >= 0) {
          // Handle swipe up
        } else if (axisIndex === 1 && axisValue > 0 && lastAxes[axisIndex] <= 0) {
          // Handle swipe down
        }
        state.lastAxes[axisIndex] = axisValue;
      });
    } else {
      // This is a connected Bluetooth gamepad which you may want to support in your VR experience
    }
  }
});

The Gear VR gamepad’s button 0 will press for a short time whenever the user taps the touchpad. There are also two axes on the gamepad, which always have the value of 0, 1, or -1. The following table indicates how these values map to swipes.

AxisIf Value = 1If Value = -1
0Swipe leftSwipe right
1Swipe downSwipe up

By recording the last button press state and last axis value you can detect when the corresponding action occurs. Try out an interactive Gamepad sample which demonstrates the Gear VR gamepad values in VR here.