John B. Matthews wrote:
In article <ddbc8$4959f55a$50db01d9$16296@news.hispeed.ch>,
Philipp Gressly <phi@gressly.ch> wrote:
I am programming a "moon-lander" and want to check every 100 ms if a
certain key is pressed. The keyboard events (key-pressed,
key-released and key-typed) are not helpful, because the operating
System (linux im my case) generates key-releases and key-presses at
its own (depending on the "key repeat speed").
Is there a command to satisfy the following interface easily?
public interface KeyState {
boolean isKeyDown(char keyCode);
}
I don't see a way to ignore spurious keyReleased() events except to
instruct the user to disable key repeat using the host's control panel.
It may be possible to do so programmatically using the Java Desktop
System's assistive technology:
<http://docs.sun.com/app/docs/doc/817-7307>
It sounds like you already understand the obvious implementation:
Enumerate the keys of interest and map the state of each. In an
implementation of the KeyListener interface (or an extension of the
KeyAdapter class), handle keyPressed() keyReleased() events accordingly:
Mark a key as down on keyPressed(); mark a key as up on keyReleased().
Return the corresponding state in isKeyDown().
You can see a coded example of what John describes in my Asteroids game.
There is a link to the source code at the bottom.