积分信息:
无忧币:114638
交易币:2147478427
贡献:999999
|
<
马上注册,结交更多好友,享用更多功能,让你轻松玩转无忧吧。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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:
|
无忧技术吧-免责声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关。一切关于该内容及资源商业行为与www.92wuyou.cn无关。
2、本站提供的一切资源内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
3、本站信息来自第三方用户,非本站自制,版权归原作者享有,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
4、注册会员通过任何手段和方法针对论坛进行破坏,我们有权对其行为作出处理。并保留进一步追究其责任的权利。
5、无忧技术吧(www.92wuyou.cn)所讨论的技术及相关工具仅限用于研究学习,皆在提高软件产品的安全性,严禁用于不良动机。任何个人、团体、组织不得将其用于非法目的,否则,一切后果自行承担。无忧技术吧不承担任何因为技术滥用所产生的连带责任。无忧技术吧内容源于网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除。如有侵权请邮件或QQ与我们联系处理。
6、如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵犯你版权的,请邮件与我们联系删除(邮箱:whctwlgzs@foxmail.com),本站将立即改正。
联系方式:
站长邮箱:whctwlgzs@foxmail.com
站长QQ:4040068
|