[java] KeyPressed and KeyReleased are kinda broken?

Started by
3 comments, last by Laz33 18 years, 1 month ago
Hi everyone =) According to the tutorials I've looked at keyboard input in java should be handled with a keyListener.. something like this: class MyLittleWindow extends JFrame implements KeyListener { ...code... public void keyPressed(KeyEvent e) { System.out.println("key " + e.getKeyCode() + " pressed"); } public void keyReleased(KeyEvent e) { System.out.println("key " + e.getKeyCode() + " released"); } } However, this code is lying! If I press and hold a key (like one of the arrow keys for example) the program thinks that I'm hammering that key like a madman.. both pressed and released events are fired like crazy at the same speed as characters would be entered if it was a textbox. For keys like alt, shift and ctrl it's working and only one event is fired when the key is pressed and then another one once it is released again. Another good example of this can be found here: http://www.brackeen.com/javagamebook/ Download, compile and run KeyTest in chapter 3 and you will see the behavior I'm talking about. Anyone got any solution for this? Thanks!
Advertisement
Are you on a Linux box? I have only seen this happen on them.
Quote:Original post by newera
Are you on a Linux box? I have only seen this happen on them.


Yes, you're right.. I hadn't tried it on windows until now (I use Gentoo). On windows it only fires lots of KeyPressed events until released.. wich wouldn't be a problem.

Anyone has any solution to this apparently linux specific problem?

Edit: hah, take a look a this..
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5011907
"closed, not a bug".. wtf.. how can I make a game without knowing the difference between the player hammering a key and holding it.. gah!
Don't do put reaction code in the listeners. Create a set of boolean values for each key. In the pressed event, set the appropriate key to true. In the released event, set the appropriate key to false. In your main game loop check the keys to see which ones are pressed. Since the pressed and released events are fired so fast, it should no cause a problem.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quote:Original post by CaptainJester
Don't do put reaction code in the listeners. Create a set of boolean values for each key. In the pressed event, set the appropriate key to true. In the released event, set the appropriate key to false. In your main game loop check the keys to see which ones are pressed. Since the pressed and released events are fired so fast, it should no cause a problem.


Edit (again): Yep, that should work, thanks.

I also found this:
http://wiki.java.net/bin/view/Games/HowToDoKeyboardInput
wich contains theese two little tidbits of information:
Quote:
Until I look into jinput and such I'm dealing with this by disabling key repeat when in Linux (just a simple Runtime.exec("xset r off")).

and:
Quote:
On X11, you can detect the auto-repeat by examining the timestamps on the KeyEvent? objects. If two subsequent KEY_RELEASED and KEY_PRESSED events have the same timestamp, they are caused by autorepeat, not by the user.

I guess my problems are solved =)

[Edited by - Laz33 on March 3, 2006 7:52:29 AM]

This topic is closed to new replies.

Advertisement