-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystickMonitor.cs
70 lines (57 loc) · 1.95 KB
/
JoystickMonitor.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX.DirectInput;
using System.Threading;
using System.Windows;
namespace JoystrickControlDemo
{
class JoystickMonitor
{
private String _joystickName;
// initialize object variables
public JoystickMonitor(string joystickName)
{
_joystickName = joystickName;
}
// Poll joystick state
public void PollJoystick(IProgress<JoystickUpdate> progress, CancellationToken cancellationToken)
{
var directInput = new DirectInput();
int devicesFound = 0;
DeviceInstance device = null;
foreach (var d in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly))
{
String trimmedProductName = d.ProductName.Substring(0, _joystickName.Length);
if (devicesFound > 0)
{
throw new Exception("More than one Joystick present, only one allowed!");
}
if (String.Compare(trimmedProductName, _joystickName) == 0)
{
devicesFound++;
device = d;
}
}
if (device == null)
{
throw new NullReferenceException("No such device found!");
}
var joystick = new Joystick(directInput, device.InstanceGuid);
joystick.Properties.BufferSize = 128;
joystick.Acquire();
// Loop until a CancellationRequest is made
while (!cancellationToken.IsCancellationRequested)
{
joystick.Poll();
JoystickUpdate[] states = joystick.GetBufferedData();
foreach (var state in states)
{
progress.Report(state);
}
}
}
}
}