简体   繁体   English

如何模拟鼠标移动

[英]How to simulate a mouse movement

How can I simulate a mouse event causing the pointer to move 500 pixels to the left, then click using C++.如何模拟鼠标事件导致指针向左移动 500 个像素,然后使用 C++ 单击。 How would I do something like this?我将如何做这样的事情?

Here's some modified Win32 code I had lying around:下面是一些修改过的 Win32 代码:

#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;
}

You'll need to call MouseSetup() to each INPUT buffer before you use it.在使用之前,您需要对每个INPUT缓冲区调用MouseSetup()

Resources资源

MSDN - SendInput()MSDN - SendInput()
MSDN - INPUT MSDN - INPUT
MSDN - MOUSEINPUT MSDN - MOUSEINPUT

Here is a solution using Xlib for those who use Linux :这是一个使用Xlib的解决方案,适用于那些使用Linux

#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;
}

Just Build it and then to simulate a click at x ,y do:只需构建它,然后模拟 x 处的点击,y 执行以下操作:

$ ./a.out x y

ie IE

$g++ -lX11 sgmousesim2.cpp

$./a.out 123 13

Use SendInput to generate the input you want to simulate.使用SendInput生成要模拟的输入。 From MSDN documentation:来自 MSDN 文档:

Synthesizes keystrokes, mouse motions, and button clicks.合成击键、鼠标动作和按钮点击。

I have never did this using C++.我从未使用 C++ 这样做过。 Nevertheless, there is a Java class called Robot which is able to produce mouse events.尽管如此,有一个名为 Robot 的 Java 类能够产生鼠标事件。 I used this back on Java version 1.4 but it does still work.我在 Java 1.4 版上使用了它,但它仍然有效。 I tried the example from this Simulate a physical mouse move in Mac OS X .我尝试了这个Simulate a physical mouse move in Mac OS X 中的示例。 It runs smoothly with Oracle Java 1.6.0_26 on MacOSX Lion.它在 MacOSX Lion 上与 Oracle Java 1.6.0_26 一起顺利运行。 The good about Java is that it is platform independent. Java 的优点在于它是独立于平台的。

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);
  }
}

You can still use JNI to bind it with C++.您仍然可以使用 JNI 将其与 C++ 绑定。

I hope it helps我希望它有帮助

C++ alone can't do this. C++本身无法做到这一点。 It has no concept of a "mouse", let alone a "click".它没有“鼠标”的概念,更不用说“点击”了。

You need some sort of library which talks to your windowing system.您需要某种与您的窗口系统对话的库。 For example, QT .例如, QT Then it's a matter of searching through the API and making the right C++ calls.然后就是搜索 API 并进行正确的 C++ 调用。

使用mouse_event函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM