Keypressing at the same time

Started by
5 comments, last by evolutional 19 years, 7 months ago
Hi, I'm just trying to make a box move with gdi and c++. I want to make it so when I push up and left to go diagonal. I have it now it the Window procedure. I saw a thread here just the other day about that but I can't find it. I was thinking about using direct input but I can't find any good tutorials. If anyone has any good info it will be greatly appreciated, thanks.
Advertisement
Look up GetAsyncKeyState(). It will simply give you the status for any key at any time.

MSDN
Is there a better way than to check if the keys are pressed than putting it in the Window Procedure. It seems once I press a key and then press another one the first one isn't pressed anymore. And what about calling the WM_PAINT message after a key has been pressed with InvalidateRect(), is there a better way of doing this?
You could store the keypresses in a char keys[256] array, so that the WM_KEYDOWN/UP message sets the byte to 1 or 0 respectively. This way, you process the key states after the message loop, essentially keeping your own buffer of input. You can query any of the buffered states by checking the array; so if (keys[VK_DOWN] && keys[VK_RIGHT]) should report a diagonal press.
As the other guy said, look up GetKeyState or GetAsyncKeyState on MSDN. Both functions will return 1 in the high-word if the key is down and 0 if it's up. It's as easy as that!
Of course, an array of bool keys[?] and updating in WM_KEYDOWN are better if you want to check the keystate every frame.
Killers don't end up in jailThey end up on a high-score!
This is how I am doing it, but it seems when I press a key and hold it down, and then press another one the first key isnt pressed any more. If I made an array wouldn't that do the same thing?

void CheckKeys(void){	if(GetAsyncKeyState(VK_UP)){		BoxY -= 1;		BoxHeight -= 1;	}	if(GetAsyncKeyState(VK_DOWN)){		BoxY += 1;		BoxHeight += 1;	}	if(GetAsyncKeyState(VK_LEFT)){		BoxX -= 1;		BoxWidth -= 1;	}	if(GetAsyncKeyState(VK_RIGHT)){			BoxX += 1;		BoxWidth += 1;	}}case WM_PAINT:			HDC hdc;			PAINTSTRUCT ps;			RECT Client;			hdc = BeginPaint(hWnd, &ps);			GetClientRect(hWnd, &Client);			FillRect(hdc, &Client, (HBRUSH) (COLOR_WINDOW+1));  			Rectangle(hdc, BoxX, BoxY, BoxWidth, BoxHeight);			char buf[256];			sprintf(buf, "X=%d \n Y=%d", BoxX, BoxY);			TextOut(hdc, 10, 10, buf, strlen(buf));			EndPaint(hWnd, &ps);			return 0;		break;		case WM_KEYDOWN:			CheckKeys();			InvalidateRect(hWnd, NULL, NULL);			return 0;		break;
Here's a simplified snippet of code from Manta-X

///// From WndProccase WM_KEYDOWN:{	m_gameData->keys[wParam] = true;return 0;}case WM_KEYUP:{	m_gameData->keys[wParam] = false;	return 0;}/////// In game loopif (m_gameData->keys[ VK_RIGHT ] == true){   m_gameData->playerShip->position.x += 1;}if (m_gameData->keys[ VK_UP ] == true){	m_gameData->playerShip->position.z -= 1;}



I get full motion with that - I can hold keys down together and it works fine.

This topic is closed to new replies.

Advertisement