Se servir d'une manette ou joystick

Contenu du snippet

Voici un petit programme simple qui permettra a certain a comprendre comment on utilise une manette ou un joystick sous c#.
Ce programme utilise le DirectInput.
Ne pas oublier d'ajouter une référence a Microsoft.DirectX.DirectInput.
Ce petit programme commence donc par rechercher tous les joystick et manettes branchés et vous affiche la valeur des abscisses et ordonnées et tout les boutons pressés.

Source / Exemple :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace test_manette
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.TextBox lbjoy;
		private System.Windows.Forms.Timer timer1;
		private Hashtable joy;
		public Form1()
		{
			InitializeComponent();
			
			Device joystick=null;
			joy = new Hashtable();
			int i=0;
			foreach(
				DeviceInstance di in 
				Manager.GetDevices(
				DeviceClass.GameControl,
				EnumDevicesFlags.AttachedOnly))
			{
				joy.Add(i,new Device(di.InstanceGuid));
				i++;
			}
			if(joy.Count == 0)
			{
				MessageBox.Show("pas de manettes");
				throw new Exception();
			}

			//entre la variation des axes.
			for(i = 0; i<joy.Count;i++)
			{
				joystick = (Device)joy[i];
				foreach(DeviceObjectInstance doi in joystick.Objects)
				{
					if((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
					{
						joystick.Properties.SetRange(
							ParameterHow.ById,
							doi.ObjectId,
							new InputRange(-5000,5000));
					}
				}

				//met l'axe des manettes en mode absolue.
				joystick.Properties.AxisModeAbsolute = true;

				//change le niveau de cooperation.
				joystick.SetCooperativeLevel(
					this,
					CooperativeLevelFlags.NonExclusive |
					CooperativeLevelFlags.Background);

				//active le devices pour la capture.
				joystick.Acquire();
			}
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.lbjoy = new System.Windows.Forms.TextBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// lbjoy
			// 
			this.lbjoy.Location = new System.Drawing.Point(0, 48);
			this.lbjoy.Multiline = true;
			this.lbjoy.Name = "lbjoy";
			this.lbjoy.Size = new System.Drawing.Size(344, 112);
			this.lbjoy.TabIndex = 0;
			this.lbjoy.Text = "textBox1";
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(344, 310);
			this.Controls.Add(this.lbjoy);
			this.Name = "Form1";
			this.Text = "Manette";
			this.ResumeLayout(false);

		}
		#endregion
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			Device joystick=null;
			string info="";
			for(int y = 0; y<joy.Count;y++)
			{
				joystick = (Device)joy[y];
				info += "Joystick["+y.ToString()+"]: ";
				
				//recupere le state du joystick.
				JoystickState state = joystick.CurrentJoystickState;

				//Capture les Positions.
				info += "X:" + state.X + " ";
				info += "Y:" + state.Y + " ";
				info += "Z:" + state.Z + " ";

				//Capture les boutons.
				byte[] buttons = state.GetButtons();
				for(int i = 0; i < buttons.Length; i++)
				{
					if(buttons[i] != 0)
					{
						info += "Button:" + i + " ";
					}
				}
				info +="\r\n";
			}
			lbjoy.Text = info;
		}
	}
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.