/// <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 = MouseKeyBoardControl.GetCursorPosition(); if ((lastCurPos - curPos).Length < pauseThresold) { if ((timeCount += 0.1f) > timeRequired) { MouseKeyBoardControl.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 (activeBodyIndex != -1) { Body body = bodies[activeBodyIndex]; if (!body.IsTracked) { activeBodyIndex = -1; } } if (activeBodyIndex == -1) { for (int i = 0; i < bodies.Length; i++) { Body body = bodies[i]; if (body.IsTracked) { activeBodyIndex = i; // No need to continue loop break; } } } if (activeBodyIndex != -1) { Body body = bodies[activeBodyIndex]; // Do stuff with known active body. // } // } //} 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) { if (body.HandLeftState == HandState.Lasso) { // 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 right hand lift forward { /* 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 = MouseKeyBoardControl.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 MouseKeyBoardControl.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) { MouseKeyBoardControl.MouseLeftDown(); wasRightGrip = true; } } else if (body.HandRightState == HandState.Open) { if (wasRightGrip) { MouseKeyBoardControl.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 = MouseKeyBoardControl.GetCursorPosition(); // float smoothing = 1 - cursorSmoothing; // MouseKeyBoardControl.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) // { // MouseKeyBoardControl.MouseLeftDown(); // wasLeftGrip = true; // } // } // else if (body.HandLeftState == HandState.Open) // { // if (wasLeftGrip) // { // MouseKeyBoardControl.MouseLeftUp(); // wasLeftGrip = false; // } // } // } //} else if (handLeft.X - spineBase.X > 0.3f) { MouseKeyBoardControl.KeyDown(); } else { wasLeftGrip = true; wasRightGrip = true; alreadyTrackedPos = false; } // get first tracked body only //break; } } } } } } }