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
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
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.
I've just discovered a windows function - getmousepos();
Would this not work?
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:
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?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...
Last edited by Jake.c; 01-17-2009 at 05:33 PM.
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).
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; }
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.