Click here to Skip to main content
Licence 
First Posted 8 Apr 2000
Views 48,027
Bookmarked 20 times

A 'mouse repeat' function

By | 8 Apr 2000 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberChristopher Camacho9:38 25 Nov '11  
GeneralExcellent Pinmemberpeter timmins4:02 11 Sep '07  
GeneralA 'mouse repeat' function PinmemberLarsson9:04 2 Aug '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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 Apr 2000
Article Copyright 2000 by Pete Sackett
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid