|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转无忧吧。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
C++DMA键鼠操作源码-可以直接用到自己项目中
采用的UC论坛上的两个大佬公布的代码提取改造,目前测试可以用,自己复制进自己的项目即可
# C++ DMA 键鼠操作源码
在现代软件开发中,自动化测试、游戏辅助工具以及图形用户界面(GUI)的自动化等场景中,模拟鼠标和键盘操作的需求日益增加。为了实现这一目标,C++语言提供了一个强大的平台——Windows API,通过它我们可以高效地控制键鼠设备。本文将详细介绍如何使用C++结合Windows API来实现DMA(Direct Memory Access)方式下的键鼠操作。
## 1. 基础知识
### 1.1 什么是DMA?
DMA是一种允许外设直接与系统内存进行数据传输的技术,而不需要通过CPU。这种技术能够显著提高数据传输效率,降低CPU负载。对于键鼠操作来说,DMA可以确保输入设备的响应速度更快,从而提升用户体验。
### 1.2 Windows API简介
Windows API是一套由微软提供的应用程序接口,用于开发Windows操作系统下的应用程序。其中,SendInput函数是实现键鼠操作的关键API之一。
## 2. 关键API介绍
### 2.1 SendInput函数
SendInput函数用于发送输入事件,可以模拟键盘和鼠标的操作。该函数定义在user32.h头文件中,其原型如下:
- cpp
- LRESULT SendInput(
- ULONG nInputs,
- IN INPUT* pInputs,
- INT cbSize
- );
复制代码
- `nInputs`:要发送的输入事件的数目。
- `pInputs`:指向一个包含所有输入事件的数组指针。
- `cbSize`:每个输入结构的大小。
### 2.2 INPUT结构体
INPUT结构体用于描述一个输入事件,可以是键盘事件或鼠标事件。其定义如下:
- cpp
- typedef struct tagINPUT {
- union {
- struct {
- USHORT type;
- USHORT wParamL;
- ULONG time;
- PARAM lParam;
- } ki;
- struct {
- USHORT type;
- USHORT wParamH;
- ULONG time;
- PARAM lParam;
- } khi;
- struct {
- USHORT type;
- ULONG wParamL;
- ULONG time;
- PARAM lParam;
- } mi;
- struct {
- USHORT type;
- ULONG wParamL;
- ULONG time;
- PARAM lParam;
- } ma;
- struct {
- USHORT type;
- ULONG wParamL;
- ULONG time;
- PARAM lParam;
- } si;
- BYTE data[8];
- };
- } INPUT, *PINPUT;
复制代码
- `type`:指定输入的类型,例如KEYDOWN、MOUSEMOVE等。
- `wParamL`、`wParamH`、`lParam`:根据不同的类型,这些参数会有不同的意义。
- `time`:事件发生的时间。
- `data`:用于存储实际的数据,例如按键的虚拟键码。
## 3. 实现步骤
### 3.1 初始化环境
在使用SendInput函数之前,需要包含必要的头文件并链接相应的库:
编译时需要链接user32.lib库:
- sh
- g++ -o keymouse_simulator keymouse_simulator.cpp -luser32
复制代码
### 3.2 模拟键盘操作
以下是一个简单的示例代码,用于模拟按下和释放某个键:
- cpp
- void simulateKeyPress(int keyCode) {
- INPUT inputs[2];
- inputs[0].type = INPUT_KEYBOARD;
- inputs[0].ki.wParam = 0; // repeat (0 means no repeat)
- inputs[0].ki.time = 0;
- inputs[0].ki.dwFlags = 0; // 0 means down, nonzero means up
- inputs[0].ki.wVk = keyCode; // virtual-key code for the "a" key
- inputs[1] = inputs[0]; // repeat
- inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP; // up event flag
- SendInput(2, inputs, sizeof(INPUT));
- }
- int main() {
- simulateKeyPress(65); // Press 'A' key
- std::this_thread::sleep_for(std::chrono::seconds(1)); // Delay for demonstration purposes
- simulateKeyPress(65); // Release 'A' key
- return 0;
- }
复制代码
### 3.3 模拟鼠标操作
类似地,我们可以模拟鼠标的移动和点击:
- cpp
- void moveMouseTo(int x, int y) {
- POINT point;
- point.x = x;
- point.y = y;
- SetCursorPos(point.x, point.y); // Set the mouse position to (x, y)
- }
- void clickMouse(int button) {
- INPUT inputs[2];
- inputs[0].type = INPUT_MOUSE;
- inputs[0].mi.dx = 0; // relative X coordinate of mouse movement
- inputs[0].mi.dy = 0; // relative Y coordinate of mouse movement
- inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE; // Mouse movement flag
- inputs[0].mi.mouseData = 0; // additional flags and data (not used in this case)
- inputs[0].mi.time = 0; // Event time (not used in this case)
- inputs[0].mi.dwExtraInfo = 0; // Extra information (not used in this case)
- inputs[1].type = INPUT_MOUSE;
- inputs[1].mi.dx = 0; // Relative X coordinate of mouse movement (same as above)
- inputs[1].mi.dy = 0; // Relative Y coordinate of mouse movement (same as above)
- inputs[1].mi.dwFlags = button == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN; // Left or right button down flag
- inputs[1].mi.mouseData = 0; // Additional flags and data (not used in this case)
- inputs[1].mi.time = 0; // Event time (not used in this case)
- inputs[1].mi.dwExtraInfo = 0; // Extra information (not used in this case)
- SendInput(2, inputs, sizeof(INPUT)); // Send both input events to simulate a mouse click
- }
- int main() {
- moveMouseTo(100, 100); // Move mouse to (100, 100)
- clickMouse(LEFT_BUTTON); // Click left mouse button at (100, 100)
- std::this_thread::sleep_for(std::chrono::seconds(1)); // Delay for demonstration purposes
- moveMouseTo(200, 200); // Move mouse to (200, 200)
- clickMouse(RIGHT_BUTTON); // Click right mouse button at (200, 200)
- return 0;
- }
复制代码
## 4. 完整示例代码
以下是一个完整的示例代码,展示了如何结合键盘和鼠标操作:
```cpp
#include
#include
#include // For std::this_thread::sleep_for()
// Function to simulate key press and release
void simulateKeyPress(int keyCode) {
INPUT inputs[2];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wParam = 0; // repeat (0 means no repeat)
inputs[0].ki.time = 0;
inputs[0].ki.dwFlags = 0; // 0 means down, nonzero means up
inputs[0].ki.wVk = keyCode; // virtual-key code for the "a" key
inputs[1] = inputs[0]; // repeat
inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP; // up event flag
SendInput(2, inputs, sizeof(INPUT));
}
// Function to move the mouse to a specific position and click it
void moveMouseTo(int x, int y) {
POINT point;
point.x = x;
point.y = y;
SetCursorPos(point.x, point.y); // Set the mouse position to (x
DMA _KeyState.cpp:
|
捷云鲸技术社区(bbs.jieyunjing.com)免责声明:
使用本社区服务即视为同意本声明全部条款。
1. 本社区所有技术、工具及内容仅限**学习研究**使用,旨在提升安全技术水平,严禁用于非法、商业及其他不良用途,违规后果由使用者自行承担。
2. 社区内容及资源来源于网络,仅代表发布者个人观点,与本社区无关,相关法律责任由发布者自负。
3. 资源版权归原作者所有,用户下载后须在**24小时内删除**,版权争议与本社区无关。
4. 禁止任何破坏社区正常运营的行为,违规将视情节处理,社区保留追责权利。
5. 本社区坚决支持正版,如涉及侵权,请通过站长邮箱或QQ联系删除。
6. 本社区对本声明拥有最终解释权,可适时修改并公示生效。
站长邮箱:jieyunjingvip@qq.com
站长QQ:4040068
|