Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKasc

Posted 03 August 2012 - 08:25 AM

Use SlimDX's direct input library, you access the joystick like so:

[source lang="csharp"] Joystick joystick; int joystickButtonCount; int joystickAxesCount; JoystickState joystickState, oldJoystickState; public Game() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; CreateDevice(); } ... protected override void Update(GameTime gameTime) { joystickState = joystick.GetCurrentState(); bool[] buttons = joystickState.GetButtons(); Console.Clear(); Console.WriteLine("Using device: {0}\nButton count: {1}\nAxes count: {2}\n", joystick.Information.ProductName, joystickButtonCount, joystickAxesCount); for (int i = 0; i < joystickButtonCount; i++) { Console.WriteLine("{0}: {1}", i + 1, buttons[i]); } } void CreateDevice() { DirectInput dinput = new DirectInput(); foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { try { joystick = new Joystick(dinput, device.InstanceGuid); joystickAxesCount = joystick.Capabilities.AxesCount; joystickButtonCount = joystick.Capabilities.ButtonCount; break; } catch { } } if (joystick == null) { Console.WriteLine("No devices"); return; ; } joystick.Acquire(); foreach (DeviceObjectInstance deviceObject in joystick.GetObjects()) { if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0) joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000); } }[/source]

Getting your axis information is a little different, if you do joystickState{dot} intellisense will show you a lot of properties, the ones with X, Y and Z on the end will give you your axis info, so to find out which one your joystick uses you'll have to print them all the console and see.

EDIT:

These two lines:

joystickAxesCount = joystick.Capabilities.AxesCount;
joystickButtonCount = joystick.Capabilities.ButtonCount;

may cause you to hang for a few seconds, so you may want to create your device on a seperate thread so your main game loop won't stall until it responds. You could also cut this out entirely and loop over the entire bool[] buttons array which I think is of size 128.

#2Kasc

Posted 03 August 2012 - 08:24 AM

Use SlimDX's direct input library, you access the joystick like so:

[source lang="csharp"] Joystick joystick; int joystickButtonCount; int joystickAxesCount; JoystickState joystickState, oldJoystickState; public Game() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; CreateDevice(); } ... protected override void Update(GameTime gameTime) { joystickState = joystick.GetCurrentState(); bool[] buttons = joystickState.GetButtons(); Console.Clear(); Console.WriteLine("Using device: {0}\nButton count: {1}\nAxes count: {2}\n", joystick.Information.ProductName, joystickButtonCount, joystickAxesCount); for (int i = 0; i < joystickButtonCount; i++) { Console.WriteLine("{0}: {1}", i + 1, buttons[i]); } } void CreateDevice() { DirectInput dinput = new DirectInput(); foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { try { joystick = new Joystick(dinput, device.InstanceGuid); joystickAxesCount = joystick.Capabilities.AxesCount; joystickButtonCount = joystick.Capabilities.ButtonCount; break; } catch { } } if (joystick == null) { Console.WriteLine("No devices"); return; ; } joystick.Acquire(); foreach (DeviceObjectInstance deviceObject in joystick.GetObjects()) { if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0) joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000); } }[/source]

Getting your axis information is a little different, if you do joystickState<dot> intellisense will show you a lot of properties, the ones with X, Y and Z on the end will give you your axis info, so to find out which one your joystick uses you'll have to print them all the console and see.

EDIT:

These two lines:

joystickAxesCount = joystick.Capabilities.AxesCount;
joystickButtonCount = joystick.Capabilities.ButtonCount;

may cause you to hang for a few seconds, so you may want to create your device on a seperate thread so your main game loop won't stall until it responds. You could also cut this out entirely and loop over the entire bool[] buttons array which I think is of size 128.

#1Kasc

Posted 03 August 2012 - 08:21 AM

Use SlimDX's direct input library, you access the joystick like so:

[source lang="csharp"] Joystick joystick; int joystickButtonCount; int joystickAxesCount; JoystickState joystickState, oldJoystickState; public Game() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; CreateDevice(); } ... protected override void Update(GameTime gameTime) { joystickState = joystick.GetCurrentState(); bool[] buttons = joystickState.GetButtons(); Console.Clear(); Console.WriteLine("Using device: {0}\nButton count: {1}\nAxes count: {2}\n", joystick.Information.ProductName, joystickButtonCount, joystickAxesCount); for (int i = 0; i < joystickButtonCount; i++) { Console.WriteLine("{0}: {1}", i + 1, buttons[i]); } } void CreateDevice() { DirectInput dinput = new DirectInput(); foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { try { joystick = new Joystick(dinput, device.InstanceGuid); joystickAxesCount = joystick.Capabilities.AxesCount; joystickButtonCount = joystick.Capabilities.ButtonCount; break; } catch { } } if (joystick == null) { Console.WriteLine("No devices"); return; ; } joystick.Acquire(); foreach (DeviceObjectInstance deviceObject in joystick.GetObjects()) { if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0) joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000); } }[/source]

Getting your axis information is a little different, if you do joystickState<dot> intellisense will show you a lot of properties, the ones with X, Y and Z on the end will give you your axis info, so to find out which one your joystick uses you'll have to print them all the console and see.

PARTNERS