Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Developing Leap Motion Integrated Windows 8.1 Apps

5.00/5 (2 votes)
7 Oct 2014CPOL1 min read 19.1K   120  
This tip is about the development Leap Motion integrated Windows 8.1 metro apps using C#

Introduction

In this tip, I will explain the tools and techniques by which you can build Leap Motion apps. Firstly, you need to know what is Leap Motion. I prefer you visit https://www.leapmotion.com/.
Leap Motion provides an SDK for the development of Touch less applications. It includes DLLs project of Visual Studio for usage of Leap controller in your application, the Leap controller of both hands or you can define your own custom gestures according to your need.

Requirements

To run the project, you need:

  1. Leap Controller
  2. Leap SDK
  3. Visual Studio 2013

Configuring Project

For developing simple console and Windows Form application, you just need to add the reference of LeapCsharp.Net4.0.dll but in case of Metro apps, this configuration will throw an exception which is "the type initializer for "leap.leapPinvoke" threw an exception".

The reason for this exception is that Windows 8.1 Metro apps runs on different CPUs like x86, x64 and ARM. I solve this issue with a quick and dirty solution in which I copied all the DLLs from Leap SDK to the debug folder of my app, after that everything works fine.

Using the Code

The project is a single page Metro application in which I first check whether the device is connected or not.

C#
//
void check(Controller ctrl)
 {
  if (ctrl.IsConnected == true)
   {
   TxtConnect.Text = "Connected !!!";
   }
  else
   {
   TxtConnect.Text = "Not Connected !!!";
   }
 }
// 

Connection Check

After that, I display the co-ordinates, type and direction of bones on onFrame event listener:

C#
public void newFrameHandler(Controller controller)
 {
  Leap.Frame frame = controller.Frame();
  foreach (Hand hand in frame.Hands)
   {
    Vector normal = hand.PalmNormal;
    Vector direction = hand.Direction;
    foreach (Finger finger in hand.Fingers)
    {
    Bone bone;
    foreach (Bone.BoneType boneType in (Bone.BoneType[])Enum.GetValues(typeof(Bone.BoneType))) 
    {
     string dn = "";
     bone = finger.Bone(boneType);
     if (bone.Direction.x > 0 && bone.Direction.y > 0)
     {
     dn = "left up";
     }
     else if(bone.Direction.x < 0 && bone.Direction.y > 0) 
     {
     dn = "Right up";
     }
     else if (bone.Direction.x < 0 && bone.Direction.y < 0)
     {
     dn = "Right Down";
     }
     else if (bone.Direction.x > 0 && bone.Direction.y < 0)
     {
     dn = "Left Down";
     }
     SafeWriteLine("Bone: " + boneType + "\n"
     + "start: " + bone.PrevJoint + "\n"
     + "end: " + bone.NextJoint + "\n"
     + "direction: " + bone.Direction.Normalized + "\n"
     + "2d direction: " + dn + "\n");
     }
   }
 }

Co-ordinates and direction

Points of Interest

In this tip, I have not explained the movement of cursor or click events. There are tons of articles about that on the web. I just explained the way with which you can integrate Leap Motion and Metro apps.

History

  • 8th October, 2014: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)