473,396 Members | 1,703 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,396 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 23383
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.