I'm trying to create a little app that will detect when the mouse cursor moves to the edge of the screen and will move it to the opposite edge, to create a continuous desktop effect, if that makes sense.
Below is some code from someone else (the mouse hook part) I adapted by added the SetCursorPos to move the mouse to a fixed position for now. When I run it, SetCursorPos returns true, which I assume means the call succeeded, but the mouse does not move. I read somewhere something about security constraints in later Windows version preventing stuff like this, which would make sense, but the source was unclear to how true this is. Does anyone know why this would not work?
Thanks, code below:
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL)
{
if (pMouseStruct->pt.x < -1900)
{
BOOL r = SetCursorPos(
500,
500
);
printf("Trigger %d. Response %d", pMouseStruct->pt.x, r);
}
}
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) KeyboardEvent,
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;
}