Jump to content
  • Advertisement
Sign in to follow this  
Laz33

[java] KeyPressed and KeyReleased are kinda broken?

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!