Jump to content


Photo

Como simular un Click de ratón


  • Please log in to reply
6 replies to this topic

#1 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4111 posts
  • LocationMadrid - España

Posted 08 July 2009 - 05:05 PM

Para conseguir realizar un Click de ratón deberemos utilizar la API SendInput. Esa misma API sirve para enviar eventos de teclado.
Desde la aparición de Windows NT la API mouse_event se ha suprimido en favor de SendInput.

Aquí­ expongo una función que simula un Click en unas coordenadas absolutas de la pantalla:



cpp
  1. int MouseClick(int x, int y)
  2. {
  3.   TPoint P;
  4.   GetCursorPos(&P); // Guardo las coordenadas del mouse
  5.  
  6.   SetCursorPos(x, y);
  7.  
  8.   INPUT Input[2];
  9.     ::ZeroMemory(Input, sizeof(Input));
  10.   Input[0].type = INPUT_MOUSE;
  11.   Input[0].mi.dx = x;
  12.   Input[0].mi.dy = y;
  13.   Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
  14.  
  15.   Input[1].type = INPUT_MOUSE;
  16.   Input[1].mi.dx = x;
  17.   Input[1].mi.dy = y;
  18.   Input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
  19.   int a = SendInput(2, Input, sizeof(INPUT));
  20.  
  21.   SetCursorPos(P.x, P.y); // Restauro las coordenadas del Mouse
  22.  
  23.   return a;  // Retorno el número de eventos enviados
  24. }



Saludos.
  • 1

#2 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4111 posts
  • LocationMadrid - España

Posted 29 October 2009 - 01:33 PM

Vista la necesidad, traduzco a delphi el código anterior:



delphi
  1. // No he encontrado la estructura INPUT tagINPUT de Windows.h en mi delphi 6 por lo que la defino
  2. // en caso de que la tengáis definida, usar la del delphi.
  3. type
  4.   TINPUT = record
  5.   Tipo: DWORD;
  6.   case integer of
  7.     0:(mi: TMOUSEINPUT;);
  8.     1:(ki: TKEYBDINPUT;);
  9.     2:(hi: THARDWAREINPUT;);
  10.   end;  PTINPUT = ^TINPUT;
  11.  
  12. function MouseClick(x: DWORD; y: DWORD): DWORD;
  13. var
  14.   P: TPoint;
  15.   VInput: array [0..1] of TINPUT;
  16. begin
  17.   GetCursorPos(P); // Guardo las coordenadas del mouse
  18.  
  19.   SetCursorPos(x, y);
  20.  
  21.   ZeroMemory(@VInput, sizeof(VInput));
  22.   VInput[0].tipo:= INPUT_MOUSE;
  23.   VInput[0].mi.dx:= x;
  24.   VInput[0].mi.dy:= y;
  25.   VInput[0].mi.dwFlags:= MOUSEEVENTF_LEFTDOWN;
  26.  
  27.   VInput[1].tipo:= INPUT_MOUSE;
  28.   VInput[1].mi.dx:= x;
  29.   VInput[1].mi.dy:= y;
  30.   VInput[1].mi.dwFlags:= MOUSEEVENTF_LEFTUP;
  31.  
  32.   // Retorno el número de eventos enviados
  33.   Result:= windows.SendInput(2, tagINPUT(VInput[0]), sizeof(TINPUT));
  34.  
  35.   SetCursorPos(P.x, P.y); // Restauro las coordenadas del Mouse
  36. end;



Y esto es una forma de usarlo:



delphi
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   // Hacemos click en las coordenadas (0, 0) de la pantalla
  4.   MouseClick(0,0);
  5. end;



Saludos.
  • 1

#3 rgstuamigo

rgstuamigo

    Member

  • Miembros
  • PipPip
  • 33 posts
  • LocationSanta Cruz-Bolivia

Posted 03 November 2009 - 01:25 PM

Y por que no usar mouse_event? :huh: http://msdn.microsof...260(VS.85).aspx 8-|
Saludos... (y)
  • 0

#4 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4111 posts
  • LocationMadrid - España

Posted 03 November 2009 - 02:08 PM

Y por que no usar mouse_event? :huh: http://msdn.microsof...260(VS.85).aspx 8-|
Saludos... (y)

Pues porque:

mouse_event Function

The mouse_event function synthesizes mouse motion and button clicks.

Windows NT/2000/XP: This function has been superseded. Use SendInput instead.

Syntax

    VOID mouse_event(     
        DWORD dwFlags,
        DWORD dx,
        DWORD dy,
        DWORD dwData,
        ULONG_PTR dwExtraInfo
    );
....................................................

:cheesy:

Saludos.

  • 0

#5 rgstuamigo

rgstuamigo

    Member

  • Miembros
  • PipPip
  • 33 posts
  • LocationSanta Cruz-Bolivia

Posted 03 November 2009 - 02:17 PM

Hahhh bueno... :-#
  • 0

#6 Delphius

Delphius

    Advanced Member

  • Administrador
  • 6295 posts
  • LocationArgentina

Posted 03 November 2009 - 07:27 PM


Y por que no usar mouse_event? :huh: http://msdn.microsof...260(VS.85).aspx 8-|
Saludos... (y)

Pues porque:

mouse_event Function

The mouse_event function synthesizes mouse motion and button clicks.

Windows NT/2000/XP: This function has been superseded. Use SendInput instead.

Syntax

    VOID mouse_event(     
        DWORD dwFlags,
        DWORD dx,
        DWORD dy,
        DWORD dwData,
        ULONG_PTR dwExtraInfo
    );
....................................................

:cheesy:

Saludos.

Vaya... yo también voy a tener que tomar nota... más de una vez he estado usando esa API :s
  • 0

#7 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 posts
  • Location127.0.0.1

Posted 13 July 2016 - 06:42 PM

Hola, Perdon por llegar tarde.

 

Una informacion adicional.... tambien puedes usar un Mouse Hook.

 

 

Hooking into the Windows API is a complicated process and there are several techniques to achieve this. Microsoft recommends that the callback procedure for most hooks should reside within a DLL, this is the method used by the TCPMouseHook Component. This Component encapsulates the procedures and functions within a separate DLL and uses Memory Mapped File (MMF) to send a custom message with the information to the calling application or process. The result is a system wide hook into all global mouse events generated by the user mouse input.

Features:

  • Supports Windows 95/98/ME/NT/2000/XP.
  • Ultra small DLL with almost no overhead or impact on system resources.
  • Simple component with one Event passing mouse position and button states.
  • True Global, System Wide Mouse Hook on all running Threads/Applications.
  • Disable Pointer or Individual Mouse Buttons from being processed by windows.
  • Swap the Left and Right Mouse Buttons.
  • Invert/Mirror or Wrap the Mouse Pointer Cursor.
  • Event Returns X and Y absolute and relative positions of Mouse Cursor.
  • Event Returns State of Buttons and Scroll Wheel.
  • Event Returns Hit Point Codes of the current Mouse Cursor.
  • Indication if Mouse Cursor is within certain areas of a Window.
  • Custom message can be set for communication with the DLL.
  • Install as a Component or use Unit and Functions directly.

 

http://torry.net/vcl...e/mousehook.zip

 

Saludos! ;)


  • 2




IP.Board spam blocked by CleanTalk.