简体   繁体   English

使用 xbox one 控制器侦听游戏手柄按钮事件

[英]Listen for gamepad button events with xbox one controller

I'd like to listen for button press events on an xbox one controller, but seems that many solutions to this are experimental and under development.我想在 xbox one 控制器上监听按钮按下事件,但似乎对此的许多解决方案都是实验性的并且正在开发中。 Most examples I've found showing how to receive input from a gamepad use a polling method to repeatedly check the gamepad for events, but for my application it is crucial that I can record the exact timing of the button press, and I don't want to miss button presses in between scanning the game pad.我发现的大多数示例都展示了如何从游戏手柄接收输入使用轮询方法反复检查游戏手柄的事件,但对于我的应用程序而言,我可以记录按钮按下的确切时间至关重要,而我没有想错过扫描游戏手柄之间的按钮按下。

I can use either Firefox or Chrome but I haven't gotten the following solutions to work in either browser yet:我可以使用 Firefox 或 Chrome,但我还没有获得以下解决方案可以在任一浏览器中使用:

Firefox: According to this page https://www.chromestatus.com/feature/5989275208253440# , "gamepad button and axis events are implemented in Firefox behind the flag dom.gamepad.non_standard_events.enabled ". Firefox:根据此页面https://www.chromestatus.com/feature/5989275208253440# ,“游戏手柄按钮和轴事件在 Firefox 中在标志dom.gamepad.non_standard_events.enabled后面实现”。 I have enabled this in Firefox but listening to state change events (as described at https://www.smashingmagazine.com/2015/11/gamepad-api-in-web-games/ ) still has no effect.我已在 Firefox 中启用此功能,但监听状态更改事件(如https://www.smashingmagazine.com/2015/11/gamepad-api-in-web-games/ 所述)仍然无效。

Chrome: This github page https://github.com/MozillaReality/gamepad-plus/blob/master/README.md looks like it can extend the Gamepad API so I can also listen for button press events in Chrome, but when I try to compile the JavaScript as a standalone module using npm run build I get the error node_modules_missing and the build fails. Chrome:这个 github 页面https://github.com/MozillaReality/gamepad-plus/blob/master/README.md看起来可以扩展 Gamepad API,所以我也可以在 Chrome 中监听按钮按下事件,但是当我尝试使用npm run build将 JavaScript 编译为独立模块,我收到错误node_modules_missing并且构建失败。

I'd appreciate advice on how to enable .addEventListener('gamepadbuttondown' ... to work in either of these browsers.我很感激有关如何启用.addEventListener('gamepadbuttondown' ... 在这些浏览器中的任何一个中工作的建议。

Thanks to a codepen example by Christopher Van Wiemeersch at https://codepen.io/cvan/pen/aOzgGE I have found a solution in Firefox.感谢 Christopher Van Wiemeersch 在https://codepen.io/cvan/pen/aOzgGE的 codepen 示例,我在 Firefox 中找到了解决方案。

First I opened a Firefox browser (version 73.0.1) and entered about:config in the URL.首先,我打开 Firefox 浏览器(版本 73.0.1)并在 URL 中输入about:config Then I toggled dom.gamepad.non_standard_events.enabled to true.然后我将dom.gamepad.non_standard_events.enabled切换为 true。

Then I used the following functions/listeners from Christopher's codepen:然后我使用了 Christopher's codepen 中的以下函数/侦听器:

var gamepadConnected = function (e) {
  console.log('Gamepad connected at index %d: %s. %d buttons, %d axes.',
    e.gamepad.index, e.gamepad.id, e.gamepad.buttons.length, e.gamepad.axes.length);
};

var gamepadDisconnected = function (e) {
  console.log('Gamepad removed at index %d: %s.', e.gamepad.index, e.gamepad.id);
};


var gamepadButtonDown = function (e) {
  console.log('Gamepad button down at index %d: %s. Button: %d.',
    e.gamepad.index, e.gamepad.id, e.button);
};

var gamepadButtonUp = function (e) {
  console.log('Gamepad button up at index %d: %s. Button: %d.',
    e.gamepad.index, e.gamepad.id, e.button);
};
window.addEventListener('gamepadconnected', gamepadConnected);
window.addEventListener('gamepaddisconnected', gamepadDisconnected);

window.addEventListener('gamepadbuttondown', gamepadButtonDown);
window.addEventListener('gamepadbuttonup', gamepadButtonUp);

Then when a button is pressed I record the timestamp using var timestamp = new Date().getTime() .然后当按下按钮时,我使用var timestamp = new Date().getTime()记录时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM