Saturday, March 17th, 2012, 460 days ago

Using hand gestures to control mouse pointer in c sharp

mouse-cursor-hand-pointer

Why mapping hand gestures to system mouse pointer controls?

Hand gesture recognition, including hand gestures and movements, is becoming increasingly attractive. Many people are currently working on it to achieve natural interaction. One important step between hand gesture recognition and natural-interaction applications is to map detected hand gestures to system mouse pointer control. In one of previous articles here.

In this article an simple example is provided to demonstrate how to map detected hand gestures’ movements to system mouse pointer position using c#.

An example

Because my current work is focusing on c#, this example is written in c#. For other programme languages, such as c++ and c, the principle is similar, the detail functions may be different though.

Below is the source codes of this example, comments are given to explain what does each line mean.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace pointerCtrl
{
class Program
{
//load system dll file user32.dll
[DllImport("user32.dll")]
//define a static function as entrance of user32.dll
static extern int SetCursorPos(int x, int y);

//the main function
static void Main(string[] args)
{
//a 1000 loop
for (int i = 0; i < 1000; i++)
{
//key part: set mouse position to specific position
SetCursorPos(i, i);
//press any key to continue to next position
Console.ReadKey(true);
}
}
}
}


Step further – more operations with mouse pointer control

There are other functions specified to take over system mouse pointer control.

[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int x, int y, unit data, UIntPrt extraInfo);
[Flags]
enum MouseEventFlag : Unit
{
Move = 0×0001,
LeftDown = 0×0002,
LeftUp = 0×0004,
RightDown = 0×0008,
RightUp = 0×0010,
MiddleDown = 0×0020,
MiddleUp = 0×0040,
XDown = 0×0080,
XUp = 0×0100,
Wheel = 0×0800,
VirtualDesk = 0×4000,
Absolute = 0×8000
}

It is not working to use
mouse_event(MouseEventFlag.LeftDown, 100,100,0,UIntPtr.Zero)
to trigger a mouse left button click at [10,10].

To realise that, there needs two steps.
1) moving pointer to right position [10,10].
2) triggering a left button click event, by using
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero).


QR code for this post, SCAN ME
QR:  Using hand gestures to control mouse pointer in c sharp

POSTS MAY BE OF INTEREST


3 Responses to “Using hand gestures to control mouse pointer in c sharp”

  1. David XP says:

    hello,I am curious about hands gestures to control mouse pointer.I am confused about left mouse or right mouse is distinguished. Thanks for reading my confusion.
    David

    • Andol says:

      @David
      as posted in the code example, there are several parameters used to identify specific mouse operations, including mouse left/right click, hold and drag, and so on.

  2. ASIM RAZA says:

    HI ALL
    CAN ANYBODY SHARE WITH ME CODE FOR
    “USING HAND GESTURES TO CONTROL THE MOUSE POINTER IN C/C++”

    THANKX

Leave a Reply

You can use these XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>