tags:

views:

326

answers:

2

We have a 3D application that retrieves keyboard presses via the IDirectInputDevice8. Is there any way, when we retrive keyboard events via the win32 API winproc loop back that we can send these commands to the DirectInputDevice?

A: 

The wndproc will is sent a combination of these messages on keyboard events:

WM_SYSKEYDOWN
WM_SYSKEYUP
WM_KEYDOWN
WM_KEYUP 
WM_CHAR
jussij
+1  A: 

Windows polls the keyboard hardware behind the scenes. When key events happen, it adds the respective WM_* messages to your Windows message queue (with associated information on keyboard state). The windows message pump pulls those messages off the queue and processes them accordingly. The main benefit of this approach is that you really don't have to do a lot to get hold of the keyboard events (plus knowledge of the keyboard layout/hardware isn't required). The disadvantage is that it really isn't real-time.

Talking to the Keyboard via DirectInput is actually similar, but it's up to you to do the polling. Usually, you use DirectInput to set up a keyboard device, and then every time you run your update loop, you poll the state of the keyboard using GetDeviceState(). It is up to you to keep track of the state of the keyboard between polls. The best thing to do is to create an object that wraps up this functionality, and perhaps fires events/callbacks or creates a command entry in a queue at the appropriate times so that it's nicer to work with. The advantage of this method is speed as you're talking to the hardware directly and can invoke updates instantly. The disadvantage is that you have to do this stuff manually which takes a bit of time to set up and get right.

Check out this example of how to use DirectInput to query the keyboard.

So there are a few answers to your question:

  1. If you have the WM_* messages already, why not just invoke the code that the DirectInput handling would invoke, hence giving you the same functionality?
  2. DirectInput, via polling, gives you the same information that Windows is giving you via WM_* messages. So you don't need to convert, or try to map one to the other. Just check the state of the keys and handle the cases where the state changes.
  3. Choose one method and ditch the other. They both do the same thing. I'd recommend using DirectInput if you want speed and responsiveness. If that's not so important, use the WM_* messages.

I hope that helps. Good luck!

OJ
send these commands to the DirectInputDevice?
Sorry, send the commands to the DirectInput message que, where it can query it for the keyboard.