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
TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|