Home > Software engineering >  How to launch a function when an event stop?
How to launch a function when an event stop?

Time:04-26

Anyone knows how I cant check when an event stop?

Im coding a game just to learn how to use the canvas tag on HTML. To make player move, Im using switch case like this:

switch (event.code) {
case 'ArrowUp':
  // code
  break;
case 'ArrowDown':
  break;
case 'ArrowRight':
  // code
  break;
case 'ArrowLeft':
  // code
default:
  break;}

But I need to launch a function when the user stop to press the key.

CodePudding user response:

To do this I got use a event listener with the parameter "keyup"

document.body.addEventListener('keyup',()=>{
    // function
};

CodePudding user response:

document.addEventListener('mousedown', function(event) {
  console.log('clicked', event.code);
  switch (event.code) {
case 'ArrowUp':
  // code
  break;
case 'ArrowDown':
  break;
case 'ArrowRight':
  // code
  break;
case 'ArrowLeft':
  // code
default:
  break;}
})
document.addEventListener('mouseup', function() {
  console.log('stopped clicking');
})

  • Related