Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A 'mouse repeat' function

0.00/5 (No votes)
8 Apr 2000 4  
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