Jump to content
  • Advertisement
Sign in to follow this  
Andrew Blem

How to Detect Multiple KeyPressing of buttons on the keyboard

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

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by kseh

Share this post


Link to post
Share on other sites
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!