Public Group

# Stupid Win API question (SOLUTION ACQUIRED)

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Catch WM_KEYDOWN/WM_KEYUP in your window message handler.

##### Share on other sites
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 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 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 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 on other sites
Quote:
 Original post by SquirmThere 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!!!!!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×