begin process at 2010 02 09 23:50:37
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

DirectX / OpenGL

 > SE SERVIR D'UNE MANETTE OU JOYSTICK

SE SERVIR D'UNE MANETTE OU JOYSTICK


 Information sur la source

Note :
Aucune note
Catégorie :DirectX / OpenGL Source .NET ( DotNet ) Classé sous :manette, joystick, csharp Niveau :Débutant Date de création :30/03/2008 Date de mise à jour :30/03/2008 12:07:48 Vu :4 582

Auteur : soussous78

Ecrire un message privé
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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

  • 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;
  • }
  • }
  • }
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;
		}
	}
}



 Historique

30 mars 2008 12:07:07 :
oubli de cocher Utilise la technologie .net
30 mars 2008 12:07:48 :
faute d'orthographe..

 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) 3VE-WALKER par jyce3d
Source avec Zip Source avec une capture Source .NET (Dotnet) IMAGINE: BASE D'UN MOTEUR 3D DIRECTX par Children
Source avec Zip Source avec une capture Source .NET (Dotnet) DIRECTX OVERLAY - CHANGE WALLPAPER ON DESKTOP WITH DIRECTX par youpiyoyo
Source avec Zip Source avec une capture Source .NET (Dotnet) MOVIE PLAYER (MANAGED DIRECTX) par Zap
Source avec Zip Source avec une capture Source .NET (Dotnet) EXPLORATION DE LABYRINTHE 3D par damned3

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) CRÉATION D'UN ACTIVEX EN UTILSANT UN USERCONTROL DOTNET CSHA... par AVerhamme
Source avec Zip Source avec une capture Source .NET (Dotnet) QCM AVEC TIMER par zahenianis
Source avec Zip Source .NET (Dotnet) AUTOMATISATION DE L'EXECUTION DE MACROS SUR DOCUMENTS WORD. par sh4gm4
Source avec Zip Source .NET (Dotnet) CALCULETTE (OPÉRATEURS : +, -, X ET :) EN SILVERLIGHT par xXTitouffXx
Source avec Zip Source .NET (Dotnet) CODE SOURCE POUR L'ARTICLE "PRÉSENTATION DE LINQ" par odahan

Commentaires et avis

Commentaire de TheSin le 07/04/2008 12:58:19

Salut.
J'ai vu l'utilisation des boutons/joysticks, mais qu'en est-il de l'utilisation des retour de force ? J'ai déja tenté de les utiliser mais le code plantait littérallement :-(.
Bref, si tu peux l'ajouter, if possible, sinon tant pis ;-).

Commentaire de soussous78 le 21/08/2008 15:02:21

J'essaie en vain de trouver comment on utilise le retour de force, ainsi que la manière de faire vibrer les manettes... Je ne trouve rien là dessus.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Csharp, c++, java ou autre ??? [ par lionel55 ] Salut à tous,je développe pas mal en html/xml/javascript/asp et j'aimerais développé dans un autre language "plus sérieux" et orienté objet avec leque Seul un mec baleze en csharp pourra m'aider... [ par Barz ] J'ai deux gros soucis avec csharp :1 - J'ai une application asp.net qui appelle un assemblage d'objets métiers qui appelle aussi une autre assemblage Utilité du CSharp/.NET [ par niceboy ] Slt,Quasiment personne n'a le Framework SDK d'installé sur son ordinateur.Et s'il faut obligatoirement l'installer pour pouvoir lancer, utiliser les p les pointeurs en csharp [ par stysty ] Bonjourje sais que les pointeurs sont discret sur csharp mais j'en ai besoin et je n' y arrive pas tropje sais qu il faut utiliser unsafe j'arrive a f comment faire un graphe en csharp [ par dusk75 ] bonjourje voudrais savoir comment faire un graphe en csharp qui me permettra de faire une courbe avec des nouveaux points que je generes a intervalle CSharp.Net [ par PPA34 ] Bonjour, je voudrais savoir s'il existe un moyen d'insérer un "clearscreen" dans un programme en mode console ?Si oui, dites-moi comment ...Merci L'equivalent du fpart en Csharp [ par kaiwoo ] En langage pour calculette, il existe une fonction fpart... C'est une fonction qui permet de travailler autour de la partie decimal d'un nombre... Exi c# et html [ par lionel55 ] bonjour,comment intégrer du code html dans une form csharp?en fait j'aimerais avoir du html dans ma form csharpmerci si vous avez des idées CSharp -> SharpDevelop [ par stailer ] Salut à tous,Je bosse actuellement sur Delphi et j'arrive à la fin d'un projet. J'aimerais mettre à profit cette nouvelle période en attanquant le C#. cours debutants Csharp [ par banzaichico ] salut tt le monde!je cherche des cours de C Sharp pour debutants, de preference en francais et en PDFmerci d'avance


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,655 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales