GamepadButton

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

* Some parts of this feature may have varying levels of support.

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

GamepadButton インターフェイスは、ゲームパッドなどのコントローラーの各ボタンを定義します。このインターフェイスによって、コントローラー上で使用できるボタンの状態を確認することができます。

GamepadButton オブジェクトは、Gamepad インターフェイスの buttons プロパティから取得できます。

インスタンスプロパティ

GamepadButton.pressed 読取専用

論理値で、ボタンが押されているかどうかを示します。ボタンが押されている場合は true、押されていない場合は false になります。

GamepadButton.touched 読取専用

論理値で、ボタンが現在タッチされている (true) か、タッチされていない (false) かを示します。

GamepadButton.value 読取専用

多くのモダンなゲームパッドにあるトリガーなどのアナログボタンの現在の状態を示す浮動小数点数です。この値は 0.0 から 1.0 までの値に正規化されており、0.0 はボタンが押されていない状態で、1.0 はボタンが完全に押されている状態を示します。

以下の例では、各ボタンの値は GamepadButton オブジェクトの配列に格納されます。使用するプロパティはこのオブジェクトの GamepadButton.value または GamepadButton.pressed プロパティのどちらかであり、どちらを使用すればよいかはボタンの種類に依存します。この例ではどちらにも対応しています。

js
function gameLoop() {
  const gp = navigator.getGamepads()[0];

  if (gp.buttons[0].value > 0 || gp.buttons[0].pressed) {
    b--;
  } else if (gp.buttons[1].value > 0 || gp.buttons[1].pressed) {
    a++;
  } else if (gp.buttons[2].value > 0 || gp.buttons[2].pressed) {
    b++;
  } else if (gp.buttons[3].value > 0 || gp.buttons[3].pressed) {
    a--;
  }

  ball.style.left = `${a * 2}px`; // ball is a UI widget
  ball.style.top = `${b * 2}px`;

  requestAnimationFrame(gameLoop);
}

仕様書

Specification
Gamepad
# gamepadbutton-interface

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
GamepadButton
pressed
Secure context required
Experimental
touched
value

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.

関連情報