Search code examples
c#.netvisual-studio-2012leap-motion

C#- not getting Leap Motion data without click event


what I am trying to do is get Hand tracking data using Leap motion and display on my C# form. I was able to do it with a button click.

Now, I do not want the data on click of a button but rather whenever I run the code it should be displayed on the GUI.

So I moved my code from click event to a method and I am calling that method from default consturctor.

What I observed is when I write dummy values on the tetbox manually, it works..but not other wise.

Here is my code :

namespace HandTrackOldSDK
{
    public partial class Form1 : Form
    {
        // Initialize the Controller object which connects to the Leap motion service
        // and captures the hand tracking data
        private Controller controller = new Controller();
        System.Threading.SynchronizationContext uiContext;

        public Form1()
        {
            uiContext = WindowsFormsSynchronizationContext.Current;
            InitializeComponent();
            getXYZVlaues();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();            
        }


        private void getXYZVlaues()
        {

               //Get the most recent tracking data using the Frame object
               Frame frame = controller.Frame();

                   for (int h = 0; h < frame.Hands.Count; h++)
                   {                       
                       Hand leapHand = frame.Hands[h];

                       Finger leapFinger = leapHand.Fingers[1];

                       Vector currentPosition = leapFinger.StabilizedTipPosition;

                      textBox1.Text = Convert.ToString(currentPosition.x);
                     textBox2.Text = Convert.ToString(currentPosition.y);
                     textBox3.Text = Convert.ToString(currentPosition.z); 
                   }
        }
    }
}

Any idea on why is this happening ?


Solution

  • From the look of your code I think you need to add a FrameReady event to your controller object and handle the new frames in that event.

    1. Change your code in your Form1() constructor to look like this:

      public Form1()
      {      
        InitializeComponent();
        controller.EventContext = WindowsFormsSynchronizationContext.Current;
        controller.FrameReady += getXYZValues;
      }
      
    2. Then change your getXYZValues() method to have object and FrameEventArgs arguments and to get the new frame from the FrameEventArgs object:

      private void getXYZValues(object sender, FrameEventArgs eventArgs)
      {
      
        //Get the most recent tracking data using the Frame object
        Frame frame = eventArgs.frame;
      
        for (int h = 0; h < frame.Hands.Count; h++)
        {
          Hand leapHand = frame.Hands[h];
      
          Finger leapFinger = leapHand.Fingers[1];
      
          Vector currentPosition = leapFinger.StabilizedTipPosition;
      
          textBox1.Text = Convert.ToString(currentPosition.x);
          textBox2.Text = Convert.ToString(currentPosition.y);
          textBox3.Text = Convert.ToString(currentPosition.z);
       }
      

      }

    That worked for me, displaying X,Y, and Z values in the three text boxes, which altered every time I moved my fingers. Note, the Leap Motion device is very sensitive and will capture all your little hand shaking tendencies so don't be surprised to see the values change rapidly and rarely settle to a constant number.

    Take a look at this page: https://developer-archive.leapmotion.com/documentation/csharp/devguide/Project_Setup.html It helped me get going with my code and provide an answer to this question. Hope it helps.