Saving keypresses into a bool array without using macro
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]
I have not much experience, but what are you setting the char array to?
If it is always either one or the other, a bool array is feasible.
If it is always either one or the other, a bool array is feasible.
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?
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?
You could use a table to map controller definitions:
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.
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.key ) ) { *myKeys.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.
//unsigned char szKeyStates[256];//bool bKeyStates[256];/*EDITED*/for(int nIndex = 0; nIndex < 256; ++nIndex){ bKeyStates[nIndex] = KeyDown(szKeyStates, nIndex) != 0;}
Edit:
I would recommend using a system similiar to hplus0603's.
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.
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.
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.
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.
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.
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
nehe.gamedev.net has a not so bad keyboard handler for anyone new to input needs, it uses the WndProc just FYI.
Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement