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
MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril SHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KITSHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KIT par Matthew
http://www.microsoft.com/download/en/details.aspx?id=28768&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MicrosoftDownloadCenter+(Microsoft+Download+Center) ...
Cliquez pour lire la suite de l'article par Matthew
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|