简体   繁体   English

如何在 javascript 中一次点击多个 html 按钮?

[英]How can I hit multiple html buttons at once in javascript?

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.我可以点击 web 页面上的箭头按钮,但是不能同时读取多个按钮。如果我点击 37 和 39 键它不起作用,但只有当我点击 37 键时它才起作用。 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.有一种方法,但依赖它并不safe ,因为您无法检测到 JavaScript 中的密钥当前是否已关闭。

The example below works only with Shift + Number.下面的示例仅适用于 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.也许您可以重复相同的登录到您的代码。

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

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