扫描二维码关注官方公众号

78 评论

0 收藏

分享

[其他源码] C++DMA键鼠操作源码-可以直接用到自己项目中

 

马上注册,结交更多好友,享用更多功能,让你轻松玩转无忧吧。

您需要 登录 才可以下载或查看,没有账号?立即注册

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头文件中,其原型如下:

  1. cpp
  2. LRESULT SendInput(
  3.     ULONG nInputs,
  4.     IN INPUT* pInputs,
  5.     INT cbSize
  6. );
复制代码


- `nInputs`:要发送的输入事件的数目。
- `pInputs`:指向一个包含所有输入事件的数组指针。
- `cbSize`:每个输入结构的大小。

### 2.2 INPUT结构体

INPUT结构体用于描述一个输入事件,可以是键盘事件或鼠标事件。其定义如下:

  1. cpp
  2. typedef struct tagINPUT {
  3.     union {
  4.         struct {
  5.             USHORT type;
  6.             USHORT wParamL;
  7.             ULONG time;
  8.             PARAM lParam;
  9.         } ki;
  10.         struct {
  11.             USHORT type;
  12.             USHORT wParamH;
  13.             ULONG time;
  14.             PARAM lParam;
  15.         } khi;
  16.         struct {
  17.             USHORT type;
  18.             ULONG wParamL;
  19.             ULONG time;
  20.             PARAM lParam;
  21.         } mi;
  22.         struct {
  23.             USHORT type;
  24.             ULONG wParamL;
  25.             ULONG time;
  26.             PARAM lParam;
  27.         } ma;
  28.         struct {
  29.             USHORT type;
  30.             ULONG wParamL;
  31.             ULONG time;
  32.             PARAM lParam;
  33.         } si;
  34.         BYTE data[8];
  35.     };
  36. } INPUT, *PINPUT;
复制代码


- `type`:指定输入的类型,例如KEYDOWN、MOUSEMOVE等。
- `wParamL`、`wParamH`、`lParam`:根据不同的类型,这些参数会有不同的意义。
- `time`:事件发生的时间。
- `data`:用于存储实际的数据,例如按键的虚拟键码。

## 3. 实现步骤

### 3.1 初始化环境

在使用SendInput函数之前,需要包含必要的头文件并链接相应的库:

  1. cpp
  2. #include
  3. #include
复制代码


编译时需要链接user32.lib库:

  1. sh
  2. g++ -o keymouse_simulator keymouse_simulator.cpp -luser32
复制代码


### 3.2 模拟键盘操作

以下是一个简单的示例代码,用于模拟按下和释放某个键:

  1. cpp
  2. void simulateKeyPress(int keyCode) {
  3.     INPUT inputs[2];
  4.     inputs[0].type = INPUT_KEYBOARD;
  5.     inputs[0].ki.wParam = 0; // repeat (0 means no repeat)
  6.     inputs[0].ki.time = 0;
  7.     inputs[0].ki.dwFlags = 0; // 0 means down, nonzero means up
  8.     inputs[0].ki.wVk = keyCode; // virtual-key code for the "a" key

  9.     inputs[1] = inputs[0]; // repeat
  10.     inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP; // up event flag

  11.     SendInput(2, inputs, sizeof(INPUT));
  12. }

  13. int main() {
  14.     simulateKeyPress(65); // Press 'A' key
  15.     std::this_thread::sleep_for(std::chrono::seconds(1)); // Delay for demonstration purposes
  16.     simulateKeyPress(65); // Release 'A' key
  17.     return 0;
  18. }
复制代码


### 3.3 模拟鼠标操作

类似地,我们可以模拟鼠标的移动和点击:

  1. cpp
  2. void moveMouseTo(int x, int y) {
  3.     POINT point;
  4.     point.x = x;
  5.     point.y = y;
  6.     SetCursorPos(point.x, point.y); // Set the mouse position to (x, y)
  7. }

  8. void clickMouse(int button) {
  9.     INPUT inputs[2];
  10.     inputs[0].type = INPUT_MOUSE;
  11.     inputs[0].mi.dx = 0; // relative X coordinate of mouse movement
  12.     inputs[0].mi.dy = 0; // relative Y coordinate of mouse movement
  13.     inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE; // Mouse movement flag
  14.     inputs[0].mi.mouseData = 0; // additional flags and data (not used in this case)
  15.     inputs[0].mi.time = 0; // Event time (not used in this case)
  16.     inputs[0].mi.dwExtraInfo = 0; // Extra information (not used in this case)

  17.     inputs[1].type = INPUT_MOUSE;
  18.     inputs[1].mi.dx = 0; // Relative X coordinate of mouse movement (same as above)
  19.     inputs[1].mi.dy = 0; // Relative Y coordinate of mouse movement (same as above)
  20.     inputs[1].mi.dwFlags = button == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN; // Left or right button down flag
  21.     inputs[1].mi.mouseData = 0; // Additional flags and data (not used in this case)
  22.     inputs[1].mi.time = 0; // Event time (not used in this case)
  23.     inputs[1].mi.dwExtraInfo = 0; // Extra information (not used in this case)

  24.     SendInput(2, inputs, sizeof(INPUT)); // Send both input events to simulate a mouse click
  25. }

  26. int main() {
  27.     moveMouseTo(100, 100); // Move mouse to (100, 100)
  28.     clickMouse(LEFT_BUTTON); // Click left mouse button at (100, 100)
  29.     std::this_thread::sleep_for(std::chrono::seconds(1)); // Delay for demonstration purposes
  30.     moveMouseTo(200, 200); // Move mouse to (200, 200)
  31.     clickMouse(RIGHT_BUTTON); // Click right mouse button at (200, 200)
  32.     return 0;
  33. }
复制代码


## 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
捷云鲸社区bbs.jieyunjing.com
回复

举报 使用道具

全部回复 (78)
查看全部
帮帮顶顶!!
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

没看完~~~~~~ 先顶,好同志
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

6666666
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

学习了,不错,讲的太有道理了
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

帮你顶下哈
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

谢谢楼主
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

支持一下
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

写的真的很不错
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

路过,学习下
捷云鲸社区bbs.jieyunjing.com

举报 回复 使用道具

admin 实名认证
管理员
主题 1380
回复 75
粉丝 1

灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老 沙发王 源码大师 在线王 终身成就 机器王 知识库 土豪 活跃会员 最佳新人 热心会员