Thread: Get coordinates on screen when user clicks?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Get coordinates on screen when user clicks?

    Are there any functions (I presume it would be Windows API) that are compatible with C, that when the user clicks somewhere on the screen, it returns the coordinates of where the user clicked?

    Cheers,

    Jake

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, Windows sends messages to your windows when events occurs.
    When a click occurs, it sends a message for one of single/double left/right/middle clicks, I think.
    But as for the entire screen... I don't know if/how it is possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Hmm okay. Because I was trying to make a macro that when a user clicks a point on a screen I could then use setcursorpos to keep clicking it using keybd_event functions.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps you might get around using a hook to hook mouse click events. A global hook. It might slow the system down, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    I've just discovered a windows function - getmousepos();

    Would this not work?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, it will return the coordinates of the mouse at any given time, but it won't help you find out when a user clicks somewhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    I'm a bit of a newb at knowing what works with programming and what doesn't but could I not have something along the lines of:

    Code:
    #include <windows.h>
    
    int main(void)
    {
    int g;
    
    printf("Press Enter and Click in the place to repeat clicks");
    getchar();
    g=1;
    
    while(g==1) //Loops and uses if function to wait for the user to click
    {
              if(GetAsyncKeyState(VK_LBUTTON))
              {
                        GetMousePos(); //Not sure how to use this yet, as soon as user clicks it returns the position...?
              }
    }
    
    etc...
    This should (in my newby theory) loop while waiting for the user to click (if function) and then as soon as this happens, the position is returned. Would this work?
    Last edited by Jake.c; 01-17-2009 at 05:33 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will also consume 100% on one cpu core. So, all in all, a bad solution.
    If you want to do it that way, I would suggest using buffered input with DirectInput.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Hmm then that's a no go. I'm sure there must be a way to do it... I've seen it done in auto-clickers before (Press button to toggle - click where you want the coordinates to be).

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    #pragma comment( lib, "user32.lib" )
    
    #include <windows.h>
    #include <stdio.h>
    
    HHOOK hMouseHook;
    
    __declspec(dllexport) LRESULT CALLBACK MouseEvent (int nCode, WPARAM wParam, LPARAM lParam)
    {
        MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
       if (pMouseStruct != NULL && wParam == WM_LBUTTONDOWN)
    	        printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
        return CallNextHookEx(hMouseHook,
            nCode,wParam,lParam);
    }
    
    void MessageLoop()
    {
        MSG message;
        while (GetMessage(&message,NULL,0,0)) {
            TranslateMessage( &message );
            DispatchMessage( &message );
        }
    }
    
    DWORD WINAPI MyMouseLogger(LPVOID lpParm)
    {
        HINSTANCE hInstance = GetModuleHandle(NULL);
        if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm); 
        if (!hInstance) return 1;
    
        hMouseHook = SetWindowsHookEx (  
            WH_MOUSE_LL,
            (HOOKPROC) MouseEvent,  
            hInstance,                 
            NULL                       
            );
        MessageLoop();
        UnhookWindowsHookEx(hMouseHook);
        return 0;
    }
    
    int main(int argc, char** argv)
    {
        HANDLE hThread;
        DWORD dwThread;
         hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
            MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
        if (hThread)
            return WaitForSingleObject(hThread,INFINITE);
        else return 1;
    }

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    You're going to need to hook the desktop's window procedure. Look up msdns SetWindowsHookEx() function. That should give you an idea on where to start.

    EDIT: Bob beat me to it, and then some.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get relative mouse coordinates when GLUT return absolute
    By joeprogrammer in forum Game Programming
    Replies: 14
    Last Post: 02-10-2009, 06:35 PM
  2. Linux Problem Screen Resoultion
    By Matus in forum Tech Board
    Replies: 1
    Last Post: 01-29-2009, 04:39 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. pic movement
    By pode in forum Game Programming
    Replies: 31
    Last Post: 08-21-2002, 09:30 PM