在Wii U网络浏览器中捕获游戏手柄输入键事件

时间:2014-01-04 05:00:18

标签: javascript javascript-events wii-u

Wii U浏览器的Internet Browser - Extended Functionality页面表示A按钮和控制板应将关键事件发送到浏览器。使用下面的示例代码我能够接收A按钮的事件,但是方向键似乎只是想在页面上滚动而不会触发任何事件。

如何正确接收这些事件的通知?

<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>

我宁愿避免轮询window.wiiu.gamepad对象,因为我只需要通过控制板和A键按键事件提供的输入。

1 个答案:

答案 0 :(得分:2)

原来可以通过任何keydown,keyup或keypress事件捕获A按钮,但只能通过keydown和keyup事件捕获八路数字键盘。您还可以取消该活动,以防止浏览器正常处理在preventDefault()页面上的链接之间移动。

示例代码:

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";
    }
};