aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/gamepadevent/index.html
blob: 6e2927bd8230bef273c3067d5ca4c58254701394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: GamepadEvent
slug: Web/API/GamepadEvent
translation_of: Web/API/GamepadEvent
---
<p>{{APIRef("Gamepad API")}}</p>

<p>Gamepad API 的 GamepadEvent 接口包含对连接到系统的控制器的引用,这也是 gamepad 事件events {{domxref("Window.gamepadconnected")}}{{domxref("Window.gamepaddisconnected")}} 被触发时响应的内容。</p>

<h2 id="构造函数">构造函数</h2>

<dl>
 <dt>{{domxref("GamepadEvent.GamepadEvent","GamepadEvent()")}}</dt>
 <dd>返回一个新的 <code>GamepadEvent</code> 对象。</dd>
</dl>

<h2 id="属性">属性</h2>

<dl>
 <dt>{{ domxref("GamepadEvent.gamepad") }} {{readonlyInline}}</dt>
 <dd>返回一个 {{ domxref("Gamepad") }} 对象,提供触发事件的控制器数据的访问。</dd>
</dl>

<h2 id="示例">示例</h2>

<p>在触发的 {{domxref("Window.gamepadconnected")}} 事件上调用控制器属性。</p>

<pre class="brush: js">window.addEventListener("gamepadconnected", function(e) {
  console.log("控制器已连接于 %d 位:%s。 %d 个按键,%d 个坐标方向。",
  e.gamepad.index, e.gamepad.id,
  e.gamepad.buttons.length, e.gamepad.axes.length);
});</pre>

<p>{{domxref("Window.gamepaddisconnected")}} 事件上的。</p>

<pre class="brush: js">window.addEventListener("gamepaddisconnected", function(e) {
  console.log("控制器已从 %d 位断开:%s",
  e.gamepad.index, e.gamepad.id);
});</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName("Gamepad", "#gamepadevent-interface", "GamepadEvent")}}</td>
   <td>{{Spec2("Gamepad")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<p>{{Compat("api.GamepadEvent")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<p><a href="/en-US/docs/Web/Guide/API/Gamepad">使用 Gamepad API</a></p>