如何映射移动触摸事件以模拟 Javascript 中的按键 up/down?
How can I map mobile touch events to simulate key press up/down in Javascript?
我有两个类似按钮的 div 元素,我想将它们映射到键盘上的左右箭头键。
<div id="leftBtn" ontouchstart="simKeyDown(event, LEFT_KEYCODE);" ontouchend="simKeyUp(event, LEFT_KEYCODE);" onmspointerup="simKeyUp(event, LEFT_KEYCODE);" onmspointerdown="simKeyDown(event, LEFT_KEYCODE);"><img src="../images/left.png"></div>
<div id="rightBtn" ontouchstart="simKeyDown(event, RIGHT_KEYCODE);" ontouchend="simKeyUp(event, RIGHT_KEYCODE);" onmspointerup="simKeyUp(event, RIGHT_KEYCODE);" onmspointerdown="simKeyDown(event, RIGHT_KEYCODE);"><img src="../images/right.png"></div>
然后在我的 Javascript 我有:
//Key codes for simulating key events
var LEFT_KEYCODE = 37; //Left cursor key
var RIGHT_KEYCODE = 39; //Right cursor key
//Simulate a key up event
function simKeyUp(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key up event
queueKeyboardEvent('KeyUp', keyCode);
}
//Simulate a key down event
function simKeyDown(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key down event
queueKeyboardEvent('KeyDown', keyCode);
}
function queueKeyboardEvent(eventType, keyCode) {
**something goes here**
}
我卡在了最后一个要将触摸事件映射到按键的函数上。任何帮助或指导将不胜感激。我也愿意接受其他实施方式。
您可以使用 2 个变量来跟踪 left/right 键是否被按下。基于此,您可以在 canvas 渲染动画方法中检查这些值以处理必要的操作。
例如:
var LEFT_KEY_PRESSED = false;
var RIGHT_KEY_PRESSED = false;
无论何时调用 keyUp 或 keyDown 方法,您都可以将变量更新为 ture/false
//Key codes for simulating key events
var LEFT_KEYCODE = 37; //Left cursor key
var RIGHT_KEYCODE = 39; //Right cursor key
var LEFT_KEY_PRESSED = false;
var RIGHT_KEY_PRESSED = false;
//Simulate a key up event
function simKeyUp(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key up event
queueKeyboardEvent('KeyUp', keyCode);
}
//Simulate a key down event
function simKeyDown(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key down event
queueKeyboardEvent('KeyDown', keyCode);
}
function queueKeyboardEvent(eventType, keyCode) {
if (eventType === 'KeyUp') {
if (keyCode == LEFT_KEYCODE) {
LEFT_KEY_PRESSED = true
}
if (keyCode == RIGHT_KEYCODE) {
RIGHT_KEY_PRESSED = true
}
}
if (eventType === 'KeyDown') {
if (keyCode == LEFT_KEYCODE) {
LEFT_KEY_PRESSED = false
}
if (keyCode == RIGHT_KEYCODE) {
RIGHT_KEY_PRESSED = false
}
}
}
如果您有任何 requestframe 方法或动画方法,您可以检查值并进行所需的更新。
例如:
function animate() {
if (LEFT_KEY_PRESSED ) {
player.left++
}
if (RIGHT_KEY_PRESSED ) {
player.right++
}
}
我有两个类似按钮的 div 元素,我想将它们映射到键盘上的左右箭头键。
<div id="leftBtn" ontouchstart="simKeyDown(event, LEFT_KEYCODE);" ontouchend="simKeyUp(event, LEFT_KEYCODE);" onmspointerup="simKeyUp(event, LEFT_KEYCODE);" onmspointerdown="simKeyDown(event, LEFT_KEYCODE);"><img src="../images/left.png"></div>
<div id="rightBtn" ontouchstart="simKeyDown(event, RIGHT_KEYCODE);" ontouchend="simKeyUp(event, RIGHT_KEYCODE);" onmspointerup="simKeyUp(event, RIGHT_KEYCODE);" onmspointerdown="simKeyDown(event, RIGHT_KEYCODE);"><img src="../images/right.png"></div>
然后在我的 Javascript 我有:
//Key codes for simulating key events
var LEFT_KEYCODE = 37; //Left cursor key
var RIGHT_KEYCODE = 39; //Right cursor key
//Simulate a key up event
function simKeyUp(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key up event
queueKeyboardEvent('KeyUp', keyCode);
}
//Simulate a key down event
function simKeyDown(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key down event
queueKeyboardEvent('KeyDown', keyCode);
}
function queueKeyboardEvent(eventType, keyCode) {
**something goes here**
}
我卡在了最后一个要将触摸事件映射到按键的函数上。任何帮助或指导将不胜感激。我也愿意接受其他实施方式。
您可以使用 2 个变量来跟踪 left/right 键是否被按下。基于此,您可以在 canvas 渲染动画方法中检查这些值以处理必要的操作。 例如:
var LEFT_KEY_PRESSED = false;
var RIGHT_KEY_PRESSED = false;
无论何时调用 keyUp 或 keyDown 方法,您都可以将变量更新为 ture/false
//Key codes for simulating key events
var LEFT_KEYCODE = 37; //Left cursor key
var RIGHT_KEYCODE = 39; //Right cursor key
var LEFT_KEY_PRESSED = false;
var RIGHT_KEY_PRESSED = false;
//Simulate a key up event
function simKeyUp(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key up event
queueKeyboardEvent('KeyUp', keyCode);
}
//Simulate a key down event
function simKeyDown(e, keyCode) {
//Suppress the default action
e.preventDefault();
//Send the event as a key down event
queueKeyboardEvent('KeyDown', keyCode);
}
function queueKeyboardEvent(eventType, keyCode) {
if (eventType === 'KeyUp') {
if (keyCode == LEFT_KEYCODE) {
LEFT_KEY_PRESSED = true
}
if (keyCode == RIGHT_KEYCODE) {
RIGHT_KEY_PRESSED = true
}
}
if (eventType === 'KeyDown') {
if (keyCode == LEFT_KEYCODE) {
LEFT_KEY_PRESSED = false
}
if (keyCode == RIGHT_KEYCODE) {
RIGHT_KEY_PRESSED = false
}
}
}
如果您有任何 requestframe 方法或动画方法,您可以检查值并进行所需的更新。
例如:
function animate() {
if (LEFT_KEY_PRESSED ) {
player.left++
}
if (RIGHT_KEY_PRESSED ) {
player.right++
}
}