How To: Read Joystick with DirectX using C#

B

Bleedledeep

I am new to C#/.NET/DirectX, and could not find a simple example of
how to read the X/Y location of the joystick.

< YIKES!!! The DirectX.DirectInput is one serious pile of API! >

After some futzing around, I came up with the following - which seems
to work just fine. I'm looking for a sanity check here, is this a
good way to access the joystick? Yeah, yeah, I only look for the
first joystick, and there is some hardcoded silliness in there...
Production code this is not!

Implementation of JoystickListener and JoystickEvent are left as
exercises for the reader...


using System;
using System.Collections;
using System.Threading;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace JoystickLibrary
{
/// <summary>
/// Summary description for JoystickManager.
/// </summary>
public class JoystickManager
{

private AutoResetEvent JOYSTICK_EVENT = new AutoResetEvent(
true );
private WaitHandle[] WAIT_FOR = new WaitHandle[1];

private Device joystick;
private ArrayList listeners = new ArrayList();

/*
*
*/
public JoystickManager()
{

WAIT_FOR[0] = JOYSTICK_EVENT;

DeviceList dl = Manager.GetDevices( DeviceType.Joystick,
EnumDevicesFlags.AttachedOnly );
while ( dl.MoveNext() )
{
DeviceInstance di = (DeviceInstance)dl.Current;
if ( di.DeviceType == DeviceType.Joystick )
{
InitializeJoystick( di );
break;
}
}
}

private void InitializeJoystick( DeviceInstance
pDeviceInstance )
{
joystick = new Device( pDeviceInstance.ProductGuid );

Thread t = new Thread( new ThreadStart(DoWork) );
t.Start();

joystick.SetEventNotification( JOYSTICK_EVENT );

joystick.Acquire();
}

private void DoWork()
{
JoystickEvent je = null;

while( true )
{
WaitHandle.WaitAll( WAIT_FOR );

JoystickState jss = joystick.CurrentJoystickState;

JoystickSpeed u = JoystickSpeed.STOPPED;
JoystickSpeed d = JoystickSpeed.STOPPED;
JoystickSpeed l = JoystickSpeed.STOPPED;
JoystickSpeed r = JoystickSpeed.STOPPED;

if ( jss.Y < 10000 )
{
u = JoystickSpeed.FAST;
}
else if ( jss.Y < 20000 )
{
u = JoystickSpeed.MEDIUM;
}
else if ( jss.Y < 30000 )
{
u = JoystickSpeed.SLOW;
}
else if ( jss.Y > 55535 )
{
d = JoystickSpeed.FAST;
}
else if ( jss.Y > 45535 )
{
d = JoystickSpeed.MEDIUM;
}
else if ( jss.Y > 35535 )
{
d = JoystickSpeed.SLOW;
}

if ( jss.X < 10000 )
{
l = JoystickSpeed.FAST;
}
else if ( jss.X < 20000 )
{
l = JoystickSpeed.MEDIUM;
}
else if ( jss.X < 30000 )
{
l = JoystickSpeed.SLOW;
}
else if ( jss.X > 55535 )
{
r = JoystickSpeed.FAST;
}
else if ( jss.X > 45535 )
{
r = JoystickSpeed.MEDIUM;
}
else if ( jss.X > 35535 )
{
r = JoystickSpeed.SLOW;
}

if ( je == null || je.UpSpeed != u || je.DownSpeed !=
d || je.LeftSpeed != l || je.RightSpeed != r )
{
je = new JoystickEvent( u, d, l, r );

for ( int i = 0; i < listeners.Count; i++ )
{

((JoystickListener)listeners).JoystickMoved( je );
}
}
}
}

/*
*
*/
public void AddListener( JoystickListener pListener )
{
if ( listeners.Contains(pListener) )
{
throw new ArgumentException( "Listener already
registered." );
}
else
{
listeners.Add( pListener );
}
}

/*
*
*/
public void RemoveListener( JoystickListener pListener )
{
if ( listeners.Contains(pListener) )
{
listeners.Remove( pListener );
}
else
{
throw new ArgumentException( "Listener is not
registered." );
}
}
}
}
 
L

Lynn Harrison

At a glance it looks like it should work OK.

--

Lynn Harrison
SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)
www.forums.Apress.com

Bleedledeep said:
I am new to C#/.NET/DirectX, and could not find a simple example of
how to read the X/Y location of the joystick.

< YIKES!!! The DirectX.DirectInput is one serious pile of API! >

After some futzing around, I came up with the following - which seems
to work just fine. I'm looking for a sanity check here, is this a
good way to access the joystick? Yeah, yeah, I only look for the
first joystick, and there is some hardcoded silliness in there...
Production code this is not!

Implementation of JoystickListener and JoystickEvent are left as
exercises for the reader...


using System;
using System.Collections;
using System.Threading;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace JoystickLibrary
{
/// <summary>
/// Summary description for JoystickManager.
/// </summary>
public class JoystickManager
{

private AutoResetEvent JOYSTICK_EVENT = new AutoResetEvent(
true );
private WaitHandle[] WAIT_FOR = new WaitHandle[1];

private Device joystick;
private ArrayList listeners = new ArrayList();

/*
*
*/
public JoystickManager()
{

WAIT_FOR[0] = JOYSTICK_EVENT;

DeviceList dl = Manager.GetDevices( DeviceType.Joystick,
EnumDevicesFlags.AttachedOnly );
while ( dl.MoveNext() )
{
DeviceInstance di = (DeviceInstance)dl.Current;
if ( di.DeviceType == DeviceType.Joystick )
{
InitializeJoystick( di );
break;
}
}
}

private void InitializeJoystick( DeviceInstance
pDeviceInstance )
{
joystick = new Device( pDeviceInstance.ProductGuid );

Thread t = new Thread( new ThreadStart(DoWork) );
t.Start();

joystick.SetEventNotification( JOYSTICK_EVENT );

joystick.Acquire();
}

private void DoWork()
{
JoystickEvent je = null;

while( true )
{
WaitHandle.WaitAll( WAIT_FOR );

JoystickState jss = joystick.CurrentJoystickState;

JoystickSpeed u = JoystickSpeed.STOPPED;
JoystickSpeed d = JoystickSpeed.STOPPED;
JoystickSpeed l = JoystickSpeed.STOPPED;
JoystickSpeed r = JoystickSpeed.STOPPED;

if ( jss.Y < 10000 )
{
u = JoystickSpeed.FAST;
}
else if ( jss.Y < 20000 )
{
u = JoystickSpeed.MEDIUM;
}
else if ( jss.Y < 30000 )
{
u = JoystickSpeed.SLOW;
}
else if ( jss.Y > 55535 )
{
d = JoystickSpeed.FAST;
}
else if ( jss.Y > 45535 )
{
d = JoystickSpeed.MEDIUM;
}
else if ( jss.Y > 35535 )
{
d = JoystickSpeed.SLOW;
}

if ( jss.X < 10000 )
{
l = JoystickSpeed.FAST;
}
else if ( jss.X < 20000 )
{
l = JoystickSpeed.MEDIUM;
}
else if ( jss.X < 30000 )
{
l = JoystickSpeed.SLOW;
}
else if ( jss.X > 55535 )
{
r = JoystickSpeed.FAST;
}
else if ( jss.X > 45535 )
{
r = JoystickSpeed.MEDIUM;
}
else if ( jss.X > 35535 )
{
r = JoystickSpeed.SLOW;
}

if ( je == null || je.UpSpeed != u || je.DownSpeed !=
d || je.LeftSpeed != l || je.RightSpeed != r )
{
je = new JoystickEvent( u, d, l, r );

for ( int i = 0; i < listeners.Count; i++ )
{

((JoystickListener)listeners).JoystickMoved( je );
}
}
}
}

/*
*
*/
public void AddListener( JoystickListener pListener )
{
if ( listeners.Contains(pListener) )
{
throw new ArgumentException( "Listener already
registered." );
}
else
{
listeners.Add( pListener );
}
}

/*
*
*/
public void RemoveListener( JoystickListener pListener )
{
if ( listeners.Contains(pListener) )
{
listeners.Remove( pListener );
}
else
{
throw new ArgumentException( "Listener is not
registered." );
}
}
}
}
 
Top