Your answer is in the second post in this thread
If you use C++ you can use microsofts detours library to hook the directx functions. As for how, look at the source for macroquest.
If you dont use C++, good luck on finding an alternative solution :)
A generic no offsets required solution(for DirectX8, but you could add similar for other versions) could go something like this:
Step 1: Hook the Direct3DCreate8 function
Step 2: In the Direct3DCreate8 hook, Install the hook for IDirect3D8::CreateDevice
Step 3: Remove the Direct3DCreate8 hook
Step 4: In the IDirect3D8::CreateDevice hook, install GetDeviceData hook
Step 5: Remove the IDirect3D8::CreateDevice hook
Now you should have a GetDeviceData hook in place where you can manipulate keyboard/mouse buffers MQ-style. You can also do other fun stuff, like hooking EndScene and draw stuff on the screen every frame, or SetRenderState for quake wall-hack style effects.