简介
写这个帮助类是对 摇杆控制器捕获 接口的一个延伸,之前有了解到 OBS 插件 input-overlay 可以显示手柄信息用于直播,所以我也做了一个UI用于显示。
实现方式
手柄型号为 Xbox One,使用 XInput API 与手柄通信。
核心代码
完整代码查看:GamepadController1
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220/// <summary>
/// 从Windows的Xbox控制器接收输入
/// https://docs.microsoft.com/en-us/windows/win32/xinput/getting-started-with-xinput
/// </summary>
public class XInputHelper
{
/// <summary>
/// 是否连接控制器
/// </summary>
public bool isGetJoystick = false;
/// <summary>
/// 连接到的控制器
/// </summary>
public Controller controller;
/// <summary>
/// 控制器状态捕获计时器
/// </summary>
private Timer _timer;
/// <summary>
/// 当前按键状态
/// 用于判断两次按键差异
/// </summary>
private GamepadButtonFlags ButtonsData;
/// <summary>
/// LT 按键状态
/// 用于判断两次按键差异
/// </summary>
private byte LeftTriggerData;
/// <summary>
/// RT 按键状态
/// 用于判断两次按键差异
/// </summary>
private byte RightTriggerData;
/// <summary>
/// 左摇杆 X 坐标状态
/// 用于判断两次摇杆坐标差异
/// </summary>
private short LeftThumbXData;
/// <summary>
/// 左摇杆 Y 坐标状态
/// 用于判断两次摇杆坐标差异
/// </summary>
private short LeftThumbYData;
/// <summary>
/// 右摇杆 X 坐标状态
/// 用于判断两次摇杆坐标差异
/// </summary>
private short RightThumbXData;
/// <summary>
/// 右摇杆 Y 坐标状态
/// 用于判断两次摇杆坐标差异
/// </summary>
private short RightThumbYData;
/// <summary>
/// 按钮变化事件
/// </summary>
public event Action<GamepadButtonFlags> ButtonsChange;
/// <summary>
/// LT 按键变化事件
/// </summary>
public event Action<byte> LeftTriggerChange;
/// <summary>
/// LT 按键变化事件
/// </summary>
public event Action<byte> RightTriggerChange;
/// <summary>
/// 左摇杆 X 变化事件
/// </summary>
public event Action<short> LeftThumbXChange;
/// <summary>
/// 左摇杆 Y 变化事件
/// </summary>
public event Action<short> LeftThumbYChange;
/// <summary>
/// 右摇杆 X 变化事件
/// </summary>
public event Action<short> RightThumbXChange;
/// <summary>
/// 右摇杆 Y 变化事件
/// </summary>
public event Action<short> RightThumbYChange;
/// <summary>
/// 连接控制器
/// </summary>
/// <returns></returns>
public bool ConnectGamepad()
{
if (!isGetJoystick && _timer == null)
{
controller = new Controller(UserIndex.One);
if (controller != null)
{
isGetJoystick = true;
_timer = new Timer(obj => Update());
_timer.Change(0, 1000 / 60);
}
}
return isGetJoystick;
}
/// <summary>
/// 断开控制器
/// </summary>
/// <returns></returns>
public void BreakOffGamepad()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
if (isGetJoystick)
{
isGetJoystick = false;
}
}
/// <summary>
/// 捕获控制器数据
/// </summary>
private void Update()
{
try
{
//// 获取电池电量和电池类型
//var vGetBatteryInformation = controller.GetBatteryInformation(BatteryDeviceType.Gamepad);
//// 获取功能按钮和震动马达信息(1)
//var vGetCapabilities = controller.GetCapabilities(DeviceQueryType.Any);
//// 获取功能按钮和震动马达信息(2)
//Capabilities vCapabilities;
//var vIsGetCapabilities = controller.GetCapabilities(DeviceQueryType.Any, out vCapabilities);
//// 获取按键信息
//Keystroke vKeystroke;
//var vGetKeystroke = controller.GetKeystroke(DeviceQueryType.Any, out vKeystroke);
//// 获取状态(1)
//var vGetState = controller.GetState();
//// 获取状态(2)
//State vState;
//var vIsGetState = controller.GetState(out vState);
//// 设置震动马达
////var vVibration = vGetCapabilities.Vibration;
////var vSetVibration = controller.SetVibration(vGetCapabilities.Vibration);
// 获取状态
var vGetState = controller.GetState();
// 按钮
var vButtons = vGetState.Gamepad.Buttons;
if (ButtonsData != vButtons)
{
ButtonsData = vButtons;
ButtonsChange?.Invoke(ButtonsData);
}
// LT 按键 0-255
var vLeftTrigger = vGetState.Gamepad.LeftTrigger;
if (LeftTriggerData != vLeftTrigger)
{
LeftTriggerData = vLeftTrigger;
LeftTriggerChange?.Invoke(LeftTriggerData);
}
// RT 按键 0-255
var vRightTrigger = vGetState.Gamepad.RightTrigger;
if (RightTriggerData != vRightTrigger)
{
RightTriggerData = vRightTrigger;
RightTriggerChange?.Invoke(RightTriggerData);
}
// 左摇杆 X
var vLeftThumbX = vGetState.Gamepad.LeftThumbX;
if (!LeftThumbXData.Equals(vLeftThumbX))
{
LeftThumbXData = vLeftThumbX;
LeftThumbXChange?.Invoke(LeftThumbXData);
}
// 左摇杆 Y
var vLeftThumbY = vGetState.Gamepad.LeftThumbY;
if (!LeftThumbYData.Equals(vLeftThumbY))
{
LeftThumbYData = vLeftThumbY;
LeftThumbYChange?.Invoke(LeftThumbYData);
}
// 右摇杆 X
var vRightThumbX = vGetState.Gamepad.RightThumbX;
if (!RightThumbXData.Equals(vRightThumbX))
{
RightThumbXData = vRightThumbX;
RightThumbXChange?.Invoke(RightThumbXData);
}
// 右摇杆 Y
var vRightThumbY = vGetState.Gamepad.RightThumbY;
if (!RightThumbYData.Equals(vRightThumbY))
{
RightThumbYData = vRightThumbY;
RightThumbYChange?.Invoke(RightThumbYData);
}
}
catch (Exception ex)
{
BreakOffGamepad();
}
}
}