I can click the arrow buttons on the web page, but multiple buttons cannot be read at the same time.If I click on 37 and 39 keys it does not work but only when I click on 37 keys it works. Same goes for other keys, how can I control more than one button at the same time?
const handleArrowButtonMouseDown = function() {
$('button').on('mousedown touchstart', (e) => {
e.preventDefault();
if(state.arrowPressed) return;
let direction = $(e.target).closest('button').attr('id');
state.setArrowPressed(direction);
state.setRobotCommand();
});
};
const handleArrowButtonMouseUp = function() {
$('button').on('mouseup mouseleave touchend', endCurrentOperation);
};
const handleArrowKeyDown = function() {
$(document).keydown((e) => {
if(state.arrowPressed) return;
switch(e.which) {
case 37:
state.setArrowPressed('left');
break;
case 38:
state.setArrowPressed('up');
break;
case 39:
state.setArrowPressed('right');
break;
case 40: // down
state.setArrowPressed('down');
break;
default: return;
}
e.preventDefault();
state.setRobotCommand();
});
};
const handleArrowKeyUp = function() {
$(document).keyup((e) => {
if (e.which === 37 && state.arrowPressed !== 'left') return;
if (e.which === 38 && state.arrowPressed !== 'up') return;
if (e.which === 39 && state.arrowPressed !== 'right') return;
if (e.which === 40 && state.arrowPressed !== 'down') return;
endCurrentOperation();
});
};
There is one way, but it's not safe to rely on, since you cannot detect if a key is currently down in JavaScript.
The example below works only with Shift + Number. It's closest to achieve when both keys are pressed.
window.addEventListener("keydown", function (event) {
if (event.shiftKey) {
if (/\d/.test(event.code)){
console.log("SHIFTKEY + Number:"+event.code);
}
}
}, true)
Maybe you can repeat the same login to your code.