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
Printable View
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
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.
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.
Perhaps you might get around using a hook to hook mouse click events. A global hook. It might slow the system down, however.
I've just discovered a windows function - getmousepos();
Would this not work?
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.
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...
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.
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.