Thread: functions interfering

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Question functions interfering

    hello, I'm relatively new to c++ so please bare with me. I'm trying to write a program where the mouse will start moving when a key is pressed then stop when the key is released. I thought I had it but the mouse keeps going after the key is released. I think it's because my mmx function calls itself and the kbHookProc function can't listens for the release but I don't know how to get around this. Can somebody help? I know about mouse keys but I'm working with someone with a disability and have some odd parameters. Here is my code

    Code:
    #define _WIN32_WINNT 0x0500
    #include<windows.h>
    using namespace std;
    
    int xg=0,nmy,nmx,x,y;
    POINT p;
    
    int mmx() {
        
    GetCursorPos(&p);
    x=p.x+1;
    y=p.y;
    
                      SetCursorPos(x, y);  
    Sleep(300);
    if (xg==1) mmx();
    return 0;
    }
    
    LRESULT CALLBACK kbHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
            if(lParam>0) {
    
    xg=1; mmx(); } 
    else {
         xg=0;
     
    }
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nShowCmd) {
    
       // Set mouse hook
       HHOOK mouseHook = SetWindowsHookEx(
                      WH_KEYBOARD,      /* Type of hook */
                      kbHookProc,    /* Hook process */
                      hInstance,        /* Instance */
                      0);
    
       // Wait for user to exit
       MessageBox(NULL, "Press OK to close.", "", MB_OK);
       return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, I see what you mean. I know almost zero about Windows programming, but it seems to me that you have a few ways of solving that: the first is to the main function do the looping. For example, it could use something like
    Code:
    for(;;) {
        if(xg) mmx();
        Sleep(300);
    }
    That means, of course, that you wouldn't be able to call MessageBox(), but perhaps there's a version of the function that allows your main thread to continue executing.

    Alternatively, you could try to register a callback function. Perhaps Windows has a mechanism to allow you to have mmx() called automatically after 300 ms. This may or may not exist.

    You could also create another thread to do the movement, but that would really be overkill!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    after a couple of day of trying to get this to work, I figured out all I had to do is this
    return 0;
    if (xg==1) mmx();

    Quote Originally Posted by dwks View Post
    Hmm, I see what you mean. I know almost zero about Windows programming, but it seems to me that you have a few ways of solving that: the first is to the main function do the looping. For example, it could use something like
    Code:
    for(;;) {
        if(xg) mmx();
        Sleep(300);
    }
    That means, of course, that you wouldn't be able to call MessageBox(), but perhaps there's a version of the function that allows your main thread to continue executing.

    Alternatively, you could try to register a callback function. Perhaps Windows has a mechanism to allow you to have mmx() called automatically after 300 ms. This may or may not exist.

    You could also create another thread to do the movement, but that would really be overkill!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So in other words, just remove the recursion altogether? Because putting some code after a return statement means that that code will never be executed. I suppose it works because kbHookProc() is called for repeated keyboard presses. Makes sense.

    Glad you got it working.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM