# DirectInput (keyboard)

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 Deviceif( 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-&gt;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 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 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.

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632993
• Total Posts
3009757
• ### Who's Online (See full list)

There are no registered users currently online

×