Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Get the keyboard button pressed during onClick and based on that run different functions

Currently, I have 9 different functions which has to triggered by single button when onclick and keyboard numpad (1 to 9).

Edit: Solution by Sode- v2

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<button type="button" id="b41" onclick="test_ks(event)">Shape Anchor Point</button>

 <script>


  const pressedButtons = {};

  window.onkeydown = (e) => (pressedButtons[e.which] = true);
  window.onkeyup = (e) => (pressedButtons[e.which] = false);

  function test_ks(event) {
    
    if (pressedButtons[97]) {
      alert('numpad 1');
    } else if (pressedButtons[98]) {
      alert('numpad 2');
    }
  }
  
</script>

</body>
</html>


  

Tried this at https://manjunath7472.w3spaces.com/saved-from-Tryit-2021-11-06.html

Issue 1.After clicking numberpad 1, numberpad 2 doesn't work & After clicking numpad2, numpad1 works but after that numpad 2 doesn't work.

2.After initial click on anyone, from second time, with just mouse click(without pressing keyboard numpad) it triggers the alert.

3.After first clcik with keypress, it doesn't consider keyboard press and alert is triggered by mouse click only.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you bind a function to onclick in the HTML tag you want to just pass the name of the function as the event parameter will be passed in by the DOM

<button type="button" id="b41" onclick="myFunction()">myButtonName</button>

but also, a button's click event won't have any keyboard data attached to it, so you may want to look into using keydown and keyup event listeners (probably on the window object) to keep track of which keys are pressed and using that information when the button is clicked

<button type="button" id="b41" onclick="myFunction()">myButtonName</button>
<span id="output"></span>

<script>
  const pressedButtons = {};

  const outputSpan = document.querySelector('#output');

  window.onkeydown = (e) => (pressedButtons[e.which] = true);
  window.onkeyup = (e) => (pressedButtons[e.which] = false);

  function myFunction(event) {
    console.log(pressedButtons);
    if (pressedButtons[97]) {
      outputSpan.innerHTML = 'numpad 1';
    } else if (pressedButtons[98]) {
      outputSpan.innerHTML = 'numpad 2';
    } else {
      outputSpan.innerHTML = '';
    }
  }
</script>

Edit: updated to change text of a span rather than use alert()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda