2

I'm at the moment trying to send a right click to a computergame without affecting my visible mouse pointer.
I'm not sure if the game just doesn't like it or if I'm doing something wrong. Here is the short-version of my code:

WORD tx = 500;
WORD ty = 500;
HWND windowHandle = FindWindow(NULL,TEXT("Game title"));
if (windowHandle != 0) {
    SendMessage(windowHandle, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(tx, ty));
    Sleep(50);
    SendMessage(windowHandle, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(tx, ty));
    Sleep(50);
    std::cout << "message sent..." << std::endl;
}

I tried this many times and made sure that the games window is on top and active etc, but the game won't register my click.. ("message sent..." is printed every time though)
I know that I could also use SendInput, but first I want to make sure that it is really not possible using PostMessage.

Forivin
  • 14,780
  • 27
  • 106
  • 199

1 Answers1

0

I used the below method to simulate a right mouse click in Qt/C++. See if it works :

mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, nX, nY, 0, 0); mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, nX, nY, 0, 0);

The libraries to be used were windows.h and winuser.h

Aakash
  • 1,860
  • 19
  • 30
  • mouse_event just clicks the the current mouse position and when I combine it with MOUSEEVENTF_MOVE it moves my visible mouse pointer. :/ Is my game causing this or is this normal behaviour? – Forivin Nov 24 '13 at 13:34
  • Why are you combining it with MOUSEEVENTF_MOVE ? I actually coded this long back for some project so dont remember whether I used MOVE+CLICK or just CLICK. However make sure you provide the coordinates in the range of 0-65535 for both axis and not in monitor resolution. – Aakash Nov 24 '13 at 15:05
  • I actually wanted to mimic a RDP application so in my case the mouse pointer moving to the click location is what was the desired behaviour. – Aakash Nov 24 '13 at 15:08
  • well i obviously added the move command because it just clicked on my current mouse position. I converted my coordinates into 0-65535 values. (mouse move moved the mouse to the correct position) Well I guess the game handles to mouse position by itself. – Forivin Nov 24 '13 at 15:33
  • As a patch you can move the pointer back to the original location ;) – Aakash Nov 24 '13 at 15:35
  • That's actually what I just did, but the method (moving and clicking) is not really viable because it constantly misses the correct click position when you move the mouse manually while running the program... (I already tried to minimize all delays etc) – Forivin Nov 24 '13 at 15:38