K
K
katyarom2018-10-07 08:10:29
JavaScript
katyarom, 2018-10-07 08:10:29

How to move the image when pressing the arrows on the keyboard?

I have a component with an image, how can I make the image move when I press the up/right/left/down buttons on the keyboard?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
s-jet, 2018-10-07
@katyarom

componentDidMount(){
        document.addEventListener("keydown", this.actionFunction, false); 
}
componentWillUnmount(){
        document.removeEventListener("keydown", this.actionFunction, false);
}
actionFunction(event){
        if(event.keyCode === 27) {
           (нажали esc - выходим из режима движения и так далее)
        };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question