Reading a USB joystick in C# .NET (full .NET)

This is a little bit off topic, but does anyone here know how to read a joystick in C# .NET? I googled around and although there are some guides, they seem to all be outdated.

Hi Chris,

Is it a HID compliant joystick?

If so there is a section in the beginners guide I noticed, it is on page 109.

Cheers

Andy

He wants it for full .net on the PC. One of the only good articles is my old one which was in .net 2.0 heh. Chris is just being lazy, he wants to copy someone’s stuff rather than do some real work :stuck_out_tongue: hehehehe

[url]http://www.codeproject.com/KB/directx/joystick.aspx[/url]
Nice !

First google result on c# joystick :clap:

I have done it before in c# if I am right. I believe my source code is lost though… :-[
It’s not hard anyway ;D

If you need help with some particular parts, feel free to ask :slight_smile:

Ah I missed that bit!

DirectInput in DX then.

Is this fun or what? Reading USB Joystick on FEZ is easier than on a PC!!! LOL :clap: GHI

Lol that’s my article Randoom. It’s out of date sadly and doesnt work on the new DirectX SDK :frowning:

Gus, of course it’s easier on a FEZ… because FEZ is freakin’ easy!

Btw, anyone who hasn’t worked out already - Chris and i are friendly and give each other crap all the time - just in case anyone is reading us jibbing each other in the forums as insulting each other :slight_smile:

I got a joystick yesterday and had it working in ten minutes. Of that five minutes was figuring out how to access the throttle information, which was in axis2.

I was hoping someone had something already figured out out that I could just copy, but since it doesn’t seem like anyone does I’ll have to go at it to see if I can work it out from Mark’s old example.

Still though, there’s gotta be someone around here with some obscure program that used it.

I wanna interface a PS1 joystick and it looks complicated…

@ Chris…If you figure it out, please share. :smiley:

I have been trying to figure this one out myself for quite some time. DirectX seems like the only real candidate with the DirectInput.dll

Mike in MN

Sorry posted info already in thread!

Done.

You will need SlimDX.

/*
 * Simple Joystick API
 *    Coded by Chris Seto 2010
 *    
 * This code released under the Apache 2.0 license, copyright Chris Seto 2010
 * 
 * */
using System;
using SlimDX.DirectInput;

namespace SimpleJoystickAPI
{
    class SimpleJoystick
    {
        /// <summary>
        /// Joystick handle
        /// </summary>
        private Joystick Joystick;

        /// <summary>
        /// Get the state of the joystick
        /// </summary>
        public JoystickState State
        {
            get
            {

                if (Joystick.Acquire().IsFailure)
                    throw new Exception("Joystick failure");

                if (Joystick.Poll().IsFailure)
                    throw new Exception("Joystick failure");

                return Joystick.GetCurrentState();
            }
        }

        /// <summary>
        /// Construct, attach the joystick
        /// </summary>
        public SimpleJoystick()
        {
            DirectInput dinput = new DirectInput();

            // Search for device
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Create device
                try
                {
                    Joystick = new Joystick(dinput, device.InstanceGuid);
                    break;
                }
                catch (DirectInputException)
                {
                }
            }

            if (Joystick == null)
                throw new Exception("No joystick found");

            foreach (DeviceObjectInstance deviceObject in Joystick.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    Joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
            }

            // Acquire sdevice
            Joystick.Acquire();
        }

        /// <summary>
        /// Release joystick
        /// </summary>
        public void Release()
        {
            if (Joystick != null)
            {
                Joystick.Unacquire();
                Joystick.Dispose();
            }

            Joystick = null;
        }
    }
}

@ Chris or anyone else for that matter…

I have been trying to play with this and also read the code samples from the codeproject sample, I just can’t seem to figure out how to put this together in order for the Joystick to work.

Does anyone have a simple little example for a Form Application they would be willing to share?

Nothing to exciting, just a layout out here and how to layout the code in the Main Form1,cs.

I can’t seem to follow the codeproject one because of the button and sider layout.

I know this is not NETMF, but if I can get the computer App to work, I am planing on sending those Joystick positions to the Panda.

I am lost.

Mike in MN

Do you have SlimDX installed?

I do.

My biggest problem is how to start laying it out to use the methods.

I think this computer programming stuff may just be over my head.

Mike in MN

Mike:

Check this out: http://files.chrisseto.com/7rK

THANKS CHRIS!

At first glance looks like exactly what I am looking for.

I just hope I can put this all together to share also.

Mike in MN

No problem, I would have uploaded it earlier, but I was away from my home computer. Have fun! :smiley: