Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simultaneous input

Started by Firecore Sep 1, 2008 at 7:20 AM 5 replies 1.3k views
Original Post
Firecore
Firecore
Hi guys. I have input processing like this. How can I arrange it so that I can have simultaneous input? Basically, whats happening in the game is once I press a key to move an object, and then press another key to move the second object, the second object does not move and only the first does. The second object only moves after I let go of both keys and press the move key for the second object. The question is, how can I make it so that both objects will move when I press both keys at once?

LRESULT D3DPong::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
		{
			// Exit when escape is pressed
			m_bShouldExit = true;
		}
		else if(wParam == VK_RETURN)
		{
			// Toggle windowed / fullscreen
			m_thePresentParams.Windowed = !m_thePresentParams.Windowed;
			ResetDevice();
		}
		else if(wParam == VK_LEFT)//1ST PADDLE - LEFT
		{
			m_paddleOnePos.x -= m_fPaddleSpeed;
			if(m_paddleOnePos.x < -(m_coordWidth/2))
				m_paddleOnePos.x = -(m_coordWidth/2);
		}
		else if(wParam == VK_RIGHT)//1ST PADDLE - RIGHT
		{
			m_paddleOnePos.x += m_fPaddleSpeed;
			if(m_paddleOnePos.x > (m_coordWidth / 2 - m_paddleTexW))
				m_paddleOnePos.x = (m_coordWidth / 2 - m_paddleTexW);
		}
		else if(wParam == 'A')//2ND PADDLE - LEFT
		{
			m_paddleTwoPos.x -= m_fPaddleSpeed;
//			m_paddleTwoPos.x -= m_paddleTexW;
			if(m_paddleTwoPos.x < -(m_coordWidth/2))
				m_paddleTwoPos.x = -(m_coordWidth/2);
		}
		else if(wParam == 'D')//2ND PADDLE - RIGHT
		{
			m_paddleTwoPos.x += m_fPaddleSpeed;
			if(m_paddleTwoPos.x > ((m_coordWidth/2) - m_paddleTexW))
				m_paddleTwoPos.x = ((m_coordWidth/2) - m_paddleTexW);
		}
		break;
	}

	// Pass all messages along to parent class
	return D3DWindow::WndProc(uMsg, wParam, lParam);
}

BLiTZWiNG
BLiTZWiNG
I'm pretty rusty at Windows and C now, but your code looks right to me. The only other thing I can think of is that some cheaply made keyboards controllers assign keys in groups, using the same codes for different keys they (hope) wont be pressed at the same time. Like on some keyboards I've had you couldn't press W and A together. You can probably imagine how much that sucked.

Have you tried using a few different keys to see if it works?
Optus
Optus
Is it because you're using "else if" blocks for each key conditional? This guarantees that only one block of code will be executed (The first that tests true in your series). I think this would definitely prevent you from getting "simultaneous" input.

Change it to a series of plain if blocks, like so:

if(wParam == VK_ESCAPE){	// Exit when escape is pressed	m_bShouldExit = true;}if(wParam == VK_RETURN){	// Toggle windowed / fullscreen	m_thePresentParams.Windowed = !m_thePresentParams.Windowed;	ResetDevice();}if(wParam == VK_LEFT)//1ST PADDLE - LEFT{	m_paddleOnePos.x -= m_fPaddleSpeed;	if(m_paddleOnePos.x < -(m_coordWidth/2))		m_paddleOnePos.x = -(m_coordWidth/2);}if(wParam == VK_RIGHT)//1ST PADDLE - RIGHT{	m_paddleOnePos.x += m_fPaddleSpeed;	if(m_paddleOnePos.x > (m_coordWidth / 2 - m_paddleTexW))		m_paddleOnePos.x = (m_coordWidth / 2 - m_paddleTexW);}if(wParam == 'A')//2ND PADDLE - LEFT{	m_paddleTwoPos.x -= m_fPaddleSpeed;//			m_paddleTwoPos.x -= m_paddleTexW;	if(m_paddleTwoPos.x < -(m_coordWidth/2))		m_paddleTwoPos.x = -(m_coordWidth/2);}if(wParam == 'D')//2ND PADDLE - RIGHT{	m_paddleTwoPos.x += m_fPaddleSpeed;	if(m_paddleTwoPos.x > ((m_coordWidth/2) - m_paddleTexW))		m_paddleTwoPos.x = ((m_coordWidth/2) - m_paddleTexW);}
Noobico
Noobico
i can just tell you how iam doing it in my pong game:

i handle the wm_keydown like that:
Pseudo-Code:
bool keys[MAX_INDEX];mywindowproc(...){...    case WM_KEYDOWN:        keys[wParam] = true;        break;    case WM_KEYUP:        keys[wParam] = false;...}


In my gameloop i check just the bool and handle the keys that way.
In your case i would check for lowercase keys like 'a' instead of 'A' but iam not sure if it makes a difference.

Otherwise your code looks fine.

I suspect that if you first press a key and then another one (while still pressing the first) the WM_KEYDOWN might react in a different than you think. you could debug what WM_KEYDOWN messages you get if you press more than one key at the same time.


BLiTZWiNG
BLiTZWiNG
Quote:
Original post by Optus
Is it because you're using "else if" blocks for each key conditional? This guarantees that only one block of code will be executed (The first that tests true in your series). I think this would definitely prevent you from getting "simultaneous" input.

Change it to a series of plain if blocks, like so:

*** Source Snippet Removed ***


Only one message gets processed at a time, so no the else if construct prevents the system from doing 20 everytime instead of the least number possible.

IIRC, checking for 'A' is correct over checking for 'a', but yes, have you set a break point to see if the other keys are getting through?

scottrick49
scottrick49
Quote:
Original post by BLiTZWiNG
The only other thing I can think of is that some cheaply made keyboards controllers assign keys in groups, using the same codes for different keys they (hope) wont be pressed at the same time. Like on some keyboards I've had you couldn't press W and A together. You can probably imagine how much that sucked.

Have you tried using a few different keys to see if it works?


!!! I have had this problem with a cheap ($15) usb keyboard I use sometimes on my laptop. The problem doesn't happen when I use my normal laptop keyboard. I just assumed my keyboard was faulty in some way. Is there any work around for this problem? Just buy a new keyboard?
scottrick49
frob
frob
Quote:
Original post by scottrick49
Quote:
Original post by BLiTZWiNG
The only other thing I can think of is that some cheaply made keyboards controllers assign keys in groups, using the same codes for different keys they (hope) wont be pressed at the same time. Like on some keyboards I've had you couldn't press W and A together. You can probably imagine how much that sucked.

Have you tried using a few different keys to see if it works?


!!! I have had this problem with a cheap ($15) usb keyboard I use sometimes on my laptop. The problem doesn't happen when I use my normal laptop keyboard. I just assumed my keyboard was faulty in some way. Is there any work around for this problem? Just buy a new keyboard?


It happens with most keyboards, not just cheap ones. The keyboard manufacturers generally avoid common combinations, but they still have to avoid something.


There are lots of posts on the topic on the forums, such as this one, among several hundred more available through the forum search.

Basically it is keyboard specific, some key combinations just won't work. Generally you want no more than two keys. Adding a third key generally causes problems, and 4+ keys will give trouble on nearly every keyboard out there.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.