TODOPIC
Lenguajes de programación para PC => C, C#, C++ => Mensaje iniciado por: antoniojdobarro en 17 de Febrero de 2011, 12:45:48
-
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...
-
has provado ponieno en las variables "int/long" convertirlas esplicitamente a string, es deciar algo asi:
private void UpdateJoystick()
{
string info = "Joystick: ";
//Get Mouse State.
JoystickState state = joystick.CurrentJoystickState;
//Capture Position.
info += "X:" + state.X.ToString() + " ";
info += "Y:" + state.Y.ToString() + " ";
info += "Z:" + state.Z.ToString() + " ";
//Capture Buttons.
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons != 0)
{
info += "Boton:" + i.ToString() + " ";
}
}
lbJoystick.Text = info;
}
es que no recuerdo ahora mismo si se podia omitir el ToString(), pero alomejor puede venirte por ahi el fallo
saludos
-
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Codigo para iniciar el Joystick
private Device joystick;
private void carga_panzer(object sender, EventArgs e)
{
this.InitDevices();
}
// Iniciando el Joystick
private void InitDevices()
{
//Cargando todos los joysticks posibles
foreach (
DeviceInstance di in
Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
//Si no encontramos ningún Joystrick
throw new Exception("No joystick found.");
}
////Configurando los dispositivos
//Configurando los ejes del Joystick
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(
ParameterHow.ById,
doi.ObjectId,
new InputRange(-75000, 75000));
}
}
joystick.Properties.AxisModeAbsolute = true;
//Nivel cooperativo
joystick.SetCooperativeLevel(
this,
CooperativeLevelFlags.NonExclusive |
CooperativeLevelFlags.Background);
//Adquiriendo datos del joystick
joystick.Acquire();
}
private void UpdateJoystick()
{
string info = "Joystick: ";
//Estado del joystick
JoystickState state = joystick.CurrentJoystickState;
//Posición
info += "X:" + state.X + " ";
info += "Y:" + state.Y + " ";
info += "Z:" + state.Z + " ";
//Botones
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons != 0)
{
info += "Button:" + i + " ";
}
}
label1.Text = info;
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateJoystick();
}
}
}
Funciona perfecto, compilado con Visual 2010.