Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / C++
Article

The Poor Man's Mouse Gesture

Rate me:
Please Sign up or sign in to vote.
4.84/5 (42 votes)
15 Jun 2005CPOL5 min read 88.1K   2.4K   55   20
An easy to use class for adding basic mouse gesture recognition to your application.

Introduction

In a project I am currently working on I had the need for the ability to handle simple mouse gestures. I wanted to be able to do stuff like change the current selection in a combo box by simply moving the mouse up or down, and not having to switch the input focus from where I was working to the combo box and back again. I found a C++ article here[^] on The Code Project about mouse gestures, but it seemed to be more of an intellectual treatise on using neural networks to recognize predetermined mouse gestures rather than a ready to use class library that could be plugged into any project others might be working on (or maybe I am just too dense to figure it out Image 1). As I am not one who likes to spend hours searching all over the web trying to find something that may or may not be there, I decided that the best way to get exactly what I wanted was to write it myself. This class and the article are the results of my efforts. Enjoy Image 2.

The idea (or how it works)

My idea on how to get this thing to work is actually quite simple. First of all, it involves setting up a mouse hook to track what the user is doing with the mouse. When the user presses one of the mouse buttons down, I start the mouse gesture by setting up a bounding square around the point where the mouse is located. If the user moves the mouse over the edge of the square I have a mouse gesture. The idea is to move the square along with the mouse, keeping track of which edge the mouse moves over. Every time the mouse moves over a different edge I get a new direction in the gesture.

Image 3

Sequence for mouse gesture 'right and down'

You can get visual feedback of the mouse tracks and the bounding square in action if you download and build the demonstration application in DEBUG mode. You can turn the visual feedback off by commenting out line number 55 in the MouseGesture.cpp file.

//////////////////////////////////////////////////////////////////////
// Uncomment the #define _SHOW_GESTURE line to draw mouse points and
// bounding squares on screen when the gesture is being made. 
//////////////////////////////////////////////////////////////////////

#   define _SHOW_GESTURE  // this line controls visual feedback

Using the CMouseGesture class

The CMouseGesture class is a simple class that you can plug into any C++ Windows code so that you too can have mouse gesture recognition in your applications. It is really quite simple. Just add the class files, MouseGesture.cpp and MouseGesture.h, to your project. Declare a CMouseGesture object, tell it which window to watch for mouse gestures, and what gestures to watch for. When the CMouseGesture object recognizes a mouse gesture it posts a registered message to the window it's watching, passing the ID of the gesture. To create the ID of the registered message pass the MOUSE_GESTURE_MESSAGE_STRING to the RegisterWindowMessage() function. The WPARAM parameter of the registered message will be the identification number of the gesture recognized, and the LPARAM parameter will be a pointer to the CMouseGesture object that posted the message.

The following code snippet sets up the CMouseGesture object to watch for a mouse gesture where the user presses the right mouse button, then moves the mouse to the right, then down, and then lets up the right mouse button again:

#include "MouseGesture.h"
#define ID_GESTURE_RIGHT_DOWN 1001

const UINT WMU_MOUSEGESTURE = 
       RegisterWindowMessage(MOUSE_GESTURE_MESSAGE_STRING);
.
.
.
    // Setup the CMouseGesture object
    CMouseGesture MyMouseGesture;
    MyMouseGesture.Attach(GetSafeHwnd());
 
    CMouseGesture::Motion WatchFor[] = { CMouseGesture::RightButton,
                                         CMouseGesture::Right,
                                         CMouseGesture::Down };
                                         
    MyMouseGesture.AddGesture(ID_GESTURE_RIGHT_DOWN, WatchFor, 3);
.
.
.
// Handle WMU_MOUSEGESTURE message
LRESULT CMyWindowClass::OnMouseGesture(WPARAM wp, LPARAM lp)
{
    UINT GestureID = (UINT)wp;
    CMouseGesture *pMouseGesture = (CMouseGesture *)lp;
    if (GestureID == ID_GESTURE_RIGHT_DOWN)
    {
        // Do whatever
    }
}

CMouseGesture class members

CMouseGesture::CMouseGesture()

CMouseGesture class constructor

Initializes all the internal variables.

Parameters:

None

Returns:

Nothing

CMouseGesture::~CMouseGesture()

CMouseGesture class destructor

Cleans up the class and removes the mouse hook if it is no longer needed.

Parameters:

None

Returns:

Nothing

bool CMouseGesture::Attach(HWND hWnd,
                  UINT Distance /* = 25 */)

CMouseGesture::Attach

Initializes the CMouseGesture object, attaching it to the supplied window handle and starting the mouse hook.

Parameters:

  • hWnd

    The window handle of the window that this CMouseGesture is to watch. The CMouseGesture object will post a registered message to this window whenever it recognizes a mouse gesture.

  • Distance

    The minimum distance in pixels that the mouse has to move before a mouse motion is registered. This parameter is optional and defaults to 25 pixels.

Returns:

  • true, if successful.
  • false, if not successful.
void CMouseGesture::Detach()

CMouseGesture::Detach

Detaches the CMouseGesture object from the window it was attached to in the Attach function. Stops the mouse hook if it is no longer needed.

Parameters:

None

Returns:

Nothing

bool CMouseGesture::RemoveGesture(UINT nID)

CMouseGesture::RemoveGesture

Removes the gesture with the supplied nID from the CMouseGesture object.

Parameters:

  • nID

    The identification number of the gesture to remove.

Returns:

  • true, if the gesture with the identification number was found and removed.
  • false, if the gesture with the identification number was not found.
int CMouseGesture::AddGesture(UINT nID,
               const CMouseGesture::Motion* Motions,
               size_t count)
int CMouseGesture::AddGesture(UINT nID,
             const CMouseGesture::Gesture& rGesture)

CMouseGesture::AddGesture

Use these overloaded functions to add mouse gesture patterns to the CMouseGesture object.

Parameters:

  • nID

    The identification number of the gesture pattern. This number has to be unique to this CMouseGesture object. (A different CMouseGesture object can use the same ID.) This is the number that is passed back to the window when the gesture is recognized.

  • Motions

    A pointer to an array of Motions. The Motion type is an enum defined in the CMouseGesture class.

  • count

    The number of Motion elements in the Motions array.

  • rGesture

    A reference to a Gesture vector containing an array of Motions.

Returns:

  • -2

    The supplied mouse gesture has already been added.

  • -1

    The supplied ID number is already in use.

  • 0

    The supplied mouse gesture has an invalid format or nID is zero.

  • >0

    The gesture was successfully added, and the return value is the number of gestures in the CMouseGesture object.

Note: The CMouseGesture::Motion type is an enum, and the CMouseGesture::Gesture type is a typedef std::vector<Motion>.

enum Motion { None         = 0x0000,

              // These are the possible directions
              Up           = 0x0001,
              Down         = 0x0002,
              Left         = 0x0003,
              Right        = 0x0004,

              // These are for the shift and control keys
              Shift        = 0x0005,
              Control      = 0x0006,

              // These are bit flags for the mouse buttons
              LeftButton   = 0x0100,
              MiddleButton = 0x0200,
              RightButton  = 0x0400
            };

typedef std::vector<Motion> Gesture;

When you build the array (or vector) of Motions to pass to the AddGesture function you must make sure that the array has the following format:

MouseButton [Shift] [Control] Motion ...

The mouse button that is pressed down to start the mouse gesture must be the first element in the array. It can be one of LeftButton, MiddleButton, or RightButton. Following the mouse button can be the optional Shift and/or Control flags. Use Shift if the gesture requires the SHIFT key on the keyboard to be pressed. Use Control if the CTRL key is supposed to be pressed. If you want to use both, then Shift has to be first, and Control has to be second. After that comes one or more direction (Up, Down, Left, or Right) motions. You can not use the same direction motion twice immediately together.

License

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


Written By
President
Canada Canada
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.

Comments and Discussions

 
QuestionVB version? Pin
Robert Dudley23-Apr-12 9:49
Robert Dudley23-Apr-12 9:49 
Generaltrack the gesture anywhere Pin
Yuval Twig30-Jan-06 11:38
Yuval Twig30-Jan-06 11:38 
GeneralRe: track the gesture anywhere Pin
PJ Arends30-Jan-06 21:02
professionalPJ Arends30-Jan-06 21:02 
GeneralA different detection Pin
tmangan22-Jun-05 5:53
tmangan22-Jun-05 5:53 
GeneralRe: A different detection Pin
PJ Arends22-Jun-05 6:16
professionalPJ Arends22-Jun-05 6:16 
GeneralRe: A different detection Pin
tmangan22-Jun-05 6:47
tmangan22-Jun-05 6:47 
GeneralRe: A different detection Pin
redfish3421-Jan-06 10:07
redfish3421-Jan-06 10:07 
I see a problem with the cell grid approach. What if the gesture includes movements that reverse? Example, what if the user wants to move Right-Left-Right, make two horizontal lines? The cell grid approach fails because it only takes a snapshot of the final gesture state. It is unable able to distinguish between a single line and double-line gesture.

PJ Arends solution is pretty elegant. I dismissed it at first because the demo was sporadic. But taking a second look after looking at those neural network solutions and others, i think this 20th century-ish technology is quite adequate for the "working" programmer.



GeneralRe: A different detection Pin
tmangan21-Jan-06 11:06
tmangan21-Jan-06 11:06 
GeneralRe: A different detection Pin
Kevin Pinkerton24-Jun-05 4:55
Kevin Pinkerton24-Jun-05 4:55 
GeneralRe: A different detection Pin
tmangan24-Jun-05 8:13
tmangan24-Jun-05 8:13 
GeneralEgo a gogo Pin
DanMatthewsUK21-Jun-05 0:12
DanMatthewsUK21-Jun-05 0:12 
GeneralRe: Ego a gogo [modified] Pin
PJ Arends21-Jun-05 12:04
professionalPJ Arends21-Jun-05 12:04 
GeneralRe: Ego a gogo Pin
DanMatthewsUK21-Jun-05 23:00
DanMatthewsUK21-Jun-05 23:00 
QuestionMouse Gesture? Pin
David Crow20-Jun-05 6:48
David Crow20-Jun-05 6:48 
GeneralBravo Pin
Stuart Konen16-Jun-05 13:40
Stuart Konen16-Jun-05 13:40 
GeneralRe: Bravo Pin
PJ Arends17-Jun-05 6:43
professionalPJ Arends17-Jun-05 6:43 
GeneralPut a little AI in it, ... Pin
WREY16-Jun-05 10:05
WREY16-Jun-05 10:05 
GeneralRe: Put a little AI in it, ... Pin
PJ Arends16-Jun-05 11:19
professionalPJ Arends16-Jun-05 11:19 
GeneralRe: Put a little AI in it, ... Pin
WREY16-Jun-05 11:27
WREY16-Jun-05 11:27 
GeneralModified Pin
PJ Arends15-Jun-05 9:34
professionalPJ Arends15-Jun-05 9:34 

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.