How to Detect Multiple KeyPressing of buttons on the keyboard

Started by
4 comments, last by Nicholas Kong 9 years, 11 months ago

guys i need help on how i can implement a way on how to read multiple key pressing from the keyboard e.g like when i am pressing the left key and a i press the shoot or pass button i want my player to pass the ball.Currently my game agent stops responding for a while when i press another key .e.g when i press the up button from the left button the player stops for a while and then heads upwards.i want the movements to be smooth

public void keyPressed(KeyEvent e) {

if(g_SoccerPitch.GameOn() && g_SoccerPitch != null){

switch (e.getKeyChar()) {

case 'w':
case 'W':{
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveUp();
chaseBall();
}
break;

case 's':
case 'S':{
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveDown();
chaseBall();
}
break;

case 'a':
case 'A':{
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveLeft();
chaseBall();
}
break;

case 'd':
case 'D':{
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveRight();
chaseBall();
}
break;

case 'm':
case 'M':{
//shoot ball if ball withinin kicking range and team is in control
if(g_SoccerPitch.UserControlledTeam.InControl() && g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinKickingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerShootBall();
}
}
break;

case 'l':
case 'L':{
//if team in control pass the ball else chase the ball
if(g_SoccerPitch.UserControlledTeam.InControl()){

if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerPassBall();
}
} else{

g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
}
break;

}//end switch

here is my code snippet and its in java

Advertisement

When you receive a keyPressed event, remember that the key is now down. Do not act on the event beyond that at this point.

When you receive a KeyReleased event, remember that the key is now up. Do not act on the event beyond that at this point.

During your update step (which should happen several times per second, for simplicity perhaps simply using a timer) act on the key up/down state.

Also, be aware that, depending on the keyboard, when certains keys are held down, certain other keystrokes may not be reported. It's called "ghosting."

In addition to the potential problem of ghosting, as a courtesy, you should provide the user with the option to remap keys by function name. I.e., let the user choose the key to be held down for MoveUp, MoveDown, etc. My preference, for instance, is to use the keypad rather than AWSD, etc.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

i will implement the joypad at a later stage for now i just want to use the keyboard.so are using i should have like a while loop.for example if i press the up buttton a certain boolean is set true and i call a certain function whilst this boolean is true.

BitMaster's suggestion is that, instead of processing key press events, you get the state of the key that you're interested in as a part of your game logic and go from there.

In the project that I'm currently working on, (essentially) I have my main game loop which calls one function to process game logic and a second function to update the display. In the game logic function, I have a call to another function to process keyboard input. This keyboard processing function calls from the SFML API isKeyPressed(key) which returns the state of the particular key that I'm interested in (I believe in the Windows API it's GetAsyncKeyState()). I keep the state for that key in one variable. I make additional calls to isKeyPressed() for every key that I'm interested in and store each result in a variable so I have every key that is currently pressed. After that's done I test the variables.

If the player has pressed the key to attack, the attack function is called.

Later in the keyboard processing function...

If the player has pressed a key to move, the move function is called.

Later in the keyboard processing function...

I can also check the variables if key1 && key2 (ctrl+D for example) are pressed simultaneously and do something else.

Sorry I don't have the code handy to show a clearer example and the above isn't what I'd call good pseudo code (the actual implementation isn't very elegant either), but hopefully it gets you on the right path.

My experience for delay response in Java has always been because correct logic was written in the key events methods which is what not to do.

This topic is closed to new replies.

Advertisement