Stupid Win API question (SOLUTION ACQUIRED)

Started by
5 comments, last by -justin- 18 years, 8 months ago
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]
Advertisement
Catch WM_KEYDOWN/WM_KEYUP in your window message handler.
oh yea... i meant to put somethin bout that in there

i'd prefer not to use it in the windows message handler :-D
#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);}
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...
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.
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!!!!!

This topic is closed to new replies.

Advertisement