Sign in to follow this  

Saving keypresses into a bool array without using macro

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

I am using Direct Input 8.1 to process my keyboard input. At the moment I use this method to detect keypresses: #define KEYDOWN(name, key) (name[key] & 0x80) char KeyboardState[256]; if(FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState))) { return; } if(KEYDOWN(KeyboardState, DIK_A)) { //do something } However, for my needs it would be much more useful just to have the current keyboard state represented as a bool array. It would be messy and time consuming to have to write out: if(KEYDOWN(KeyboardState, DIK_A)) { keyboardkey[0] = true; } if(KEYDOWN(KeyboardState, DIK_B)) { keyboardkey[1] = true; } etc. How would I go about converting the keyboard state array into an array of bools? [Edited by - CIJolly on March 6, 2006 9:07:22 PM]

Share this post


Link to post
Share on other sites
The problem is that I can't figure out what the char array is being filled with. I just know to send a char array as the argument for GetDeviceState, and then use it in conjunction with the KEYDOWN macro to detect if a key is down.
I don't actually know what data it is storing in the array, although I assumed it would be a char corresponding to the key pressed. I tried checking online and in the DirectX documentation, but it just kept going on about unsigned bytes.

There must be some simple loop that I can use to change whatever Direct Input puts in the char array into a bool array, surely?

Share this post


Link to post
Share on other sites
You could use a table to map controller definitions:


bool forwardDown;
bool leftDown;
bool rightDown;

struct KeyMapping {
DWORD key;
bool * flag;
};

KeyMapping myKeys[] = {
{ DIK_W, &forwardDown },
{ DIK_A, &leftDown },
{ DIK_D, &rightDown },
};

for( size_t i = 0; i < sizeof(myKeys)/sizeof(myKeys[0]); ++i ) {
if( KEYDOWN( KeyboardState, myKeys[i].key ) ) {
*myKeys[i].flag = true;
}
}


Btw: the char array contains the key status byte for each key; the 0x80 bit tells you whether the key is down or not. It's really just a pretty shallow wrapper around WM_KEYDOWN. In fact, you're probably better off (lower latency, simpler code) to just do keyboard handing with WM_KEYDOWN instead of using DirectInput.

Share this post


Link to post
Share on other sites
Just out of curiousity, what is the advantage of using Direct Input to process keypresses over handling the WM_KEYUP and WM_KEYDOWN messages in your WndProc? I just have a bool array that I set values to true and false in response to the messages. Never really looked at Direct Input.

Share this post


Link to post
Share on other sites
Thanks for the help everyone. The reason I am using Direct Input is pretty much just because the game programming book I learnt DirectX out of described how to use it. I assumed it would be quicker or more efficient than letting the wndproc handle it.

The reason I needed the keypresses in a bool array is because I never need to know the name of the key which has been pressed. The user presses keys to map them to various functions, and the key they pressed is recorded as an int pointing to the corresponding part of the bool array.

It made sense to do it this way because then I can feed other input into the same system, such as is the LMB down, is the angle between my ship and the mouse pointer < 179, is the ships health lower than 10%, etc.

It also means the AI will be able to control ships using the exact same system, by activating various actions when appropriate conditions are met or in response to patterns.

Share this post


Link to post
Share on other sites
Using a bool array instead of char should also work.
If a key is pressed, the corresponding char will be set to the value of 0x80 or 128.
By using 2 char buffers, you can even determine when a key is pressed the first time, held down, or being released.

Share this post


Link to post
Share on other sites
EasilyConfused: Direct Input is just another layer on top of the WndProc, so basically you're giving up speed for pretty much nothing (not much speed so its not a huge deal). Direct Input is being killed off, there will be a replacement for modern "joysticks" (aka controllers) but I believe it will not handle keyboard at all, leaving that to the WndProc.

nehe.gamedev.net has a not so bad keyboard handler for anyone new to input needs, it uses the WndProc just FYI.

Mike

Share this post


Link to post
Share on other sites

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