DirectX input for joystick work is a different way. You can create an
Event
(
EventWaitHandle
for .NET API) object which is set by the joystick driver interrupt on every input event. You can write code which fires events.
Here is how: Create the input event object. Create a thread; in this thread make an infinite loop which waits for this event object. While waiting, your thread is set to the wait state and consume zero CPU time until awaken by the event wait handle. When it happens, re-read joystick status, pack the change in status (compared to the previous state) into some event argument and fire the event; then the loop will get it the the wait state again. In this way, this thread will work as a source of events. Overall, it looks like interrupt-driven behavior.
I have only C# code, so I can use
Microsoft.DirectX.DirectInput
. Do something like this:
using Microsoft.DirectX.DirectInput;
JoystickDevice Device;
Consider you have some device wrapper JoystickDevice:
class JoystickDevice {
public void Acquire(System.Threading.WaitHandle deviceEvent) {
Device.SetEventNotification(deviceEvent);
Device.Acquire();
}
}
while (true) {
JoystickStateNotifierEvent.WaitOne();
}
—SA