0

I'm making a game and want to have the player character be controlled by the keyboard, but I don't want to use key listeners if I don't have to because they call their methods when the key is pressed which I want to avoid. Is there a way to check if specific keys (e.g the w key) are being held down inline instead of having the listener run code when the key is pressed?

I want to have control over when the keys are "looked at" as opposed to keyPressed() happening in the middle of the program doing other things. Is there anything like:

getIsKeyPressed(VK_w);

That would return a boolean value for if the key is currently held down?

ASF
  • 64
  • 11
  • 1
    What "front-end" do you use? JavaFX or Swing? One solution is to listen to events to maintain your own keyboard state object, and pull the keys' state on it. – kagmole May 24 '18 at 11:10
  • 1
    Save state in some object and update it on events https://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key – TEXHIK May 24 '18 at 11:11
  • Storing the values in an object and reading from it as needed sounds like it would be a good solution, my main worry was values changing mid calculation and putting things in an erroneous state. That should probably be posted as an answer though. – matthew hurley May 25 '18 at 11:29

0 Answers0