0

Eventually I'm trying to move the object on the screen using keyboard arrow keys. At the moment I'm trying to see if the code responds to keys. It does not. I press arrow up and nothing gets output to browser console.

_handleKey(event){
    console.log(event);
    if(event.keyCode == 38){
      console.log("Arrow Up");
    }
   }

  componentDidMount() {
    document.addEventListener("keyPress", this._handleKey, false);
  }

The full code is here: http://codepen.io/wasteland/pen/GZvWeo

What do I do wrong?

1

1 Answer 1

1

I think what you want to use is the keydown event.

document.addEventListener("keydown", this._handleKey, false);

arrow keys are only triggered by onkeydown, not onkeypress

Take a look into this as well

http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.