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

184
Visualizações
Javascript, how do I make the up key only pressed once
onEvent("screenGamemode3", "keydown", function(event) {
  if (event.key == "Left"|| event.key == "a") {
    xloc = xloc - 10;
  } else if (event.key == "Right" || event.key == "d" ) {
    xloc = xloc + 10;
  } else if (event.key == "Down" || event.key == "s" ) {
    yloc = yloc + 20;
  } else if (event.key == "Up" || event.key == "w") {
    yloc = yloc - 50;
  }
});

I want the else if (event.key == "Up" || event.key == "w") { yloc = yloc - 50;part only clickable once and not be able to get clicked twice

Thank you so much in advance

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

0

You could keep track of the last pressed key, but I'm guessing you probably want to use keyup instead of keydown. The latter is fired continuously while keyup is only fired once. Or at least until the user presses the same key again.

about 4 years ago · Juan Pablo Isaza Relatório

0

The easiest way to use a flag variable, by default set it to false and when KEYUP is clicked - set it to true. And check also your flag variable in your else if condition. Here is the code:

let isKeyupPressed = false;

window.addEventListener('keydown', event => {

  if (event.key == "ArrowLeft"|| event.key == "a") {
    // xloc -= 10;
  } else if (event.key == "ArrowRight" || event.key == "d" ) {
    // xloc += 10;
  } else if (event.key == "ArrowDown" || event.key == "s" ) {
    // yloc += 20;
  } else if (!isKeyupPressed && (event.key == "ArrowUp" || event.key == "w")) {
    // yloc -= 50;
    isKeyupPressed = true;
    console.log('Arrow Up invoke? ', event.key); // it will log only once
  }
  
  console.log(event.key);
})

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure I understand what you want to do, but can something like this resolve your problem ?

let isFirst = true;
onEvent("screenGamemode3", "keydown", function(event) {
  if (event.key == "Left"|| event.key == "a") {
    xloc = xloc - 10;
  } else if (event.key == "Right" || event.key == "d" ) {
    xloc = xloc + 10;
  } else if (event.key == "Down" || event.key == "s" ) {
    yloc = yloc + 20;
  }
    else if ((event.key == "Up" || event.key == "w") && isFirst == true) {
    yloc = yloc - 50;

    }
});
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