School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,419 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,509 people online right now. Registration is fast and FREE... Join Now!




[SDL]How to catch 2 KeyDowns at the same moment?

 

[SDL]How to catch 2 KeyDowns at the same moment?

Speedy_92

4 Jul, 2009 - 12:53 AM
Post #1

D.I.C Head
**

Joined: 2 Jul, 2009
Posts: 53



Thanked: 1 times
My Contributions
Hello,
I am sorry, if I don't write everything correct because English is not my native language but I'll give my best smile.gif

I have a function like this:
CODE
OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
{
    switch(sym)
    {
        case SDLK_UP:
        {
            posYPaddle2 -= 1;
            break;
        }
        case SDLK_DOWN:
        {
            posYPaddle2 += 1;
            break;
        }
        case SDLK_w:
        {
            posYPaddle1 -= 1;
            break;
        }
        case SDLK_s:
        {
            posYPaddle1 += 1;
            break;
        }
        default:
        {
            break;
        }
    }
}


It catches KeyDowns on my keyboard and my current problem is, that I cannot catch 2 Keydowns at the same moment...
I try to explain it a bit deeper:
I am trying to create a Pong-clone. For this I have set the KeyRepeat to this:
CODE
SDL_EnableKeyRepeat(1, SDL_DEFAULT_REPEAT_INTERVAL / 8);

When now Player1 presses "w" and Player2 presses a moment later "UP", the KeyRepeat of Player1 is interruptet. My aim is that both Players can press their button for the paddles at the same time.
I have thought about threads, which could solve the problem, but I wanted to hear some advice from you, how to solve it because maybe there is just a very easy solution.
I hope you understand what I mean smile.gif

Edit: Now two other solutions came in my mind: I could either take the mouse for Player 2 (but which is not my aim, cause I wanted both using the keyboard) or trying to send the KeyRepeat till KeyUp appears, but I don't know how to do the second idea.

Bye,
Speedy_92

This post has been edited by Speedy_92: 4 Jul, 2009 - 01:02 AM

User is offlineProfile CardPM
+Quote Post


stayscrisp

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 01:57 AM
Post #2

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions

Hi

I just attempted this using my engine for 2D Games and I have a slightly different problem, they can both move at the same time but if I try to switch from up to down they interrupt each other.

This only happens if the player doesn't let go of the other direction, this is something to do with my acceleration I think, but OK back on topic smile.gif

You should use some boolean values for movement, for example

CODE

case SDLK_UP: {
          Paddle1.moveUp = true;
}


etc...

This should work better as only by letting go of the key will the value become false.
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 02:27 AM
Post #3

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions

You will have to set

CODE

if(moveUp)
{
    PaddeYValue -= 1;  // or whatever you want to do
}

User is offlineProfile CardPM
+Quote Post

numeric

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 03:47 AM
Post #4

D.I.C Head
**

Joined: 12 Jan, 2009
Posts: 71



Thanked: 7 times
My Contributions
You cold probably use if statements as an alternative - seeing as a program will act on more than one if statement in a loop without exiting it'll allow actions to take place while more than one key is pressed.



This post has been edited by numeric: 4 Jul, 2009 - 03:51 AM
User is offlineProfile CardPM
+Quote Post

Speedy_92

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 04:37 AM
Post #5

D.I.C Head
**

Joined: 2 Jul, 2009
Posts: 53



Thanked: 1 times
My Contributions
Thanks first smile.gif

And I guess I understand you:
So in my OnKeyDown() function is a switch statement in which I just set the booleans and after it I move the paddles, depending on which direction and in my OnKeyUp() function I just change the boolean for the paddle to false smile.gif
I hope I am right, but it sounds logical and should word wink2.gif

Thanks again,

Speedy_92
User is offlineProfile CardPM
+Quote Post

Speedy_92

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 05:07 AM
Post #6

D.I.C Head
**

Joined: 2 Jul, 2009
Posts: 53



Thanked: 1 times
My Contributions
Just wanted to tell you that I have tested it aaaaaand.... IT WORKS smile.gif

Thank you.

Speedy_92
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

4 Jul, 2009 - 05:07 AM
Post #7

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions
Yup, heres a snippet from my code

CODE

          case SDL_KEYDOWN:
                switch (event.key.keysym.sym) {
                    case SDLK_UP: {
                        player.m_bMoveUp = true;
                        break;
                    }
                        
                    case SDLK_DOWN: {
                        player.m_bMoveDown = true;
                        break;
                    }
                    
                    case SDLK_w: {
                        player2.m_bMoveUp = true;
                    }
                        
                    case SDLK_s: {
                        player2.m_bMoveDown = true;
                    }
                    
                        
                    }
                    break;

            case SDL_KEYUP:    
                switch (event.key.keysym.sym) {    
                    
                    case SDLK_UP: {
                        player.m_bMoveUp = false;
                        break;
                    }
                        
                    case SDLK_DOWN: {
                        player.m_bMoveDown = false;
                    }
                        
                    case SDLK_w: {
                        player2.m_bMoveUp = false;
                    }
                        
                    case SDLK_s: {
                        player2.m_bMoveDown = false;
                    }
                        
                }
            
                
        }


Hope that helps smile.gif

*edit* didn't see your second post there smile.gif Glad it works!

This post has been edited by stayscrisp: 4 Jul, 2009 - 05:07 AM
User is offlineProfile CardPM
+Quote Post

Pontus

RE: [SDL]How To Catch 2 KeyDowns At The Same Moment?

6 Jul, 2009 - 11:05 AM
Post #8

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 569



Thanked: 8 times
Dream Kudos: 275
My Contributions
What I use is this:

CODE
while(SDL_PollEvent(&event))
{
if(event.key.keysym.sym==SDLK_UP)
{
  //do this
}
if(event.key.keysym.sym==SDLK_DOWN)
{
  //do that
}
}


This post has been edited by Pontus: 6 Jul, 2009 - 11:06 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:57PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month