473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How To: Read Joystick with DirectX using C#

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[i]).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." );
}
}
}
}
Nov 15 '05 #1
1 23380
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" <bl*********@yahoo.com> wrote in message
news:a1*************************@posting.google.co m...
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[i]).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." );
}
}
}
}

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Peter | last post by:
Hi I would like to get access to joystick information. Buttons, lever position. Is the easiest way throug DirectX? Which SDK should I need. An example would be great. Thanks. Peter.
2
by: PawelR | last post by:
Hello group, My English is very little. I want same samples about programming joysticks connected to USB port. If you can help me please send same adresses or samples. Thx PawelR
2
by: Regan Hurd | last post by:
I want to know how to have my visual basic .net program to be able to tell when a joystick button is pressed and raise an event? Thanks
2
by: Vicente Nicolau | last post by:
Is there any way to recieve events from a joystick and to hadle them using C#??? Without using pulling.... Thanks.
1
by: Vicente Nicolau | last post by:
All example codes I found with DirectX or XNA access the joystick by pulling... ?? is there anybody who has been able to use the joystick with events (with C#)?? Is it possible? If it is not...
2
by: Vicente Nicolau | last post by:
All example codes I found with DirectX or XNA access the joystick by pulling... ?? is there anybody who has been able to use the joystick with events (with C#)?? Is it possible? If it is not...
0
by: Dhanu225 | last post by:
Hi all, Please help me to get the throttle control of joystick (zPos) using VB6.0. I got other information about buttons and joystick position using JoyInformation function in "Winmm.dll". Please...
2
by: Jack Russell | last post by:
Does .net have support for this. I wrote some code to drive a model train under vb6 (I think I used a third party control) but cannot find anything under .net (or at least anything that makes sense...
0
by: ngcson | last post by:
hi, i have facing some problems in my joystick code. it cant run correctly and will not return to its center point. hope someone can help me. below is the code: Private Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.