Accueil > > > SE SERVIR D'UNE MANETTE OU JOYSTICK
SE SERVIR D'UNE MANETTE OU JOYSTICK
Information sur la source
Description
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
Commentaires et avis
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
|
Derniers Blogs
PARUTION DE MON LIVRE SUR WPF 4PARUTION DE MON LIVRE SUR WPF 4 par odewit
La 2e édition de mon livre sur WPF sort aujourd'hui en version numérique et lundi en version papier :-)
L'ouvrage présente de façon approfondie les fonctionnalités de WPF 4 : graphisme 2D et 3D, animation, multimédia, interfaces utilisateur, databind...
Cliquez pour lire la suite de l'article par odewit EDM : COMMENT UTILISER L'HORIZONTAL ENTITY SPLITTINGEDM : COMMENT UTILISER L'HORIZONTAL ENTITY SPLITTING par Matthieu MEZIL
Une des raisons pour lesquelles j'adore l'Entity Framework est la puissance de son mapping. Beaucoup de développeurs pour ne pas dire la plus part n'en n'ont pas conscience. Pour rappel, j'ai réalisé des videos (en anglais) sur le mapping . Certains scena...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [WP7DEV][REACTIVE] RENDRE LES REACTIVE EXTENSIONS PLUS STABLES[WP7DEV][REACTIVE] RENDRE LES REACTIVE EXTENSIONS PLUS STABLES par jay
Lorsque l'on développe des applications .NET, les exceptions non gérées dans des threads ont le désagréable effet de terminer le processus courant.
Dans l'exemple suivant.......(read more) ...
Cliquez pour lire la suite de l'article par jay WINDBG / SOS / PSSCOR2 : FAILED TO LOAD DATA ACCESS DLL (MSCORDACWKS)WINDBG / SOS / PSSCOR2 : FAILED TO LOAD DATA ACCESS DLL (MSCORDACWKS) par coq
Ceux d'entre nous qui analysent des dumps d'applications .NET (notamment ceux créés via WER après un crash) en dehors de l'environnement initial ont probablement tous été confrontés au moins une fois au message suivant, à la saisie d'une commande SOS ...
Cliquez pour lire la suite de l'article par coq
Forum
RE : CRéATION DE MODULERE : CRéATION DE MODULE par The Meteorologist
Cliquez pour lire la suite par The Meteorologist
Logiciels
Microsoft Office (2010)MICROSOFT OFFICE (2010)Microsoft Office 2010 offre de nouveaux moyens flexibles et puissants pour optimiser votre travai... Cliquez pour télécharger Microsoft Office SeaMonkey (2.0.7)SEAMONKEY (2.0.7)Le projet SeaMonkey est issu d'un effort communautaire pour developper une application tout en un... Cliquez pour télécharger SeaMonkey Safari (5.0.2)SAFARI (5.0.2)Le navigateur d'Apple a lui aussi été mis à jour, aussi bien dans sa mouture Windows que celle po... Cliquez pour télécharger Safari Mozilla FireFox (4.0 béta 5)MOZILLA FIREFOX (4.0 BéTA 5)Firefox 4.0 béta 5
L'une des nouveautés visibles les plus attendues réside sans doute dans l'a... Cliquez pour télécharger Mozilla FireFox Mozilla Firefox (3.6.9)MOZILLA FIREFOX (3.6.9)Firefox 3.6.9 corrige les problèmes suivants :
* Introduced support for the X-FRAME-OPTION... Cliquez pour télécharger Mozilla Firefox
|