简体   繁体   English

在Wii-U web浏览器中捕获游戏手柄输入按键事件

[英]Capturing gamepad input key events in Wii-U web browser

The Inte.net Browser - Extended Functionality page for the Wii-U browser indicates that the A button and the control pad should send key events to the browser. Inte.net 浏览器 - Wii-U 浏览器的扩展功能页面指示A按钮和控制面板应向浏览器发送按键事件。 Using the sample code below I was able to receive events for the A button but the directional pad seems to just want to scroll around the page and no events are triggered.使用下面的示例代码,我能够接收A按钮的事件,但方向键似乎只想在页面上滚动,没有触发任何事件。

How can I properly receive notification of these events?我怎样才能正确接收这些事件的通知?

<script>
  document.body.onkeypress = function (event) {
    var pad = ["LEFT", "UP", "RIGHT", "DOWN"];
    var div = document.getElementById("text");
    // handle the A button
    if (event.keyCode == 13) {
      div.innerText = "A";
    }
    // handle the control pad - this doesn't seem to work
    if (event.keyCode >= 37 && event.keyCode <= 40) {
      div.innerText = pad[event.keyCode - 37];
    }
  };
</script>

I would rather avoid polling the window.wiiu.gamepad object as I only need the input that should be provided through the Control Pad and A button key events.我宁愿避免轮询window.wiiu.gamepad object,因为我只需要通过控制板和A按钮键事件提供的输入。

Turns out the A button can be captured by any of the the keydown, keyup or keypress events but the eight way digital pad can only be captured through the keydown and keyup events.事实证明,A 按钮可以被任何 keydown、keyup 或 keypress 事件捕获,但八向数字键盘只能通过 keydown 和 keyup 事件捕获。 You can also cancel the event to prevent the normal browser handling of moving between links on a page with preventDefault() .您还可以取消该事件,以阻止正常的浏览器处理使用preventDefault()在页面上的链接之间移动。

Sample code:示例代码:

document.body.onkeyup = function (event) {
    var pad = ["LEFT", "UP", "RIGHT", "DOWN"];
    var div = document.getElementById("text");
    // handle the A button
    if (event.keyCode == 13) {
        div.innerText = "A - KEYUP";
    }
    // handle the control pad
    if (event.keyCode >= 37 && event.keyCode <= 40) {
        div.innerText = pad[event.keyCode - 37] + " - KEYUP";
    }
    // prevent the Wii U browser from processing the event further
    event.preventDefault();
    return false;
};

document.body.onkeydown = function (event) {
    var pad = ["LEFT", "UP", "RIGHT", "DOWN"];
    var div = document.getElementById("text");
    // handle the A button
    if (event.keyCode == 13) {
        div.innerText = "A - KEYDOWN";
    }
    // handle the control pad
    if (event.keyCode >= 37 && event.keyCode <= 40) {
        div.innerText = pad[event.keyCode - 37] + " - KEYDOWN";
    }
    // prevent the Wii U browser from processing the event further
    event.preventDefault();
    return false;
};

document.body.onkeypress = function (event) {
    var div = document.getElementById("text");
    // handle the A button
    if (event.keyCode == 13) {
        div.innerText = "A";
    }
};

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

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