Sign in to follow this  

Keypressing at the same time

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

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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


Share this post


Link to post
Share on other sites
Here's a simplified snippet of code from Manta-X



///// From WndProc


case WM_KEYDOWN:
{
m_gameData->keys[wParam] = true;
return 0;
}
case WM_KEYUP:
{
m_gameData->keys[wParam] = false;
return 0;
}



/////// In game loop

if (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.

Share this post


Link to post
Share on other sites

This topic is 4859 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this