Coding4Fun Kinect Projects

Kinect Mouse Cursor

Description

Kinect Mouse Cursor

Kinect Mouse Cursor is a demo application that uses the Kinect for Windows SDK and its skeletal tracking features to allow a user to use their hands to control the Windows mouse cursor.

This sample demonstrates how to use the Kinect skeletal tracking feature to move the mouse cursor with a user's hands.

  • By default, the right hand moves the cursor and the left hand controls the left mouse button.
  • Then the left hand is raised, the mouse button is held down.  When the left hand is lowered, the mouse button is released.
  • Use the checkbox on the main UI to switch to "left-handed" mode where the hand functions are reversed.

Project Information URL: https://channel9.msdn.com/coding4fun/projects/Kinect-Mouse-Cursor

Project Download URL: http://kinectmouse.codeplex.com/

image

         Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' hook up our events for depth and sekeltal tracking
            AddHandler _runtime.DepthFrameReady, AddressOf _runtime_DepthFrameReady
            AddHandler _runtime.SkeletonFrameReady, AddressOf _runtime_SkeletonFrameReady
            Try

               ' tell Kinect we need the depth buffer and skeletal tracking
                _runtime.Initialize(RuntimeOptions.UseDepth Or RuntimeOptions.UseSkeletalTracking)
            Catch ex As Exception
                MessageBox.Show("Could not initialize Kinect device: " & ex.Message)
            End Try           
            ' parameters used to smooth the skeleton data

            _runtime.SkeletonEngine.TransformSmooth = True
            Dim parameters As New TransformSmoothParameters()
            parameters.Smoothing = 0.7f
            parameters.Correction = 0.3f
            parameters.Prediction = 0.4f
            parameters.JitterRadius = 1.0f
            parameters.MaxDeviationRadius = 0.5f
            _runtime.SkeletonEngine.SmoothParameters = parameters
            Try

                ' open the depth stream at the proper resolution

                _runtime.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.Depth)
            Catch ex As Exception

                MessageBox.Show("Could not open depth stream: " & ex.Message)

            End Try

        End Sub 

 

 Private Sub _runtime_SkeletonFrameReady(ByVal sender As Object, ByVal e As SkeletonFrameReadyEventArgs)
    For Each sd As SkeletonData In e.SkeletonFrame.Skeletons
        ' the first found/tracked skeleton moves the mouse cursor
        If sd.TrackingState = SkeletonTrackingState.Tracked Then
            ' make sure both hands are tracked
            If sd.Joints(JointID.HandLeft).TrackingState = JointTrackingState.Tracked AndAlso sd.Joints(JointID.HandRight).TrackingState = JointTrackingState.Tracked Then
                Dim cursorX, cursorY As Integer
                ' get the left and right hand Joints
                Dim jointRight As Joint = sd.Joints(JointID.HandRight)
                Dim jointLeft As Joint = sd.Joints(JointID.HandLeft)
                ' scale those Joints to the primary screen width and height
                Dim scaledRight As Joint = jointRight.ScaleTo(CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), SkeletonMaxX, SkeletonMaxY)
                Dim scaledLeft As Joint = jointLeft.ScaleTo(CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), SkeletonMaxX, SkeletonMaxY)

                ' figure out the cursor position based on left/right handedness
                If LeftHand.IsChecked.GetValueOrDefault() Then
                    cursorX = CInt(Fix(scaledLeft.Position.X))
                    cursorY = CInt(Fix(scaledLeft.Position.Y))
                Else
                    cursorX = CInt(Fix(scaledRight.Position.X))
                    cursorY = CInt(Fix(scaledRight.Position.Y))
                End If
                Dim leftClick As Boolean                ' figure out whether the mouse button is down based on where the opposite hand is

                If (LeftHand.IsChecked.GetValueOrDefault() AndAlso jointRight.Position.Y > ClickThreshold) OrElse ((Not LeftHand.IsChecked.GetValueOrDefault()) AndAlso jointLeft.Position.Y > ClickThreshold) Then
                    leftClick = True
                Else
                    leftClick = False
                End If
                Status.Text = cursorX & ", " & cursorY & ", " & leftClick
                
                NativeMethods.SendMouseInput(cursorX, cursorY, CInt(Fix(SystemParameters.PrimaryScreenWidth)), CInt(Fix(SystemParameters.PrimaryScreenHeight)), leftClick)                Return
            End If
        End If
    Next sd
End Sub

 Contact Information:

The Discussion

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.