Jump to content
Tuts 4 You

Help me coding use DirectInput8 to send keys.


Recommended Posts

Who can help me code a program use Direct Input 8 to send keys to a other window? This is my Project (Not successful!).


I was tryed vzy much but code to Set the keyboard behaviour. Not know continue how i coding it...

Plz help me!! Tks vzy much ! :(

Link to comment
..Helps Those Who Help Themselves

Study the DirectInput API, in particular the methods.

Keyboard input is going to be in one of two different ways for DirectInput, either polled or event based.

The event based method would be harder to emulate, and luckily I haven't run into it my self.

Your target will more then likely be using the polling method.

The traditional, but not the only way:

1) Hook the "DirectInput8Create" import from "dinput8.dll" to grab the interface pointer as it's created.

2) When the API hook fires, from the interface pointer hook the "CreateDevice" vector to catch when the device is created.

3) Once the KB device is created, caught from your hook, you finally hook the methods of interest.

In your case you want "GetDeviceState" (the polling method, the event/buffered one is "GetDeviceData"), and probably "SetCooperativeLevel" to force keyboard to "DISCL_BACKGROUND | DISCL_NONEXCLUSIVE" flags if you still want to be able to send/emulate/spoof inputs to the target when it is minimized.


Most if not all games will use a full buffer size of 256.

What I did that works easy and simple enough is create my own 256 byte array as a flag buffer array.

To emulate a key down it's a simple "aFakeKey[uDIKeyCode] = 0x80", for key up "aFakeKey[uDIKeyCode] = 0".

Now in your "GetDeviceState" hook you just OR your own array with the real device read.

You could also do a "if(GetFocus() == NULL)". If NULL then your window is not in focus (and not taking inputs) so you can just copy over your buffer instead of OR'ing it.

To simulate presses independently then you could use another flag (like the first bit) of your array, with a little more code and process it so it's "down" on the first poll and "up" on the next.

The method hooking way is usually how people do this for Direct3D too.

Just Google for these things, you will find some source code and detailed explanations.

Edited by Sirmabus
  • Like 1
Link to comment
  • 2 weeks later...

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
  • Create New...