Thread: Infinite Loop with GetAsyncKeyState

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Infinite Loop with GetAsyncKeyState

    Heres my code:
    Code:
    if ( GetAsyncKeyState ( VK_LBUTTON ) )
            {
    
                for ( int i = 0; i < 3; i++ )
                {
                    Sleep ( 30 );
                    
                    
                    if ( macro.mouseMacro == 1 )
                    {   
                        left_click(); //function i created to simulate left click
                    }
                    else
                    {
                        sk.SendKeys(macro.keyboardMacro.c_str());
                    } // end else
                } // end for
            } // end if
    For some reason, when I click once, it goes in an infinite loop and keeps simulating a left click, when its actually only supposed to do it only 3 times.

    Left Click Function
    Code:
    int left_click()
    {
        // left mouse button down
        INPUT    Input={0};
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
        ::SendInput(1,&Input,sizeof(INPUT));
        
        // left mouse button up
        ::ZeroMemory(&Input,sizeof(INPUT));
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
        ::SendInput(1,&Input,sizeof(INPUT));
    }
    any help will be appreciated.

    thanks,

    guitarist809
    ~guitarist809~

  2. #2
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    If you are using this to simulate clicks on your own application, then there could be a problem. When you simulate a left click, the message pump will pick it up and send a WM_LBUTTONDOWN message to that program. if you are sending this simulated key to your own program it will cause an infinite loop.

    Also, I noticed in a few cases in my own programs, that some windows API calls will invoke messages to your program, and your program will act upon them. This can cause an infinite loop. You can try putting a break point before the SendInput() call. Once you break on that breakpoint, put another breakpoint in your message handing procedure to see if you are receiving any messages before SendInput() ends.

    Its worth a shot, since your logic is correct.
    Founder and avid member of the Internationsl Typo Associateion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Escaping an infinite loop
    By 00Sven in forum C Programming
    Replies: 12
    Last Post: 03-04-2006, 09:48 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM