Java: Can't access mouseMoved() or keyPressed()
Go to solution
Solved by fizzlesticks,
One last question: How do I do the same for the KeyListener()?
Here is the complete code: http://pastebin.com/uzqYPnXK
KeyListener should only fire events for whatever component is focused. Since you can't focus a JPanel you'll need to add it to the frame. Try adding the keylistener to game instead of canvas.
Edit: Also, arrow keys won't fire keyTyped events. That will only go off for keys that have some kind of display like a-z, 0-9, enter, backspace etc. You'll need to use keyPressed if you want to use the arrow keys.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now