1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| 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;
}
}
} |
Partager