Click here to Skip to main content
Click here to Skip to main content

A 'mouse repeat' function

, 8 Apr 2000
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Pete Sackett

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberChristopher Camacho25-Nov-11 9:38 
GeneralExcellent Pinmemberpeter timmins11-Sep-07 4:02 
GeneralA 'mouse repeat' function PinmemberLarsson2-Aug-04 9:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 9 Apr 2000
Article Copyright 2000 by Pete Sackett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid