# How should i have the key/mouse input in an advanced java game?

I am a self taught programmer, so I do not know the proper ways to do things. I have made simple games like asteroids and snake, but in those games, you can easily modify the variables within the keyevent functions. Here is how I did it in my simple Asteroids game:

/*
* key listener events
*/
public void keyReleased(KeyEvent k){
int keyCode = k.getKeyCode();

switch(keyCode){

case KeyEvent.VK_LEFT:
turnLeft = false;
break;

case KeyEvent.VK_RIGHT:
turnRight = false;
break;

case KeyEvent.VK_UP:
accel = false;
break;

case KeyEvent.VK_1:
cls = true;
break;
case KeyEvent.VK_ENTER:
break;
case KeyEvent.VK_SPACE:
fire = false;
}
}
public void keyTyped(KeyEvent K){}
public void keyPressed(KeyEvent k){
int keyCode = k.getKeyCode();

switch(keyCode){

case KeyEvent.VK_LEFT:
turnLeft = true;
break;

case KeyEvent.VK_RIGHT:
turnRight = true;
break;

case KeyEvent.VK_UP:
accel = true;
break;

case KeyEvent.VK_1:
cls = false;
break;
case KeyEvent.VK_ENTER:
clearAllBullets();
break;
case KeyEvent.VK_SPACE:
fire = true;
}
}


If I were to make a more advanced game (with a main menu, options, main game, etc.), how should I do the key/mouse input?

Also, if I were to go into the single-player, should i put all of the gameplay code into one class? Is there a way to put the single player code into a separate class and somehow have the key input still modify the variables and such?

• +1 Your answer allowed me to learn something new from bearcdp's answer. Thanks for asking! =) – Will Marcouiller Mar 25 '11 at 13:14

I am no expert, and I nevertheless suggest separating both the controls and the business/game/menu (name it) logic.

I have already answered a similar question in regards to giving directions to a Snake in a game designed with classes. Java being an object-oriented language, I guess you are able to write adequate classes and stuff. Then, when you have written your object class, if it is so that your object needs to react to some keys and mouse clicks, I would go with a Controller class that knows what is currently displayed on screen, then tell this object what it should do depending on the key that has been pressed or the region that has been clicked.

Here's the link to the question I told you about which I answered about separating the controls from the object logic. It also includes a code sample which illustrates my thought.

(Unfortunately, none of the answer was yet accepted by the OP, so we don't know for sure whether it solved his concerns. Besides, I'm always designing this way and found no trouble by doing so so far.)

Now I know, it's tagged XNA. The idea or design is a higher architecture level than the technology that is used, so this design could be simply adapted to Java syntax. The general idea remains the same. =)

I do hope this helps! I'll be glad to help further when I can, just ask! =)

Will's answer shows you what is probably the most flexible and straightforward, and I'd recommend it if what you need is straight-ahead input polling.

But, if you need an easy way to check for different combinations of button presses while avoiding massive amounts of switch and if statements, you can use bit flag enums.

I dunno what you know about bit vectors, so here's an overview: Basically, you have an enum class that represents all possible keys. Each enum should have a value that corresponds to a bit in an integer. (ie: 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0011, 0x0012, 0x0014, etc). (Link for doing this in Java).

Now, since you can pass around information about up to 32 (or 64 if you use longs) keys, you can have a hash table that associates bit vectors (int or long) with objects falling some sort of KeyComboCallback interface, which just contains a single method call, Do(), that gets called when the associated combination of keys are pressed.

Now when you instantiate/initialize your player object or your menu object, you can make a call to some sort of input manager and pass it all the key combination bit vectors you want and associate them with anonymous inner classes.

// ... in your init code for Player

InputManager.map(
KeyMap.A.val() | KeyMap.B.val() | KeyMap.C.val(),
new KeyComboCallback(KeyMap keys) {
public void Do() {
this.DoCrazyAwesomeComboAttack();
}
});

// ...  continue init


This is really quick & convenient in C++ and C#, but Java enums are special, so you may find it more convenient to create a KeyCombination class or something that will store all of this information without worrying about enums. But the idea is still the same. I'm using this in an XNA game, and I like the facility a lot. You can also use it to separate input regions, which is useful for comparing different parts of the keyboard or gamepad independently (might be useful for two players on one keyboard).

• I like your post, especially the "this.DoCrazyAwesomeComboAttack();" XD. Your path seems to be the better way for complicated stuff, so im going to stick with easier stuff. Thank you for your help! – Steven Rogers Mar 25 '11 at 6:01
• +1 Very interesting answer! I have learned from this answer and shall take an eye out it when I have more time to spend in my hobby game development. And thanks for telling that my answer is good, though it is for simpler stuff, which I wouldn't argue, since I'm a beginner in GD. =) Thanks! – Will Marcouiller Mar 25 '11 at 13:13