Public Group

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

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

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;

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:
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:
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 on other sites
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 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 on other sites
In Java, I usally use auto-gen. from netbeans ;)

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.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5
Rutin
12

• 12
• 12
• 58
• 14
• 15
• Forum Statistics

• Total Topics
632119
• Total Posts
3004208

×