C++DMA键鼠操作源码-可以直接用到自己项目中
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;
};
} INPUT, *PINPUT;
- `type`:指定输入的类型,例如KEYDOWN、MOUSEMOVE等。
- `wParamL`、`wParamH`、`lParam`:根据不同的类型,这些参数会有不同的意义。
- `time`:事件发生的时间。
- `data`:用于存储实际的数据,例如按键的虚拟键码。
## 3. 实现步骤
### 3.1 初始化环境
在使用SendInput函数之前,需要包含必要的头文件并链接相应的库:
cpp
#include
#include
编译时需要链接user32.lib库:
sh
g++ -o keymouse_simulator keymouse_simulator.cpp -luser32
### 3.2 模拟键盘操作
以下是一个简单的示例代码,用于模拟按下和释放某个键:
cpp
void simulateKeyPress(int keyCode) {
INPUT inputs;
inputs.type = INPUT_KEYBOARD;
inputs.ki.wParam = 0; // repeat (0 means no repeat)
inputs.ki.time = 0;
inputs.ki.dwFlags = 0; // 0 means down, nonzero means up
inputs.ki.wVk = keyCode; // virtual-key code for the "a" key
inputs = inputs; // repeat
inputs.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;
inputs.type = INPUT_MOUSE;
inputs.mi.dx = 0; // relative X coordinate of mouse movement
inputs.mi.dy = 0; // relative Y coordinate of mouse movement
inputs.mi.dwFlags = MOUSEEVENTF_MOVE; // Mouse movement flag
inputs.mi.mouseData = 0; // additional flags and data (not used in this case)
inputs.mi.time = 0; // Event time (not used in this case)
inputs.mi.dwExtraInfo = 0; // Extra information (not used in this case)
inputs.type = INPUT_MOUSE;
inputs.mi.dx = 0; // Relative X coordinate of mouse movement (same as above)
inputs.mi.dy = 0; // Relative Y coordinate of mouse movement (same as above)
inputs.mi.dwFlags = button == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN; // Left or right button down flag
inputs.mi.mouseData = 0; // Additional flags and data (not used in this case)
inputs.mi.time = 0; // Event time (not used in this case)
inputs.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;
inputs.type = INPUT_KEYBOARD;
inputs.ki.wParam = 0; // repeat (0 means no repeat)
inputs.ki.time = 0;
inputs.ki.dwFlags = 0; // 0 means down, nonzero means up
inputs.ki.wVk = keyCode; // virtual-key code for the "a" key
inputs = inputs; // repeat
inputs.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:
**** Hidden Message *****
帮帮顶顶!! 没看完~~~~~~ 先顶,好同志 6666666 学习了,不错,讲的太有道理了 帮你顶下哈 谢谢楼主 支持一下 写的真的很不错 路过,学习下