0

I'm doing a little game, but I need to detetct more than one key. KeyListener doesn't seem very useful at this.

Is there a way to detect more than one key at a time.

Thanks in advance!

John
  • 1
  • 2
    What do you mean by more than one key at a time? As in keys with modifiers shift/ctrl? Or do you mean if the user presses A, and S at the same time? – matt Jul 29 '20 at 07:43
  • 3
    Does this answer your question? [Swing's KeyListener and multiple keys pressed at the same time](https://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time) – aBnormaLz Jul 29 '20 at 07:44
  • Anyway KeyListener can handle the key press and release at the same time. There will be different events, but it handle them. If you need to catch for ex. like A+D at the same time, you can store somewhere pressed buttons and check if needed combination is used. – bench_doos Jul 29 '20 at 07:46

1 Answers1

0

When programming a game you usually have a game loop. in the loop you render each frame after checking for things You want

Meaning you can receive the press events and save them in an array or a map and then Chek this array for all pressed keys Before rendering the next frame.

The release events will remove thibgs from the pressed keys map

You can also ignore the game loop and just check and update the pressed keys map when you get a new event (but it depends on the kind of game you are trying to build )

Gabriel H
  • 1,558
  • 2
  • 14
  • 35