
[Solved Mostly]Can't read joystick properly
Posted Sunday, 6 June, 2010 - 19:37 by Tal inHere is my Joystick test(part of a GameWindow code and I use VS debugger):
protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); if (Keyboard[Key.Escape]) Exit(); int cont = Joysticks[0].Button.Count; for (int i = 0; i < cont; i++ ) if (Joysticks[0].Button[i]) throw (new Exception()); }
As you can guess by the title, no exeption is thrown when I push all the buttons!
I also check that the I have one and only one joystick by this test:
protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); if (Keyboard[Key.Escape]) Exit(); int cont = Joysticks.Count; if (cont == 1) throw (new Exception()); }
And as you can guess, the exeption was thrown.
(Please don't critic me about my tests that they are'nt like NUnit and etc. :-) )
My joystick is made by Xcom and it looks like a standard Playstation's joystick. Here is a picture of it(sorry about the size):

It's fully supported by DirectX, and I play TrackMania with it without problems.
So what is the problem here?


Comments
Re: Can't read joystick properly
Let us have a look in the function where you handle the event joystickbuttonpressed.
Re: Can't read joystick properly
OpenTK 1.0rc1 has a few joystick-related bugs that need to be fixed. If possible, I'd like to see your Joystick test - having a complete test case would help track down and fix those bugs.
Re: Can't read joystick properly
I didn't thought about using event, but I gave it a chance.
I put this in the end of OnLoad method:
Joysticks[0].ButtonDown += JoystickUpdate;And this is the method:
I press on all the buttons, and no exeption is thrown.
I have Win XP so I checed again in Control Panel -> Game Controllers and my joystick works fine.
And one more strange thing: I checked in VS debugger and the Joysticks[0].Description value is null.
So how can I read my joystick?
**edit**
Sorry I post this message after you. You post yours before I pressed "Post comment".
So I'll try to make an orginized test using console application.
Re: Can't read joystick properly
Here is my test:
And the output:
There is 1 joystick connected.
Joystick number 0:
No description(null)
Device Type: Hid
No matter how many time I move/press all my joystick, the result is nothing,
Re: Can't read joystick properly
RC1 doesn't update the joystick data.
I found a solution by recompiling OpenTK and adding to GameWindow.cs: add InputDriver.Poll(); just before OnUpdateFrameInternal(update_args);
That's on line 452 in svn revision 2701, I'm not sure where it is exactly in the RC source, but probably not far off.
This works, but I'm not sure how close it is to being how The Fiddler wants it or how efficient it is. It seems fine for testing or small projects though.
Re: Can't read joystick properly
Ok I understand what you're saying, but there is a second problem(can't compile):
http://www.opentk.com/node/1849
And don't forget - the joystick device description is null, so it's not just the updates.
Re: Can't read joystick properly
Can you please perform a fresh SVN checkout from https://opentk.svn.sourceforge.net/svnroot/opentk/branches/1.0? (instructions)
It contains a number of fixes on the joystick issues (plus you'll be able to get it to compile!)
Re: Can't read joystick properly
Ok, so I download it and use this line in cmd:
svn co https://opentk.svn.sourceforge.net/svnroot/opentk/trunk opentk
The process end up with V mark, but the compile errors are the same(where is the "OpenTK.snk" file?), and even if I renew the OpenTK assembly, the test result is the same.
I didn't reboot my computer after I installed SVN, but I saw the process working fine so I don't think it's the problem.
Re: Can't read joystick properly
The compile problem has been fixed.
The test yeild results only by Mincus's solution.
OK test result(finally)!
good:
All buttons works great.
The Value of the axes are 100% correct.
bad:
Desription is always null(I also find out in the source code that the description is never been set).
Move event is invoked even if non of the axes move.
Delta parameter in Move event(but Value is ok):
In POV(arrows):
Axis 4(horizontal):
Right- Del = -1 Value = 1
Left- Del = 1 Value = -1
Axis 5(vertical):
Up - Del = -1; Val = 1
Down - Del = 1; Val = -1
And in the other axes(also in POV), Delta = -Value(again, Value is correct).
So I'm happy I can use full joystick input now, even know I can't relay on the Move event and the Description.
Thank you all!
Re: Can't read joystick properly
New here, but I've made some basic improvements to windows joystick support. (FWIW, I LOVE that it's using winm.dll instead of DirectX -- I made these changes explicitly to avoid DirectX and go with an OpenTK implementation). I didn't touch it on linux yet as I'm working in windows at the moment.
Two main fixes:
1) No description. DirectX gives you a nice name for the joystick device plugged in. this is because it goes to the registry and looks it up from what the driver installs there. I didn't get that to work (time and dind't need to really). What I DID do was simply assign the vendorID and productID to the description, so for example, an XBox controller's description will come up as "1118:654". This si because I needed a way to uniquely identify what joystick was plugged in to dynamically reassign axes/buttons to different actions when different joysticks are used. Previously there was no simple way to tell the difference between two joysticks.
2) I added dynamic updates to the joystick device list. Previously the list of joysticks was read once, when a GameWindow is created. If you plugged in a joystick later, or plugged in a different joystick, it wouldn't catch the changes. It now reads new joysticks and removes ones that are no longer attached from the list. I also added a Connected property to JoystickDevice.cs so that the application using the JoystickDevice could know whether it was still connected or not (if it's not connected it won't throw an exception, it will just return back 0's for all data). This all assumes you are polling the device -- no changes will show up until you poll the joystick (GameWindow.InputDriver.Poll()). Unfortunately, the InputDriver is deprecated, but needs to be replaced by something that exposes the Poll() method. Perhaps it could be exposed in JoystickDevice...?
Another improvement might be to change the data representation from JoystickAxisCollection / JoystickButtonCollection to simply float[] / bool[]. As a rule, I never want my code to rely too heavily on external libraries, and so I immediately convert the collections to primitive arrays anyway. But I'm quirky that way....
Here's the changes:
JoystickDevice.cs
Changed/New methods in WinMMJoystick.cs