備忘録的プログラミングリファレンス

GamepadEvent インターフェイス

 GamepadEvent インターフェイスは、ゲーム用操作デバイス、特にゲームパッドに関するインターフェイスです。

 MouseEvent インターフェイスを継承していますので、MouseEvent インターフェイスの Property と Method、Event タイプが使えます。

*GamepadEvent インターフェイスは未だ未対応部分があります。

GamepadEvent継承図

 ゲームパッドのイベントには、Window.gamepadconnected イベント、Window.gamepaddisconnected イベントがあります。 Window.gamepadconnected イベントはゲーム関連デバイスが接続されたら発生するイベントです。Window.gamepaddisconnected イベントはゲーム関連デバイスが外されたら発生するイベントです。

- ad -

プロパティ(Properties)

Example Syntax

var object_gamepad = Event.gamepad;
Property
gamepadゲーム関連デバイスに関するオブジェクト

 ゲームパッドを接続することで発生するWindow.gamepadconnected イベントから gamepad オブジェクトを取得するには以下のようにします。

gamepad の取得
window.addEventListener("gamepadconnected", (event) => {
  console.log( event.gamepad );
});

メソッド(Methods)

 GamepadEvent インターフェイスに独自のメソッドはないです。

GamepadEvent Types

 GamepadEventタイプはエレメントに設定してwebページで起こるイベントを捉えます。

Example Syntax

object.onabort = function(){script_or_function};
onGamepadEvent type
ongamepadconnectedゲームパッドが接続された
ongamepaddisconnectedゲームパッドが外された

ゲームパッドの操作情報を取得

 仕様書によれば、ゲームパッドの操作状況は、接続されたデバイスの I/O を取得する Navigator オブジェクトから取得します。
  Navigator オブジェクトは Window.navigator から取得できます。

gamepad の取得
function runAnime(){
	window.requestAnimationFrame(runAnime);
	for( const pad of window.navigator.getGamepads() ) {
	  console.log( pad );
	}
}

window.requestAnimationFrame(runAnime);

 以上の例は、window.requestAnimationFrame() メソッドで 描写が行われる度にゲームパッドの操作状況を取得するものです。
 ゲームパッドの操作状況を取得するには、常時その変化を取得するようなコードにしなければなりません。上の例は、描写が行われる度にパッドの状況を取得します。

 ゲームパッドが操作されているボタンの番号はゲームパッドによるようです。仕様書による標準的な番号は以下です。

Button/AxisLocation
buttons[0]Bottom button in right cluster
buttons[1]Right button in right cluster
buttons[2]Left button in right cluster
buttons[3]Top button in right cluster
buttons[4]Top left front button
buttons[5]Top right front button
buttons[6]Bottom left front button
buttons[7]Bottom right front button
buttons[8]Left button in center cluster
buttons[9]Right button in center cluster
buttons[10]Left stick pressed button
buttons[11]Right stick pressed button
buttons[12]Top button in left cluster
buttons[13]Bottom button in left cluster
buttons[14]Left button in left cluster
buttons[15]Right button in left cluster
buttons[16]Center button in center cluster
axes[0]Horizontal axis for left stick (negative left/positive right)
axes[1]Vertical axis for left stick (negative up/positive down)
axes[2]Horizontal axis for right stick (negative left/positive right)
axes[3]Vertical axis for right stick (negative up/positive down)

 詳しくは W3C のGamepadEventInit dictionaryを参照してください。

- ad -