-
Notifications
You must be signed in to change notification settings - Fork 2
/
KinectControl.cs
329 lines (285 loc) · 12.4 KB
/
KinectControl.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Windows;
using System.Windows.Input;
using System.Runtime.InteropServices;
using System.Windows.Threading;
//using System.Windows.PresentationSource;
using System.Windows.Media;
//using System.Windows.Forms;
using Microsoft.Kinect;
namespace Microsoft.Samples.Kinect.DiscreteGestureBasics
{
class KinectControl
{
/// <summary>
/// Active Kinect sensor
/// </summary>
KinectSensor sensor;
/// <summary>
/// Reader for body frames
/// </summary>
BodyFrameReader bodyFrameReader;
/// <summary>
/// Array for the bodies
/// </summary>
private Body[] bodies = null;
/// <summary>
/// Screen width and height for determining the exact mouse sensitivity
/// </summary>
int screenWidth, screenHeight;
/// <summary>
/// timer for pause-to-click feature
/// </summary>
DispatcherTimer timer = new DispatcherTimer();
/// <summary>
/// How far the cursor move according to your hand's movement
/// </summary>
public float mouseSensitivity = MOUSE_SENSITIVITY;
/// <summary>
/// Time required as a pause-clicking
/// </summary>
public float timeRequired = TIME_REQUIRED;
/// <summary>
/// The radius range your hand move inside a circle for [timeRequired] seconds would be regarded as a pause-clicking
/// </summary>
public float pauseThresold = PAUSE_THRESOLD;
/// <summary>
/// Decide if the user need to do clicks or only move the cursor
/// </summary>
public bool doClick = DO_CLICK;
/// <summary>
/// Use Grip gesture to click or not
/// </summary>
public bool useGripGesture = USE_GRIP_GESTURE;
/// <summary>
/// Value 0 - 0.95f, the larger it is, the smoother the cursor would move
/// </summary>
public float cursorSmoothing = CURSOR_SMOOTHING;
// Default values
public const float MOUSE_SENSITIVITY = 3.5f;
public const float TIME_REQUIRED = 2f;
public const float PAUSE_THRESOLD = 60f;
public const bool DO_CLICK = true;
public const bool USE_GRIP_GESTURE = true;
public const float CURSOR_SMOOTHING = 0.2f;
/// <summary>
/// Determine if we have tracked the hand and used it to move the cursor,
/// If false, meaning the user may not lift their hands, we don't get the last hand position and some actions like pause-to-click won't be executed.
/// </summary>
bool alreadyTrackedPos = false;
/// <summary>
/// for storing the time passed for pause-to-click
/// </summary>
float timeCount = 0;
/// <summary>
/// For storing last cursor position
/// </summary>
Point lastCurPos = new Point(0, 0);
/// <summary>
/// If true, user did a left hand Grip gesture
/// </summary>
bool wasLeftGrip = false;
/// <summary>
/// If true, user did a right hand Grip gesture
/// </summary>
bool wasRightGrip = false;
public KinectControl()
{
// get Active Kinect Sensor
sensor = KinectSensor.GetDefault();
// open the reader for the body frames
bodyFrameReader = sensor.BodyFrameSource.OpenReader();
bodyFrameReader.FrameArrived += bodyFrameReader_FrameArrived;
// get screen with and height
screenWidth = (int)SystemParameters.PrimaryScreenWidth;
screenHeight = (int)SystemParameters.PrimaryScreenHeight;
// set up timer, execute every 0.1s
timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
// open the sensor
sensor.Open();
}
/// <summary>
/// Pause to click timer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Timer_Tick(object sender, EventArgs e)
{
if (!doClick || useGripGesture) return;
if (!alreadyTrackedPos)
{
timeCount = 0;
return;
}
Point curPos = MouseControl.GetCursorPosition();
foreach (Body body in this.bodies)
{
if ((lastCurPos - curPos).Length < pauseThresold)
{
if ((timeCount += 0.1f) > timeRequired)
{
//MouseControl.MouseLeftDown();
//MouseControl.MouseLeftUp();
if (body.HandRightState == HandState.Lasso)
{
MouseControl.DoMouseClick1();
}
else if(body.HandRightState == HandState.Open && body.HandLeftState == HandState.Open)
{
MouseControl.DoScroll();
}
else if (body.HandRightState == HandState.Closed)
{
MouseControl.DoMouseClick();
}
timeCount = 0;
}
}
else
{
timeCount = 0;
}
lastCurPos = curPos;
}
}
/// <summary>
/// Read body frames
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void bodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
// As long as those body objects are not disposed and not set to null in the array,
// those body objects will be re-used.
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (!dataReceived)
{
alreadyTrackedPos = false;
return;
}
foreach (Body body in this.bodies)
{
// get first tracked body only, notice there's a break below.
if (body.IsTracked)
{
// get various skeletal positions
CameraSpacePoint handLeft = body.Joints[JointType.HandLeft].Position;
CameraSpacePoint handRight = body.Joints[JointType.HandRight].Position;
CameraSpacePoint spineBase = body.Joints[JointType.SpineBase].Position;
if (handRight.Z - spineBase.Z < -0.15f)
{
if ((handRight.X - spineBase.X >=0.5f )&&(body.HandRightState == HandState.Open))
{
System.Windows.Forms.SendKeys.SendWait("{Left}");
}
else if ((handLeft.X - spineBase.X <= 0.5f) && (body.HandLeftState == HandState.Open))
{
System.Windows.Forms.SendKeys.SendWait("{Right}");
}
else if (handRight.Z - spineBase.Z < -0.15f) // if right hand lift up
{
/* hand x calculated by this. we don't use shoulder right as a reference cause the shoulder right
* is usually behind the lift right hand, and the position would be inferred and unstable.
* because the spine base is on the left of right hand, we plus 0.05f to make it closer to the right. */
float x = handRight.X - spineBase.X + 0.05f;
/* hand y calculated by this. ss spine base is way lower than right hand, we plus 0.51f to make it
* higer, the value 0.51f is worked out by testing for a several times, you can set it as another one you like. */
float y = spineBase.Y - handRight.Y + 0.51f;
// get current cursor position
Point curPos = MouseControl.GetCursorPosition();
// smoothing for using should be 0 - 0.95f. The way we smooth the cusor is: oldPos + (newPos - oldPos) * smoothValue
float smoothing = 1 - cursorSmoothing;
// set cursor position
MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));
alreadyTrackedPos = true;
// Grip gesture
if (doClick && useGripGesture)
{
if (body.HandRightState == HandState.Closed)
{
if (!wasRightGrip)
{
MouseControl.MouseLeftDown();
wasRightGrip = true;
}
}
else if (body.HandRightState == HandState.Open)
{
if (wasRightGrip)
{
MouseControl.MouseLeftUp();
wasRightGrip = false;
}
}
}
}
}
else if (handLeft.Z - spineBase.Z < -0.15f) // if left hand lift forward
{
float x = handLeft.X - spineBase.X + 0.3f;
float y = spineBase.Y - handLeft.Y + 0.51f;
Point curPos = MouseControl.GetCursorPosition();
float smoothing = 1 - cursorSmoothing;
MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));
alreadyTrackedPos = true;
if (doClick && useGripGesture)
{
if (body.HandLeftState == HandState.Closed)
{
if (!wasLeftGrip)
{
MouseControl.MouseLeftDown();
wasLeftGrip = true;
}
}
else if (body.HandLeftState == HandState.Open)
{
if (wasLeftGrip)
{
MouseControl.MouseLeftUp();
wasLeftGrip = false;
}
}
}
}
else
{
wasLeftGrip = true;
wasRightGrip = true;
alreadyTrackedPos = false;
}
// get first tracked body only
break;
}
}
}
public void Close()
{
if (timer != null)
{
timer.Stop();
timer = null;
}
if (this.sensor != null)
{
this.sensor.Close();
this.sensor = null;
}
}
}
}