using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
////////////
//iAdd Code for init Joystick
private Device joystick;
private void iJoy_Load(object sender, EventArgs e) //iAdd
{
this.InitDevices();
}
private void InitDevices()
{
//create joystick device.
foreach (
DeviceInstance di in
Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
//Throw exception if joystick not found.
throw new Exception("Joystick no encontrado.");
}
//Set joystick axis ranges.
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(
ParameterHow.ById,
doi.ObjectId,
new InputRange(-5000, 5000));
}
}
//Set joystick axis mode absolute.
joystick.Properties.AxisModeAbsolute = true;
//set cooperative level.
joystick.SetCooperativeLevel(
this,
CooperativeLevelFlags.NonExclusive |
CooperativeLevelFlags.Background);
//Acquire devices for capturing.
joystick.Acquire();
}
private void UpdateJoystick()
{
string info = "Joystick: ";
//Get Mouse State.
JoystickState state = joystick.CurrentJoystickState;
//Capture Position.
info += "X:" + state.X + " ";
info += "Y:" + state.Y + " ";
info += "Z:" + state.Z + " ";
//Capture Buttons.
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons != 0)
{
info += "Boton:" + i + " ";
}
}
lbJoystick.Text = info;
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateJoystick();
}
}
}
Este es el código en c# para capturar un joystick, pero por que no me imprime sus coordenadas en pantallla.
En pantalla tengo la etiqueta lbJoystick, y el timer, todo compila bien, sin problema, pero...