7 Replies - 684 Views - Last Post: 06 July 2009 - 12:05 PM Rate Topic: -----

#1 Speedy_92  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-July 09

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

Posted 04 July 2009 - 01:53 AM

Hello,
I am sorry, if I don't write everything correct because English is not my native language but I'll give my best :)

I have a function like this:
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:
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 :)

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: 04 July 2009 - 02:02 AM


Is This A Good Question/Topic? 0
  • +

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

#2 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

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

Posted 04 July 2009 - 02:57 AM

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 :)

You should use some boolean values for movement, for example

case SDLK_UP: {
		  Paddle1.moveUp = true;
}



etc...

This should work better as only by letting go of the key will the value become false.
Was This Post Helpful? 1
  • +
  • -

#3 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

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

Posted 04 July 2009 - 03:27 AM

You will have to set

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


Was This Post Helpful? 0
  • +
  • -

#4 numeric  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 77
  • Joined: 12-January 09

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

Posted 04 July 2009 - 04:47 AM

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: 04 July 2009 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 Speedy_92  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-July 09

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

Posted 04 July 2009 - 05:37 AM

Thanks first :)

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 :)
I hope I am right, but it sounds logical and should word ;)

Thanks again,

Speedy_92
Was This Post Helpful? 0
  • +
  • -

#6 Speedy_92  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-July 09

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

Posted 04 July 2009 - 06:07 AM

Just wanted to tell you that I have tested it aaaaaand.... IT WORKS :)

Thank you.

Speedy_92
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

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

Posted 04 July 2009 - 06:07 AM

Yup, heres a snippet from my 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 :)

*edit* didn't see your second post there :) Glad it works!

This post has been edited by stayscrisp: 04 July 2009 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#8 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

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

Posted 06 July 2009 - 12:05 PM

What I use is this:

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: 06 July 2009 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1