What is the best way to use joystick in c#?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello!
I am currently extending my WPF app with capability of responding to joystick buttons (only buttons). This is a very simple task but I cannot find a simple way to solve it. Any help would be appreciated.
I have considered the following ways of working with joysticks.
1. http://msdn.microsoft.com/en-us/library/dd757116(v=vs.85).aspx Windows Multimedia API .
1.1. http://msdn.microsoft.com/en-us/library/windows/desktop/dd757114(v=vs.85).aspx joySetCapture with fChanged = true (event-based model). It supports only four joystick buttons, I need buttons 5 - 8.
1.2. joySetCapture with fChanged = false (timer-based model). Timer-based pull model is always worse than push model but this one works. There is only one problem: it has a bug. If you call joySetCapture, then
http://msdn.microsoft.com/en-us/library/windows/desktop/dd757113(v=vs.85).aspx
joyReleaseCapture and then joySetCapture again the second (and subsequent) call will fail. I need to turn on and off this listening functionality during the application lifecycle many times, so this variant is also unaccepatable.
2. Timers:
2.1. Using multimedia timer: http://msdn.microsoft.com/en-us/library/windows/desktop/dd757634(v=vs.85).aspx
TimeSetEvent with periodical call of http://msdn.microsoft.com/en-us/library/windows/desktop/dd757108(v=vs.85).aspx
joyGetPosEx (timer-based model again). This API is considered deprecated in favor of CreateTimerQueueTimer function.
2.2. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682485(v=vs.85).aspx
CreateTimerQueueTimer : is this functions accessible in .NET via
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx System.Threading.Timer class? Does this class provide high precision timer while working with hardware? Is this the best way of working with joysticks?
3. http://msdn.microsoft.com/en-us/library/ee416842(v=vs.85).aspx DirectInput . This API is considered as a replacement of multimedia functions (especially for joysticks).
3.1. Direct use of DirectInput is very hard task: to access http://msdn.microsoft.com/en-us/library/windows/desktop/ee417799(v=vs.85).aspx
IDirectInput8 interface you need to call http://msdn.microsoft.com/en-us/library/microsoft.directx_sdk.reference.directinput8create(v=vs.85).aspx
DirectInput8Create function which cannot be "pInvoked" (or I dont know how?).

3.2. I can use DirectInput via C++/CLI. This would require additional dll in my solution only for solving this simple task. I also need the way to compile the whole project under "Any CPU" and don t know, is there a way to compile C++/CLI library with such target
(and without C++ Runtime libraries). I also dont know is it the best way of solving my problem.
3.3. Using http://slimdx.org/ SlimDX or http://sharpdx.org/
SharpDX . Ill try to avoid such a way because it will require to load a huge amount of unnecessary code.
3.4. Maybe I can exclude a joystick-related functionality from that libraries (they load "Dinput8.dll" via "LoadLibrary" and call
http://msdn.microsoft.com/en-us/library/microsoft.directx_sdk.reference.directinput8create(v=vs.85).aspx
DirectInput8Create in a hard manner), if it will not violate their licence.
4. http://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx
Raw Input . I have tested this method today and http://msdn.microsoft.com/en-us/library/windows/desktop/ms645600(v=vs.85).aspx
RegisterRawInputDevices does not work with http://vjoystick.sourceforge.net/site/
VJoy virtual joystick driver in http://msdn.microsoft.com/en-us/library/windows/desktop/ms645546(v=vs.85).aspx
this example (no WM_INPUT messages were sent to my window). Does it work only with real drivers? (VJoy works perfectly with multimedia functions). I consider such API unstable.
As I said, any helpful advice will make me happy :)
Thank you.
<
Vladimir Khil
<br/>

View the full article
 
Back
Top