0
我嘗試模擬WASD在智能手機上的移動。當我點擊智能手機上的按鈕時,它會觸發該按鈕,但從不停止。模擬點擊不起作用的鍵
如何讓程序停止釋放按鈕時停止?有沒有像keyup需要或有任何人更好的主意?
function fireKey(el, key) {
if (document.createEventObject) {
var eventObj = document.createEventObject();
eventObj.keyCode = key;
el.fireEvent("onkeydown", eventObj);
} else if (document.createEvent) {
var eventObj = document.createEvent("Events");
eventObj.initEvent("keydown", true, true);
eventObj.which = key;
eventObj.keyCode = key;
el.dispatchEvent(eventObj);
}
}
這會觸發按鈕:
<paper-button data-key="LEFT" on-down="handleButton">
<iron-icon data-key="LEFT" icon="hardware:keyboard-arrow-left" style="color: white;"></iron-icon>
</paper-button>
<paper-button data-key="RIGHT" on-down="handleButton">
<iron-icon data-key="RIGHT" icon="hardware:keyboard-arrow-right" style="color: white;"></iron-icon>
</paper-button> <br/>
<paper-button data-key="DOWN" on-down="handleButton">
<iron-icon data-key="DOWN" icon="hardware:keyboard-arrow-down" style="color: white;"></iron-icon>
</paper-button>
您是否嘗試過在按鈕釋放時添加onkeyup事件? – AmericanUmlaut
你用什麼事件觸發'fireKey()'? –
你永遠不會說它停止。改爲使用'keyPress'事件。只需用'onkeypress'和'keypress'替換上面的'onkeydown'和'keydown'即可。 – allnodcoms