Jump to content
  • Advertisement
Sign in to follow this  
-justin-

Stupid Win API question (SOLUTION ACQUIRED)

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

Alright so I made some code where a character picks up an object; and I also have some code for him to throw that object. Both of these are initiated by pressing a key. However using the win api (calling GetAsyncKeyState) it initiates both of the two things at the same time! Is there anyway to have the keystate be sent only once (even if held down) in the win api? If it isn't possible in WIN API then let me know if it is in directinput. I don't mind using dinput I just would prefer not to at this point. Thanks :) [Edited by - -justin- on July 27, 2005 1:29:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
oh yea... i meant to put somethin bout that in there

i'd prefer not to use it in the windows message handler :-D

Share this post


Link to post
Share on other sites
#define KEYDOWN(key) ((GetAsyncKeyState(key) & 0x8000) ? TRUE : FALSE)

If the least significant bit is set in the WORD returned form GetAsyncKeyState then the key was pressed after the previous call to the method, if the most significant bit is set then the key is down.

You could process the WM_KEYDOWN message.

LRESULT CALLBACK WindowProc(...)
{
switch(uMsg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP:
// PICKUP ITEM
break;
case VK_DOWN:
// DROP ITEM
break;
}
return 0;
} break;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

Share this post


Link to post
Share on other sites
not tryinig to be mean but i know that already...

the thing is i don't want to define multiple keyboard keys; i'd like to have one key do both pick-up/drop an item;

there has to be a way to make it so that even if you hold down a key it doesn't send multiple calls...

Share this post


Link to post
Share on other sites
There are better ways to do it than this. They are a design decision. This will do it for now and you can worry about better designs another day:


enum Action
{
Neutral,
Pickup,
Got,
Throw
};

Action act = Neutral;

loop()
{
if (keydown(X))
{
if (act == Neutral)
{
picksomethingup();
act = Pickup;
}
else if (act == Got)
{
throwsomething();
act = Throw;
}
}
else
{
if (act == Throw)
act = Neutral;
else if (act == Pickup)
act = Got;
}
}




Basically, you have a state, which is changed by the use pushing the key. What mechanism you use to represent the states is up to you - the enumerate above is nice and simple for simple situations, and only gets unweildly if you have lots of states or more complicated transitions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Squirm
There are better ways to do it than this. They are a design decision. This will do it for now and you can worry about better designs another day:

*** Source Snippet Removed ***

Basically, you have a state, which is changed by the use pushing the key. What mechanism you use to represent the states is up to you - the enumerate above is nice and simple for simple situations, and only gets unweildly if you have lots of states or more complicated transitions.


beautiful; something like this came to mind but i wasn't able to think of how to do it :)

THANKS!!!!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!