Jump to content

  • Log In with Google      Sign In   
  • Create Account


I cannot get Java to properly recognize my key releases


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Gixugif   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 January 2012 - 09:53 PM

I am aware that there is a bug, a 12 or 13 year old bug, that exists on Linux and probably other Unix based systems, that certain ways of detecting when a key is pressed or released do not function the way one would expect. I am also aware there are a few ways around this and I have gone so far to attempt to implement this solution that can be viewed here: http://stackoverflow.../4679195/596359

The problem is that this does not seem to fix anything. The first question someone will ask I believe I can anticipate. I placed this code in a ".java" file named "RepeatingKeyEventsFixer.java". This file is in "../src/com/example/". To use it in my code I have put the following lines at the beginning of my main() method:

RepeatingKeyEventsFixer repeatingkeyeventsfixer = new RepeatingKeyEventsFixer();
repeatingkeyeventsfixer.install();

Still, when I press down a key, it tells me it's being pressed down and held, but it will also say it is being released with just about the same frequency. Does anyone have any idea what I may have done wrong? I have also posted this on StackExchange with poor results. I'll post the link here in case the way I phrased things there could help any more: http://stackoverflow.../8948238/596359

The following code is the code I am using to test if a key has been released or not. I appreciate the time any takes to consider my question.

addKeyListener(new KeyAdapter() {
	public void keyPressed(KeyEvent e){
		processKey(e);
	}
	public void keyReleased(KeyEvent g){
		processKeyReleased(g);
	}
}
...
private void processKeyReleased(KeyEvent g){
	int keyCode = g.getKeyCode();
	if(!isPaused && !gameOver){
		if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT){
			System.out.println("released");
			player.stopLooping();
		}
	}
}


Sponsor:

#2 medv4380   Members   -  Reputation: 98

Like
1Likes
Like

Posted 20 January 2012 - 11:43 PM

That's one of several issues in java.

In truth the fix is working as intended.
This is the line you need to edit in

RepeatingKeyEventsFixer.java

to try and get the code to behave the way you want it.
public static final int RELEASED_LAG_MILLIS = 5;

It is using a timer trick to ignore the key repeats. The way the code is written it waits for 5 milliseconds by default before it reverts back to the undesirable behavior.

There are other "bugs" that you'll eventually run into using KeyAdapters and KeyListeners. One annoying one is when multiple keys are being pressed.

These aren't really bugs in truth. They behave this way because the OS is really sending these events. It makes the events worthless to listen to but that's how it goes. In C and C++ the key repeat is turned off for games since it ruins the experience.

Oracle really should look into integrating some of the more useful features needed for games, but until then you might want to consider using the lwjgl or something similar.
With the lwjgl you can write something that looks at the keyboard state instead, and if you still needed to know if the key was just pressed or if the key was just released you could write something like this.
    boolean keyUp = false;
    private void checkInput() {
	    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
		    GameThread.isRunning = false;
	    }
	    if(Keyboard.isKeyDown(Keyboard.KEY_UP)  && keyUp == false){
		    System.out.println("up Key Pressed");
		    keyUp = true;
	    }
	    if(keyUp == true && !Keyboard.isKeyDown(Keyboard.KEY_UP)){
		   System.out.println("up Key Released");
		   keyUp = false;
	    }
    }

I believe you can even turn off the repeat events if you wanted to, but basically if you want to do what you're doing in Java correctly then you'd have to implement some native code or an exiting library that already has the native code.

#3 Gixugif   Members   -  Reputation: 122

Like
0Likes
Like

Posted 21 January 2012 - 12:20 AM

Thank you, I see how that works now. I mean to read through the solution when I get the chance to better understand it, but it seems this solution would still be rather troubling in the context I'm using it. Since writing this game is also meant to be a learning experience I'm trying to implement many things myself that higher-level libraries would otherwise take care of such as loading sounds and graphics, but taking a looking at lwjgl just for this may be the way to go and it may prevent problems later on too.

Edit: Of course using this for my input here would require me to use it in other areas to, requiring a massive rewrite. I'm wondering if that update will be quicker and maybe even save me a lot more time in the long run, or if I can think of a nice solution for this using the method I was originally going with.

#4 Antheus   Members   -  Reputation: 2397

Like
1Likes
Like

Posted 21 January 2012 - 09:11 AM

I'm trying to implement many things myself that higher-level libraries


Here is study of key event behavior in JavaScript. While JS adds another layer of abstraction, it's still closely tied to platform it's running on.

Just something to think about when trying to do something from scratch, let alone trying to do something portable. Each such issue can easily become a life-long full-time job.

The behavior is also not a bug as such, just different way of handling keyboard events.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS