Sign in to follow this  
alex_myrpg

DirectInput (keyboard)

Recommended Posts

Hi, My question is how can I get the keys *just* pressed on the last poll using DirectInput? Currently, I am using GetKeyboardState() to return a KeyboardState object and I can get the keys that are pressed at that time of polling the device, but I can't get the keys that were pressed on this poll, but not last time (i.e. KeyDown event detection). This wouldn't be so much of a problem if the KeyboardState constructor were available, the MemberwiseClone function were available, the Item property wasn't read-only, or in general if the object behaved like a normal array. Please let me know how I can detect keys that have just been pressed using DirectInput. Thanks in advance. Alex

Share this post


Link to post
Share on other sites
Have you considered using buffered input rather than polling?

I've always preferred that mechanism as it just fills up a queue with the keyboard events, then (when you wish to check) you can read back the queue and find out what happened. When you read back that event disappears, such that your subsequent read-backs will only show you events that have happened since the last poll - which is what you want [smile]

Alternatively, if you stick with your current code, I think your only option is to go through and make a copy of each state and then calculate deltas based on what you had previously stored and the state you've now received.

hth
Jack

Share this post


Link to post
Share on other sites
Thanks for your reply. :) Buffered input seems like the best choice, but I don't know how to do it in Managed DirectX. If it's not possible in MDX then I suppose I'll try the other method, though it may bit a bit slow (or perhaps not?). Anyway, please let me know how you would do buffered input in managed DX.

Alex

Share this post


Link to post
Share on other sites
Unfortunately I'm a "Native DirectX" programmer, so I don't know that much about MDX...

The code that I use in C++ is:
DIPROPDWORD prop;
prop.diph.dwSize = sizeof( DIPROPDWORD );
prop.diph.dwHeaderSize = sizeof( DIPROPHEADER );
prop.diph.dwObj = 0;
prop.diph.dwHow = DIPH_DEVICE;
prop.dwData = (DWORD)iBufferSize; //iBufferSize is how many events to store

//pKeyb is a DirectInput Device
if( FAILED( pKeyb->SetProperty( DIPROP_BUFFERSIZE, &prop.diph ) ) )

//This line retrieves the current buffer. Performed each frame in our engine.
//'data' is a DIDEVICEOBJECTDATA[] array of size 'iBufferSize'
DWORD ret = pKeyb->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), data, &dwElements, 0 );

I mention this code as you might be able to spot some of the same functions in MDX and try a simple conversion..

As for the performance of calculating deltas... I dont know how MDX does it, but the keyboard state in C++ is just an array of states (iirc), so copying that and storing it really shouldn't hurt you too much.

hth
Jack

Share this post


Link to post
Share on other sites
duh...found it now :) - GetBufferedData - that's all i need, so simple in MDX. thanks for the code though, it helped me find and understand getting buffered input data. i'll let you know how it goes...

Share this post


Link to post
Share on other sites
well i've almost got there now :) just one problem - in fullscreen mode it crashes the computer and i can't tell the exact error because of this. i think it could be something to do with the cooperative level, but here's the initialization code anyway:

'Create keyboard device
dinKeyboard = New DirectInput.Device(SystemGuid.Keyboard)
dinKeyboard.SetDataFormat(DeviceDataFormat.Keyboard)
dinKeyboard.SetCooperativeLevel(cTarget, CooperativeLevelFlags.Foreground Or CooperativeLevelFlags.Exclusive Or CooperativeLevelFlags.NoWindowsKey)
dinKeyboard.Properties.BufferSize = 8

'Acquire keyboard
dinKeyboard.Acquire()

please tell me the correct init process. thanks.

Share this post


Link to post
Share on other sites

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