Infinite Loop with GetAsyncKeyState

This is a discussion on Infinite Loop with GetAsyncKeyState within the Windows Programming forums, part of the Platform Specific Boards category; Heres my code: Code: if ( GetAsyncKeyState ( VK_LBUTTON ) ) { for ( int i = 0; i < ...

  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-15-2009, 12:24 AM
  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, 01: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, 12:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21