Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC
Article

A 'mouse repeat' function

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
8 Apr 2000 58.3K   22   3
A function that simulates the keyboard repeat behavior for mouse clicks

This executes DoClickThing() (any function you wish to have called on a WM_LBUTTONDOWN message) as long as

  1. the mouse button is down and
  2. the mouse pointer remains within myRect.

You need to override your OnLButtonDown, OnMouseMove, OnLButtonUp and OnTimer overrides in your CWnd derived class. The repeat rate is set to simulate the keyboard repeat behavior.

Thanks to Christian Sloper for the idea.

void CTestCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
    if(PtInRect(&myRect, point))
    {
        DoClickThing();
        SetCapture();

        // initial timer interval = keyboard repeat delay
        int setting = 0;
        SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, &setting, 0);
        int interval = (setting + 1) * 250;
        TimerID = SetTimer(99, interval, NULL);
        TimerStep = 1;
    }
}

void CTestCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
    if(TimerStep && !PtInRect(&myRect, point))
    {
        KillTimer(TimerID);
        ReleaseCapture();
        TimerStep = 0;
    }
}

void CTestCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
    if(TimerStep)
    {
        KillTimer(TimerID);
        ReleaseCapture();
        TimerStep = 0;
    }
}

void CTestCtrl::OnTimer(UINT nIDEvent)
{
    if(TimerStep == 1)
    {
        KillTimer(TimerID);

        // set timer interval per keyboard repeat rate
        DWORD setting = 0;
        SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &setting, 0);
        int interval = 400 - (setting * 12);
        TimerID = SetTimer(100, interval, NULL);
        TimerStep = 2;
    }

    if(TimerStep)
        DoClickThing();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Christopher Camacho25-Nov-11 9:38
Christopher Camacho25-Nov-11 9:38 
GeneralExcellent Pin
peter timmins11-Sep-07 4:02
peter timmins11-Sep-07 4:02 
GeneralA 'mouse repeat' function Pin
Larsson2-Aug-04 9:04
Larsson2-Aug-04 9:04 

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.