Archived

This topic is now archived and is closed to further replies.

wazoo69

Easy DInput keyboard translation??

Recommended Posts

Hi, I''m trying to make a VERY simple bare-bones console, and all I want to do, is to throw into a global character buffer the key corresponding to the DIK value received by DirectInput.. Is there something I''m missing here?? I want to stick to using DirectInput rather than Windows to handle grabbing the key (so I don''t want to use a WM_CHAR event)...has anyone had any experience with this?? thanks in advance, Wazoo

Share this post


Link to post
Share on other sites
This is Copy&Paste from my game... Tell me if it''s wrong

UCHAR InputKeyStatus[MAX_INPUT_STATES];
HWND main_window_handle;
HINSTANCE main_instance;
LPDIRECTINPUT lpdi = NULL; // dinput object
LPDIRECTINPUTDEVICE lpdikey = NULL; // dinput keyboard

#define IS_KEY_DOWN(n) (OldInputKeyStatus[n] & 0x80) //macro

int DInput_Init_Keyboard(void)
{
//create DirectInput object
if(FAILED(DirectInputCreate(main_instance,DIRECTINPUT_VERSION,&lpdi,NULL)))
return(-1);

// this function initializes the keyboard device

// create the keyboard device
if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)!=DI_OK)
return(0);

// set cooperation level
if (lpdikey->SetCooperativeLevel(main_window_handle,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
return(0);

// set data format
if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)
return(0);

// acquire the keyboard
if (lpdikey->Acquire()!=DI_OK)
return(0);

// return success
return(1);

} // end DInput_Init_Keyboard

void DInput_Release_Keyboard(void)
{
// this function unacquires and releases the keyboard

if (lpdikey)
{
lpdikey->Unacquire();
lpdikey->Release();
lpdikey=NULL;
} // end if

} // end DInput_Release_Keyboard

int DInput_Read_Keyboard(void)
{
HRESULT result;
// this function reads the state of the keyboard

if (lpdikey)
{
memcpy(&OldInputKeyStatus,&InputKeyStatus,sizeof(InputKeyStatus));

result=lpdikey->GetDeviceState(256, (LPVOID)InputKeyStatus);
if(result!=DI_OK)return(0);
}
else
{
// keyboard isn''t plugged in, zero out state
memset(InputKeyStatus,0,sizeof(InputKeyStatus));

// return error
return(0);
} // end else

// return sucess
return(1);

} // end DInput_Read_Keyboard

Then use

DInput_Read_Keyboard() to refresh input status

and the macro IS_KEY_DOWN to see what the inputs are

if(IS_KEY_DOWN(VK_A)){Buffer[NextCharacter]=''a'';}

You could copy the input states to a second buffer to check if a key has been pressed (was up and now is down) during the last refresh.

Share this post


Link to post
Share on other sites
Voxelsoft: You can''t be Serious!...there HAS to be a better way than that!!!

That would mean about 26 If statments!!

I use your method for reading in keys during the game
(ie. if DIK_LEFT, then move ship left..)

but to output text would be just unbearable..

I found a few articles on GameDev which address this issue, however I STILL haven''t been able to find just HOW to pass the
current DIK flag into the ScanC2Ascii function...

(ie. HOW do you get send the current key hit WITHOUT translating it??)

thanks Voxelsoft, but I''m trying to find an easier way (I''ll only use your method if I have to...)
Wazoo

Share this post


Link to post
Share on other sites
So take it another step. The method mentioned is great for when you have 4 move keys, fire, and jump. You want the full text. The If statement is basically hitting the array OldInputKeyStatus.

Using C how can you get OldInputKeyStatus to just cough up it''s current keys?

My first thought is this:
Make an array that holds all the characters you want to accept, any parts of the array you don''t want, put in a /0. AND that with OldInputKeyStatus to produce a 3rd string that is either /0 characters or the letter that was in your first array.

Nope, scratch that, you''re still going to have to loop through the 3rd string (an if statement of the loop). If you''re clever you can remove the if statement from the "If this char is not a /0, keep it".

Personally, for my console state, I would drop the DirectInput, just go with the OS keyboard functions.


Dustin

Share this post


Link to post
Share on other sites
Dustin_00: hmmmm well a quick cheap way would be to do a gigantic for loop through the keyboard state array, and return
any key that is true when &''d with 0x80??

Okay enough of it! I''ll just use the WM_CHAR keyboard event,
and set a global flag if I''m in console mode or not..if I''m not, then just move to my DI routine...if I am, then send the key to the Console buffer...

Thanks for the help guys!

Wazoo

Share this post


Link to post
Share on other sites
Sounds like a good plan. The windows events are (relatively) slow, but a console doesn''t need to react like it''s in the middle of a shoot-out and the user is pushing 8 keys at once.

Dustin

Share this post


Link to post
Share on other sites
Dustin_00: Yeah...Hopefully..I''m planning on it
acting as a console during a multiplayer game...so during the action I want players to be able to send messages to each other during battle..

But I''ll test it out and see what happens...

thanks dude,
Wazoo

Share this post


Link to post
Share on other sites