如何模拟鼠标移动
How to simulate a mouse movement
如何模拟导致指针向左移动500像素的鼠标事件,然后使用C++单击。我该怎么做这样的事?
下面是我身边的一些修改过的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;
}
在使用之前,您需要调用MouseSetup()
到每个INPUT
缓冲区。
资源
MSDN-
SendInput()
MSDN-INPUT
MSDN-MOUSEINPUT
以下是针对使用Linux
的用户使用Xlib
的解决方案:
#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, "Errorn");
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errorn");
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;
}
只需构建它,然后模拟点击x,y do:
$ ./a.out x y
即
$g++ -lX11 sgmousesim2.cpp
$./a.out 123 13
使用SendInput
生成要模拟的输入。来自MSDN文档:
合成击键、鼠标移动和按钮单击。
我从未使用过C++。然而,有一个名为Robot的Java类能够生成鼠标事件。我在Java 1.4版本上使用过这个,但它仍然有效。我尝试了在Mac OS X中模拟物理鼠标移动的例子。它在MacOSX Lion上与Oracle Java 1.6.0_26顺利运行。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);
}
}
您仍然可以使用JNI将其与C++绑定。
我希望它能帮助
仅靠C++无法做到这一点。它没有"鼠标"的概念,更不用说"点击"了。
你需要某种能与你的窗口系统对话的图书馆。例如,QT。然后就是搜索API并进行正确的C++调用。
使用mouse_event函数。
相关文章:
- SendInput()鼠标移动计算
- Win32 发送输入鼠标移动滞后并冻结
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- 使用具有设置延迟C++的mouse_event平滑鼠标移动
- Qt将鼠标移动事件转移到新窗口
- Qt 如何使用鼠标移动事件移动视图
- 触发鼠标按下事件,但不触发鼠标移动事件
- 发送到另一个窗口的鼠标移动消息不执行任何操作
- 如何将双精度类型的鼠标移动 x 和 y 从主机传递到 Qemu 中的虚拟机
- GLFW 不会检测到 GLFW 窗口外的鼠标移动
- QT QTest::键单击或鼠标移动似乎不适用于 QMenu 在我的情况下
- MFC GUI 自定义控件:如何绘制光标更新以响应鼠标移动?
- 在Qt5的OpenGL绘图中通过鼠标移动计算对象旋转角度
- SDL2 中的 GLFW 风格的无限鼠标移动
- 如何使用SFML鼠标移动分别移动多个图像
- 当光标锁定在屏幕中时,如何获得鼠标移动
- 当鼠标移动时,如何从wm_mousemove中获得每个像素坐标
- 如何在使用鼠标移动时捕捉QWidget几何图形
- 在Qt中获取原始鼠标移动
- 在eventFilter()中未捕获Qt鼠标移动事件