Jump to content
  • Advertisement
Sign in to follow this  

Java - need help with key/mouse input...

This topic is 3854 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

Hello, I am a starting Java programmer, I am taking classes currently, and have been able to create a basic 2d rendering program. I am familiar with KeyListener,KeyEvent, and ActionListener. I haven't implemented a mouse listener, but I gather it is probably similar. Anyhow, I was wondering what all of you have devised as the best practice for handling key/mouse input. Do you just code in only the keyCodes you use for your current game, or is there an all encompassing way to incorporate a generic KeyState class which I can just use at my whims? This is sort of what I am looking for, I am just looking for the best implementation of it.
public class KeyState
{
	protected boolean escape,w,a,s,d;
	private boolean escapeDown,wDown,aDown,sDown,dDown;

	public void keyPressed(int key)
	{
		switch(key)
		{
			case 27:
			if(!escape && !escapeDown){escape=true; escapeDown=true;}
			break;
			case 87:
			if(!w && !wDown){w=true; wDown=true;}
			break;
			case 65:
			if(!a && !aDown){a=true; aDown=true;}
			break;
			case 83:
			if(!s && !sDown){s=true; sDown=true;}
			break;
			case 68:
			if(!d && !dDown){d=true; dDown=true;}
			break;
		}
	}

	public void keyReleased(int key)
	{
		switch(key)
		{
			case 27:
			if(escape && escapeDown){escape=false; escapeDown=false;}
			break;
			case 87:
			if(w && wDown){w=false; wDown=false;}
			break;
			case 65:
			if(a && aDown){a=false; aDown=false;}
			break;
			case 83:
			if(s && sDown){s=false; sDown=false;}
			break;
			case 68:
			if(d && dDown){d=false; dDown=false;}
			break;
		}
	}
}


So in my input method, I just use: //KeyState keys=new KeyState instantiated earlier. if(keys.escape){//do code} Is this good, or are there better ways? I have had good results with what I have already, but like I said, I would prefer to use what everyone deems is the best practice.

Share this post


Link to post
Share on other sites
Advertisement
I guess my next question is, how do you disable the auto-repeat for KeyEvents?

If a user presses and holds a key, it repeats over and over. Id prefer to just catch that initial keyPressed/keyReleased without having to use double booleans.

Share this post


Link to post
Share on other sites
Hi, I'am also a beginner OpenGL-Java programmer, and the JOGL nehe samples were VERY useful for me. There are samples for your questions.
I use jogl 1.1.0 with netbeans 5.5.
By

Share this post


Link to post
Share on other sites
Since Java provides easy-to-use API's for almost everything you need, I'd advise you to stick only to the keys you will used in your app-gamme. Implementing othr functions to new keys is rather easy, while trying to account for every keystroke may be too difficult and resouce consuming, and it's ultimately pointless. If you already understand the mechanics of the KeyEvents and KeyEventListeners, then you won't have any problems implementing it in a different app which uses different commands for each keystroke.

And as for the other question, as far a I remember, when you hold a key, it sends ONE KeyPressed event, and after a time, it sends ONE KeyRepeated event for as long as it is pressed. I don't know if you are having problems with this, but I may be wrong. I'm certain that what I said is true in J2ME, but I don't know in J2SE. I'll research on this matter and I may come with an answer later.

[EDIT]: I've researched and found that J2SE does not have a keyRepeated() method as J2ME. So, this meams that you may need to check that the key was pressed but it was not released to prevent key repeating issues, or maybe using flags that set/reset when the keys are pressed/released could be a better approach for gaming keystroke input.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!