Simulate mouse click

Asked

Viewed 1,384 times

1

I need to simulate the click of mouse, but I can’t use PostMessage nor mouse_event. Is there any other way to send the click?

1 answer

1


You can use the API SendInput.

Synthesizes keystrokes, mouse movements and button clicks.

Obs: This function is subject to UIPI. Applications shall be authorised to inject the entree only in applications that are at a level of equal or lower integrity.

Assuming you first want to move the cursor to a specific point, you should use absolute coordinates in the interval between 0 and 65535, therefore, to correctly calculate the coordinates of the screen, use the following function:

function ObterCoordenada(const Indice, Posicao: Integer): Integer;
begin
  Result := (65535 div (GetSystemMetrics (Indice) -1)) * Posicao;
end;

Now, the function responsible for moving the cursor and simulating the left click with the function SendInput:

procedure SimularClick(const X, Y: Integer);
var
Entrada: array [0..1] of TInput;
begin
  ZeroMemory(@Entrada, SizeOf(Entrada));
  // Primeiramente move o cursor do mouse a uma coordenada especifica, em Pixels!
  Entrada[0].Itype := INPUT_MOUSE;
  Entrada[0].mi.dwFlags := MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE;
  Entrada[0].mi.dx := ObterCoordenada(SM_CXSCREEN, X);
  Entrada[0].mi.dy := ObterCoordenada(SM_CYSCREEN, Y);
  SendInput(1, Entrada[0], sizeof(TInput)); // Envia o pedido

  // Realiza o click com o botão esquerdo do mouse
  Entrada[1].Itype := INPUT_MOUSE;
  Entrada[1].mi.dwFlags := MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP;

  SendInput(1, Entrada[1], sizeof(TInput));   // Envia o pedido
end;

Example of use:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SimularClick(625, 486); // Onde "625" é X e "486", Y
end; 
  • I can’t use sendinput either because the program has hooked the function lock. http://pastebin.com/q8JmP6ix, what can I do to bypass this hook?

  • @Fake Complicated, because the hooking works at low level, to cancel the activity of the program that intercepts the mouse calls, you will have to finish it or prevent it from installing the hook , that is, cancel the function SetWindowsHookEx. Here says something about code Injection, here has another approach.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.