Click here to Skip to main content
15,881,600 members
Articles / General Programming / Algorithms
Tip/Trick

The Kinect Mouse Controller C#

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
2 Nov 2012CPOL2 min read 94.5K   3.2K   12   29
In this tip, I will describe how to implement a cursor controller in your project that use the Kinect to control the mouse of your PC.

The zip file provided with this article contains a DLL file KinectMouseController.DLL that could be used in all kinds of C# projects to control the cursor of Windows OS system from a Kinect device.

This schema explains how the provided DLL could be useful to send position (x,y) from the Kinect device to the Windows cursor:

Image 1

Introduction

Controlling the cursor using the Kinect is a common question that I have noticed in many web sites, This feature could help a lot of people to make something more interactive, Designer, Architect, exploring maps, exploring 3D building ,... etc.

So in this quick description, I will explain how to add this feature to your project and also provide an application that could help people who aren't familiar with User32.dll and Kinect SDK.

Background

Having an experience with Kinect SDK isn't a must to benefit from this DLL,you should have some basics in coding a Kinect application to implement cursor controller in your Kinect project and use my code.

You should have a Kinect device, install the Kinect SDK and have hardware requirements to benefit from Kinect device in your computer.

Learn my first article about the Kinect for more details, and take a look at this.

What is User32 ?

User32 is one of the DLLs cached on system32 of Windows OS.

It's a Microsoft DLL that provides a lower-level control of Windows, menu, mouse, keyboard,... etc, it's based on many native methods; MOUSE_EVENT etc. that allows a control of a Windows user interface.

Using the Code

To use the DLL KinectMouseController in your project, you should first add a reference to this DLL in your project.

So after creating a project that will use the Kinect SDK, right click the reference folder of your project:

Image 2

Choose the Browse tab, and select the KinectMouseController.DLL:

Image 3

In your .cs file where you need to use the mouse controller, add a using KinectMouseController:

Image 4

Now, you can use the following method:

C#
SendMouseInput(int CursorX, int CursorY,int ScreenWidth, int ScreenHeight, bool isClicked)

In this method, we give the mouse position, the screen dimension and a boolean that represents a mouse click, true if down and false if up.

C#
KinectMouseController.KinectMouseMethods.SendMouseInput(cursorX, cursorY, 
 (int)SystemInformation.PrimaryMonitorSize.Width, 
 (int)SystemInformation.PrimaryMonitorSize.Height, true); 

This code could be used in nui_SkeletonFrameReady() of Kinect SDK, in this code I'll use it to control the mouse using my right hand ,e.g :

C++
void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
       { 
            SkeletonFrame allSkeletons = e.SkeletonFrame;

            //get the first tracked skeleton
            SkeletonData skeleton = (from s in allSkeletons.Skeletons 
                                     where s.TrackingState == SkeletonTrackingState.Tracked
                                     select s).FirstOrDefault();
            //Test if skelton exist and tracked
            if (skeleton != null && skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                SwG.Add(skeleton.Joints[JointID.HandRight].Position, nui.SkeletonEngine);

                // scale those Joints to the primary screen width and height 
                Joint scaledRight = skeleton.Joints[JointID.HandRight].ScaleTo((
                    int)SystemInformation.PrimaryMonitorSize.Width, 
                    (int)SystemInformation.PrimaryMonitorSize.Height, 
                    SkeletonMaxX, SkeletonMaxY);

                int cursorX = (int)scaledRight.Position.X;
                int cursorY = (int)scaledRight.Position.Y;
KinectMouseController.KinectMouseMethods.SendMouseInput
	(cursorX, cursorY, (int)SystemInformation.PrimaryMonitorSize.Width, 
	(int)SystemInformation.PrimaryMonitorSize.Height, false); 
          }

In the nui_SkeletonFrameReady(), we detect the first Skelton, after that we get the right hand portion of this skelton cursorX and cursorY, those coordinates are passed to the SendMouseInput() method of the KinectMouseController to take control of the mouse using the right hand of the first detected Skelton.

Points of Interest

I also wrote a related article:

Note

This code is tested with Windows 7, Kinect SDK beta 1 and Visual Studio 2010.

License

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


Written By
Technical Writer SaadMos
Morocco Morocco
currently an IT Manager (ITSM), with background in Web, Mobile and Could
specialized in Web and software: JavaScript; Wakanda; ASP.NET, C #, WPF, JAVA, C... (SQL Server, jQuery, AJAX, ...)
I am also interested to mobile: Kinect, iOS, hybrid mobile app, and Android
To contact me : saadmos06@gmail.com

Comments and Discussions

 
Questionflash with kinect Pin
Member 1251581010-May-16 19:04
Member 1251581010-May-16 19:04 
Questiongood day! Pin
Ralph Jason Austin Fonte15-Sep-14 22:20
Ralph Jason Austin Fonte15-Sep-14 22:20 
AnswerRe: good day! Pin
Saad Mousliki15-Sep-14 22:29
professionalSaad Mousliki15-Sep-14 22:29 
GeneralRe: good day! Pin
Ralph Jason Austin Fonte16-Sep-14 15:33
Ralph Jason Austin Fonte16-Sep-14 15:33 
QuestionCode Pin
Minh Map Vu12-Feb-14 17:17
Minh Map Vu12-Feb-14 17:17 
AnswerRe: Code Pin
Saad Mousliki12-Feb-14 17:56
professionalSaad Mousliki12-Feb-14 17:56 
QuestionThe Kinect mouse controller C# Pin
miguel061222-Nov-12 19:37
miguel061222-Nov-12 19:37 
AnswerRe: The Kinect mouse controller C# Pin
Saad Mousliki22-Nov-12 22:36
professionalSaad Mousliki22-Nov-12 22:36 
Generalhihi Pin
hoangquybao4-Sep-12 22:02
hoangquybao4-Sep-12 22:02 
GeneralRe: hihi Pin
Saad Mousliki4-Sep-12 23:54
professionalSaad Mousliki4-Sep-12 23:54 
GeneralRe: hihi Pin
hoangquybao5-Sep-12 19:20
hoangquybao5-Sep-12 19:20 
GeneralRe: hihi Pin
Saad Mousliki14-Sep-12 0:32
professionalSaad Mousliki14-Sep-12 0:32 
GeneralRe: hihi Pin
hoangquybao16-Sep-12 13:55
hoangquybao16-Sep-12 13:55 
QuestionCould you help me please Pin
Chinnawat Heranya11-Aug-12 6:14
Chinnawat Heranya11-Aug-12 6:14 
AnswerRe: Could you help me please Pin
Saad Mousliki14-Aug-12 3:07
professionalSaad Mousliki14-Aug-12 3:07 
QuestionControl Windows 7 with Kinect SDK with Source Code Pin
alhazmi112-May-12 17:53
alhazmi112-May-12 17:53 
AnswerRe: Control Windows 7 with Kinect SDK with Source Code Pin
Saad Mousliki2-May-12 22:05
professionalSaad Mousliki2-May-12 22:05 
GeneralRe: Control Windows 7 with Kinect SDK with Source Code Pin
alhazmi112-May-12 22:33
alhazmi112-May-12 22:33 
Questionthe kinect Pin
jimbobwillarby2-May-12 16:02
jimbobwillarby2-May-12 16:02 
QuestionThanks I see Pin
Kimmina29-Apr-12 4:21
Kimmina29-Apr-12 4:21 
AnswerRe: Thanks I see Pin
Saad Mousliki29-Apr-12 5:31
professionalSaad Mousliki29-Apr-12 5:31 
GeneralRe: Thanks I see Pin
Kimmina29-Apr-12 15:20
Kimmina29-Apr-12 15:20 
GeneralRe: Thanks I see Pin
Saad Mousliki30-Apr-12 0:56
professionalSaad Mousliki30-Apr-12 0:56 
Questionimages Pin
Saad Mousliki25-Apr-12 6:33
professionalSaad Mousliki25-Apr-12 6:33 
hi,

are the images still not displayed ?
QuestionKinect SDK Beta 1 or RTW v1? Pin
gduncan41123-Apr-12 6:21
gduncan41123-Apr-12 6:21 

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.