I'm new about C# and Sharpdx. I have this code problem from a couple of days and I don't understand way doesn't work! This is a simple task about acquiring a value of one axis of a Joystick and show it on a text box in a Form.
I did a new project on Visual Studio 2010 express and I did a Form with a button and a textBox for show the value of the joystick axis (X axis).
The first part of code down here is the example on the sharpdx documentation, the second part is a little bit different.
The problem is that the value doesn't change every time I push the button
Something is wrong but I don't know what
private void button3_Click(object sender, EventArgs e)
{
// Initialize DirectInput
var directInput = new DirectInput();
// Find a Joystick Guid
var joystickGuid = Guid.Empty;
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick e stato
Joystick joystick = new Joystick(directInput, joystickGuid);
JoystickState stato = new JoystickState();
// specifico se relativo o assoluto
joystick.Properties.AxisMode = DeviceAxisMode.Absolute;
// effettuo un collegamento con il joystick
joystick.Acquire();
// qui faccio una acquisizione dello stato che memorizzo
joystick.Poll();
// effettuo una lettura dello stato
joystick.GetCurrentState(ref stato);
// stampo il valore dell'ordinata
textBox1.Text = stato.X.ToString();
}
I think the problem is that you are calling both
Poll
andGetCurrentState
- you only need do one or the other.From your question it sounds like the latter - that is you want to
GetCurrentState
when the button is pressed - notPoll
for changes in a loop.If you do want to get the current state then you want something like this.
If you want to poll for changes you you want something like this.