Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have search everywhere. I am trying to create a joystick event similar to a mouse click etc but all I can find is joystick polling (using a timer to check the state of the joystick.)

I am using vb.net and DirectX input. Is it possible to create a class that can create a joystick event that fires upon state change? It seems like a possibility but it eludes me.

Thanks! :)
Posted

1 solution

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:

C#
using Microsoft.DirectX.DirectInput;

JoystickDevice Device;
//Find your device using FindControllers, you can use them all

Consider you have some device wrapper JoystickDevice:

class JoystickDevice {
    public void Acquire(System.Threading.WaitHandle deviceEvent) {
            Device.SetEventNotification(deviceEvent);
            Device.Acquire();
    } //Acquire
}

//this is the key: create another class, thread wrapper, create a some wait handle and pass it
//to Device.Acquire. Now this handle will be Set by hardware device, your thread only waits:
//In this thread:

while (true) {
    JoystickStateNotifierEvent.WaitOne();
    //now poll Joystick state using Device;
    //determine what changed and fire some event
} //loop


—SA
 
Share this answer
 
v3
Comments
JBenhart 22-Mar-11 2:23am    
Not to be condescending but...


...huh? lol

Can you be a bit more specific? :)
Sergey Alexandrovich Kryukov 22-Mar-11 20:25pm    
It needs some time...
--SA
Sergey Alexandrovich Kryukov 22-Mar-11 20:40pm    
Is it more clear now. Works for .NET 2.0 and later.
If you want something else, get the idea. DirectX input API must be the same.
--SA
JBenhart 23-Mar-11 0:19am    
I'm sorry but I do not think I am understanding it fully. Basically, you create a class wrapper that tracks the joystick. Then you create a separate thread that fires upon state change? I think I am partially getting the concept but I am still missing the implementation.
Sergey Alexandrovich Kryukov 23-Mar-11 0:38am    
It simply means it is not that simple. Did you catch the idea? Are you using the same I as do (.NET, C# and Microsoft.DirectX.DirectInput)? I mentioned all classes and interfaces involved. After all, get help on them, as I did in first place.

Your questions. It's all up to you. You need some thread which runs all the application lifetime in infinite cycle. You can consider it as a part of device and have a thread per device (anyway, you want have too many joysticks at a time). You goal is to avoid permanent poll. So, you should have a thread which sleeps (is kept in wait state, not scheduled for execution by OS until awaken by the wait handle; and wait handle is set by hardware driver). Idea is kind of simple, the rest if it is your own design.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900