Q Z
Q Z

Reputation: 121

How often does KeyListener check keyboard input? (Java GUI)

I am coding a program whose functions include one that checks whether the user input is one of the arrow keys and moves an on-screen sprite accordingly.

I am using the KeyListener interface for an inner class I call ArrowListener. Currently, I have code in the keyPressed() that moves the sprite on the screen.

I am wondering how often my ArrowListener class checks for keyboard input, because I have another method in the larger component class that calls repaint() every 100 milliseconds. If the KeyListener class checks user input more or less often than this, I will change the repaint frequency as well.

EDIT:

I realized that KeyListener doesn't check/poll the keyboard for inputs regularly, but processes interrupts from the keyboard. Still, if I were to hold down a key on the keyboard for, say, 5 seconds, how many interrupts would KeyListener process?

Upvotes: 2

Views: 269

Answers (1)

Jacob G.
Jacob G.

Reputation: 29730

It looks like I was mistaken; what you're looking for are KeyBindings; these allow you to execute an ActionListener only once when a key is pressed.

Upvotes: 0

Related Questions