Search code examples
javascripthtmlbuttonesp8266keyevent

JavaScript key holding down and release


I can control my bot through web. These are its url-

I am using html button to do this. But I want to control it with "arrow" key of keyboard. My idea is if I press and hold up arrow key then the bot will go forward(https://172.0.0.1/forward) and if I release the up arrow key then the bot will Stop(https://172.0.0.1/stop) going. and the same thing to

  • backward - "down arrow"
  • left- "left arrow"
  • right-"right arrow"

I'm not familiar with JavaScript Can you please help me...


Solution

  • try like this.

    window.addEventListener('keydown',function(event){
    if(event.keyCode===37) { //left
    }
    if(event.keyCode===38) { //up
    }
    
    if(event.keyCode===39) { //right
    }
    if(event.keyCode===40) { //down
    }
    
    });
    

    UPDATE

    window.addEventListener('keyup',function(event){
    //    do something
    
    });