teclado solo simular repetir repetidor que programar programa para movimientos movimiento juegos haga hacer grabar como clicks automaticos automatico c++ windows simulation

c++ - solo - Cómo simular un movimiento del ratón.



repetir click mouse (6)

¿Cómo puedo simular un evento de mouse que hace que el puntero se mueva 500 píxeles hacia la izquierda y luego haga clic con C ++? ¿Cómo haría algo como esto?


Aquí hay un código Win32 modificado que tenía alrededor:

#define WIN32_LEAN_AND_MEAN #define _WIN32_WINNT 0x0500 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <string.h> #include <windows.h> #define X 123 #define Y 123 #define SCREEN_WIDTH 1024 #define SCREEN_HEIGHT 800 void MouseSetup(INPUT *buffer) { buffer->type = INPUT_MOUSE; buffer->mi.dx = (0 * (0xFFFF / SCREEN_WIDTH)); buffer->mi.dy = (0 * (0xFFFF / SCREEN_HEIGHT)); buffer->mi.mouseData = 0; buffer->mi.dwFlags = MOUSEEVENTF_ABSOLUTE; buffer->mi.time = 0; buffer->mi.dwExtraInfo = 0; } void MouseMoveAbsolute(INPUT *buffer, int x, int y) { buffer->mi.dx = (x * (0xFFFF / SCREEN_WIDTH)); buffer->mi.dy = (y * (0xFFFF / SCREEN_HEIGHT)); buffer->mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE); SendInput(1, buffer, sizeof(INPUT)); } void MouseClick(INPUT *buffer) { buffer->mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN); SendInput(1, buffer, sizeof(INPUT)); Sleep(10); buffer->mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP); SendInput(1, buffer, sizeof(INPUT)); } int main(int argc, char *argv[]) { INPUT buffer[1]; MouseSetup(&buffer); MouseMoveAbsolute(&buffer, X, Y); MouseClick(&buffer); return 0; }

Deberá llamar a MouseSetup() a cada búfer INPUT antes de usarlo.

Recursos

MSDN - SendInput()
MSDN - INPUT
MSDN - MOUSEINPUT


C ++ solo no puede hacer esto. No tiene el concepto de un "ratón", y mucho menos un "clic".

Necesita algún tipo de biblioteca que se comunique con su sistema de ventanas. Por ejemplo, QT . Entonces es cuestión de buscar a través de la API y hacer las llamadas correctas de C ++.


Nunca he hecho esto usando C ++. Sin embargo, existe una clase de Java llamada Robot que puede producir eventos de mouse. Usé esto de nuevo en la versión 1.4 de Java, pero todavía funciona. Intenté el ejemplo de este Simular un movimiento físico del mouse en Mac OS X. Funciona sin problemas con Oracle Java 1.6.0_26 en MacOSX Lion. Lo bueno de Java es que es independiente de la plataforma.

import java.awt.AWTException; import java.awt.Robot; public final class MovingMouseDemo { public static void main(String[] args) throws AWTException { Robot robot = new Robot(); robot.setAutoDelay(5); robot.setAutoWaitForIdle(true); //put mouse in the top left of the screen robot.mouseMove(0, 0); //wait so that you can see the result robot.delay(1000); //put the mouse 200 pixels away from the top //10 pixels away from the left robot.mouseMove(200, 10); robot.delay(1000); robot.mouseMove(40, 130); } }

Aún puedes usar JNI para enlazarlo con C ++.

Espero que ayude


Utilice SendInput para generar la entrada que desea simular. De la documentación de MSDN:

Sintetiza pulsaciones de teclas, movimientos del mouse y clics de botones.



Aquí hay una solución que usa Xlib para aquellos que usan Linux :

//sg #include <X11/Xlib.h> #include<stdio.h> #include<unistd.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <X11/Xlib.h> #include <X11/Xutil.h> void mouseClick(int button) { Display *display = XOpenDisplay(NULL); XEvent event; if(display == NULL) { fprintf(stderr, "Errore nell''apertura del Display !!!/n"); exit(EXIT_FAILURE); } memset(&event, 0x00, sizeof(event)); event.type = ButtonPress; event.xbutton.button = button; event.xbutton.same_screen = True; XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state); event.xbutton.subwindow = event.xbutton.window; while(event.xbutton.subwindow) { event.xbutton.window = event.xbutton.subwindow; XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state); } if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error/n"); XFlush(display); usleep(100000); event.type = ButtonRelease; event.xbutton.state = 0x100; if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error/n"); XFlush(display); XCloseDisplay(display); } int main(int argc, char * argv[]) { int x , y; x = atoi(argv[1]); y = atoi(argv[2]); Display *display = XOpenDisplay(0); Window root = DefaultRootWindow(display); XWarpPointer(display, None, root, 0, 0, 0, 0, x, y); mouseClick(Button1); XFlush(display); XCloseDisplay(display); return 0; }

Solo compílelo y luego simule un clic en x, y haga:

$ ./a.out x y

es decir

$g++ -lX11 sgmousesim2.cpp $./a.out 123 13