keyboard GetDeviceData (buffered) never releases keys?

Started by
3 comments, last by Mick is Stumped 11 years, 6 months ago
Bottom line using the buffered methods GetDeviceData buffers presses but not releases!? Same with the laptop keyboard and a USB keyboard.

What could be going on here? Edit controls etc work just fine.

Disclaimer: the keyboard is part of a generic controller framework, and it is used for gamey inputs so I am not sure why Direct Input is unsuitable for that (as is often proclaimed; read: so please spare everyone the lecture) though raw input will probably be looked into as an alternative if it can distinguish between multiple keyboards. Not sure what to do if this won't work sad.png

I remember having issues with buffered keyboard for "ghosting" in the past but nothing this dramatic.
Advertisement
I found this (http://www.flipcode.com/archives/DirectInput_Example.shtml) with a comment that suggests releases are not buffered:

if ((key_pressed[0].dwData & 0x80) ? 1 : 0) // only key-down events are reported
{
keyevent[key_pressed[0].dwOfs] = true;
}


Which seems to be directly contradicted by this (http://msdn.microsoft.com/en-us/library/windows/desktop/ee416238%28v=vs.85%29.aspx) quoted below because microsoft.com links have a very short shelf life...

[quote=Buffered Keyboard Data]o retrieve buffered data from the keyboard, you must first set the buffer size (see Device Properties). This step is essential because the default size of the buffer is 0.

You must also declare an array of DIDEVICEOBJECTDATA structures. This array can have up to the same number of elements as the buffer size. You do not have to retrieve the entire contents of the buffer with a single call. You can have just one element in the array and retrieve events one at a time until the buffer is empty.

After acquiring the keyboard device, you can examine and flush the buffer at any time by using the IDirectInputDevice8::GetDeviceData method. (See Buffered and Immediate Data.)

Each element in the DIDEVICEOBJECTDATA array represents a change in state for a single key-that is, a press or release. Because Microsoft DirectInput gets the data directly from the keyboard, any settings for character repeat in Control Panel are ignored. This means that a keystroke is counted only once, no matter how long the key is held down.

You can determine which key an element in the array refers to by checking the dwOfs member of the DIDEVICEOBJECTDATA structure against the DirectInput Keyboard Device. (See also Interpreting Keyboard Data.)

The data for the change of state of the key is located in the dwData member of the DIDEVICEOBJECTDATA structure. Only the low byte of dwData is significant. The high bit of this byte is set if the key was pressed, and it is clear if it was released. In other words, the key was pressed if (dwData & 0x80) is nonzero.[/quote]

I don't know why the world puts up with such a ghetto corporation. Oh yeah all of the others make the MS look like a saint nowadays and non-corporations don't have an advertising budget :(

Any ideas?
In my opinion there is no contradiction here. The MSDN is quite specific about how the dwData should be interpreted.

The code you presented works as described = only key-down events are reported. Why is that? Because "(key_pressed[0].dwData & 0x80) ? 1 : 0" results as 0 when the key is released which is also equal to if(0), which means that your code isn't run when the key is released.

I think that there is a logical flaw in your code.

Best regards!
^Hi, actually that's not my code. But if you do the experiment you will find that the condition in it is never false. I think the comment is just announcing the expected behavior. I could be wrong but the down events also appear to repeat. Which seems really counter to how DirectInput should work as there is nothing "direct" about translated input.

It seems like the person (lets give MS the benefit of doubt) who programmed the buffered side of the system keyboard device was just a world class twit and MS in its supreme irresponsibility never fixed it.

At any rate I describe a fix here (http://xboxforums.cr...s/t/108722.aspx) that basically just fills a buffer with the downed keys and calls GetAsyncKeyState until the key is lifted. In theory a down event could get timestamped before a previous up event (especially with the repeating behavior; though you could overwrite the timestamps too) but other than that its not so awful for buffered keyboard input. And its the only non-trivial specialization I had to do with for a system that exposes controllers as completely abstract beasts. In theory I could finish Super Mario Bros. with the nobs and mute button on a little speaker I have that for some reason shows up as a Direct Input controller. It was really annoying having to setup a DIK to VK translation table though.

If you are implementing input handlers on a subsystem basis you really don't have to pick a subsystem. It's probably the best that can be done for Direct Input and probably should work without a hitch if the buffer readback loop is of sufficient resolution. In other words there are plans to offer end users more than one keyboard option in my case.
Ok faithful readers and anyone in the same boat....

Over in the xna forums (http://xboxforums.create.msdn.com/forums/p/108722/642144.aspx#642144) we discovered this behavior is caused by calling Unacquire in your event processing loop. Funnily enough only the keyboard seems to be affected.

Now if I could just figure out what the hell Acquire and Unacquire really do.

Sorry about the cross posting btw. Just doubles the odds of some one int a pinch finding this I guess laugh.png

Microsoft's sites are notoriously impermanent after all dry.png

This topic is closed to new replies.

Advertisement