DInput keypresses

Started by
3 comments, last by Houdini 24 years ago
Is there an easier way to detect keypresses in between frames using direct input than to use a for loop and manually check each key? It seems like a huge waste of processing power to have a 0 to 100 for loop just to check for keypresses in between every single frame...
- Houdini
Advertisement
A loop ?
I can hardly imagine why you should use a 1..100 loop to check your keys...

Either you''re using immediate data mode and are calling GetDeviceState(), in which case you''ll just get an array containing the status of all keys, then you''ll just check the keys needed by you game, ex.

unsigned char keys[256];
int key_left = DIK_LEFT;
int key_right = DIK_RIGHT;

DIKeyboard->GetDeviceState(keys, sizeof(keys));
if(keys[key_left])
some_x -= 1;
if(keys[key_right])
some_x += 1;

...or you''re using buffered mode, where you''ve got something like this:

DIDEVICEOBJECTDATA DIDObjectData[128];
DWORD numevents;
unsigned char keys[256];
int count;

DIKeyboard->GetDeviceData(sizeof(DIDObjectData), DIDObjectData, &numevents, 0);
for(count = 0; count < numevents; count++)
keys[DIDObjectData[count].dwOfs] = DIDObjectData[count].dwData & 128;

In both cases, a loop makes no sense to me. Perhaps you could explain in some more detail or even post part of your code ?

-Markus-
Well, I had the same problem just a few weeks ago. As far as I know, there''s no way around it. I''ve had to check for each key one by one as well (in a loop of course). I think the whole point of DirectInput with a keyboard device is to turn the keyboard into a HUGE gamepad. Just check for the keys you want, and do what you need to do with the input.

Hope this helps!
I''m with Markus. I personally use buffered input for the keyboard. You don''t do ANY processing of keyboard input (beyond calling GetDeviceData) unless there has been some. If you are worried about states, keep track of that internally -- (dumb example):

bool forwardKeyPressed;

void onForwardKeyPressed()
{
forwardKeyPressed = true;
}

void onForwardKeyReleased()
{
forwardKeyPressed = false;
}

Then you have a very easy time during the app loop:

if ( forwardKeyPressed )
moveForward();

Yes, this is very dumb and simplistic, but that''s kinda the point.

i came up with a simplistic system to handle keyboard inputs
i setup buffered input mode, and then save a pointer to a function with parameters of a DWORD state, a DWORD which_key, and a void* passed to the function by the caller
this is then used as a callback function, where my little encapsulating class has an update function called which is passed the void pointer
the callback is then called for each keypress in order since the last update
very simple, but a very easy and intuitive system that resembles WndProc
-PoesRaven

This topic is closed to new replies.

Advertisement