Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#
Tip/Trick

Developing Leap Motion Integrated Windows 8.1 Apps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Oct 2014CPOL1 min read 18.3K   120   7   10
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)


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLeap Motion with windows 8.1 app Problem Pin
xiaotingfu15-Mar-15 22:28
xiaotingfu15-Mar-15 22:28 
AnswerRe: Leap Motion with windows 8.1 app Problem Pin
Muhammad Muddasar Yamin16-Mar-15 19:00
professionalMuhammad Muddasar Yamin16-Mar-15 19:00 
GeneralRe: Leap Motion with windows 8.1 app Problem Pin
xiaotingfu17-Mar-15 20:36
xiaotingfu17-Mar-15 20:36 
GeneralRe: Leap Motion with windows 8.1 app Problem Pin
Muhammad Muddasar Yamin17-Mar-15 22:35
professionalMuhammad Muddasar Yamin17-Mar-15 22:35 
QuestionSDK version Pin
Vangos Pterneas27-Oct-14 5:48
professionalVangos Pterneas27-Oct-14 5:48 
AnswerRe: SDK version Pin
Muhammad Muddasar Yamin27-Oct-14 20:49
professionalMuhammad Muddasar Yamin27-Oct-14 20:49 
GeneralRe: SDK version Pin
Vangos Pterneas28-Oct-14 6:50
professionalVangos Pterneas28-Oct-14 6:50 
GeneralRe: SDK version Pin
Muhammad Muddasar Yamin28-Oct-14 9:15
professionalMuhammad Muddasar Yamin28-Oct-14 9:15 
GeneralRe: SDK version Pin
Member 114955397-Sep-15 6:44
Member 114955397-Sep-15 6:44 
GeneralRe: SDK version Pin
Muhammad Muddasar Yamin7-Sep-15 18:58
professionalMuhammad Muddasar Yamin7-Sep-15 18:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.