鱼C论坛

 找回密码
 立即注册
查看: 22|回复: 8

请问为何改掉乱码就不能运行?

[复制链接]
发表于 昨天 15:06 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 磊之茶 于 2026-8-19 15:21 编辑

我用AI写代码,复制时不知道怎么地成乱码了但是可以运行
[code]/*
* ============================================================================
* TypingSimulator - 鏂囨湰鏂囦欢妯℃嫙閿洏杈撳叆宸ュ叿
* ============================================================================
* 閫傜敤锛歐indows 11 + 鍗庣绗旇鏈紙瀹為檯鏀寔鎵

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:07 | 显示全部楼层
发帖子时出错了,以下是乱码版
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:08 | 显示全部楼层
本帖最后由 磊之茶 于 2026-8-19 15:20 编辑

[code]/*
* ============================================================================
* TypingSimulator - 鏂囨湰鏂囦欢妯℃嫙閿洏杈撳叆宸ュ叿
* ============================================================================
* 鍔熻兘锛氳鍙?TXT 鏂囦欢鍐呭锛岄

typing_simulator.zip

3.33 KB, 下载次数: 1

乱码来的

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:11 | 显示全部楼层
本帖最后由 磊之茶 于 2026-8-19 15:25 编辑

乱码版发不出来,只能像上面那样只有一小段,其余发布后异常消失
以下是修改版
  1. /*
  2. * ============================================================================
  3. * TypingSimulator - 文本文件模拟键盘输入工具
  4. * ============================================================================
  5. * 功能:读取 TXT 文件内容,通过 SendInput API 逐字模拟键盘输入
  6. *       可绕过网页的"禁止粘贴"限制(因为本质是模拟硬件键盘事件)
  7. *
  8. * 适用:Windows 11 + 华为笔记本(实际支持所有 Windows 系统)
  9. * 编译:gcc typing_simulator.c -o typing_simulator.exe -luser32 -mwindows
  10. *       或 cl typing_simulator.c user32.lib
  11. *
  12. * 使用流程:
  13. *   1. 运行程序,输入 TXT 文件路径
  14. *   2. 程序倒计时 5 秒(给你时间切换到目标网页输入框)
  15. *   3. 自动开始逐字"打字"输入文件内容
  16. *
  17. * 注意:
  18. *   - TXT 文件请保存为 UTF-8 编码(含 BOM 也可)
  19. *   - 输入前请确保目标输入框已获得焦点(光标在闪烁)
  20. *   - 输入过程中不要动键盘鼠标,以免干扰
  21. * ============================================================================
  22. */

  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>

  27. /*
  28. * 发送单个 Unicode 字符(核心函数)
  29. * 原理:使用 SendInput + KEYEVENTF_UNICODE
  30. * 这种方式直接向系统输入队列注入 Unicode 字符,
  31. * 完全绕过了输入法和粘贴检测机制
  32. */
  33. void SendUnicodeChar(wchar_t ch)
  34. {
  35.     INPUT input = {0};

  36.     /* 按下事件 */
  37.     input.type = INPUT_KEYBOARD;
  38.     input.ki.wVk = 0;                /* 使用 Unicode 时 wVk 必须为 0 */
  39.     input.ki.wScan = ch;             /* 直接填充 Unicode 字符 */
  40.     input.ki.dwFlags = KEYEVENTF_UNICODE;
  41.     input.ki.time = 0;
  42.     input.ki.dwExtraInfo = 0;

  43.     /* 释放事件 */
  44.     input.type = INPUT_KEYBOARD;
  45.     input.ki.wVk = 0;
  46.     input.ki.wScan = ch;
  47.     input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
  48.     input.ki.time = 0;
  49.     input.ki.dwExtraInfo = 0;

  50.     SendInput(2, input, sizeof(INPUT));
  51. }

  52. /*
  53. * 发送一个虚拟键(用于发送回车、Tab 等特殊键)
  54. */
  55. void SendVirtualKey(WORD vk)
  56. {
  57.     INPUT input = {0};

  58.     input.type = INPUT_KEYBOARD;
  59.     input.ki.wVk = vk;
  60.     input.ki.dwFlags = 0;

  61.     input.type = INPUT_KEYBOARD;
  62.     input.ki.wVk = vk;
  63.     input.ki.dwFlags = KEYEVENTF_KEYUP;

  64.     SendInput(2, input, sizeof(INPUT));
  65. }

  66. /*
  67. * 模拟"打字"一个字符串
  68. * text     : 要输入的宽字符字符串
  69. * charDelay: 每个字符之间的延迟(毫秒),推荐 30~100
  70. */
  71. void SimulateTyping(const wchar_t* text, int charDelay)
  72. {
  73.     for (int i = 0; text[i] != L'\0'; i++)
  74.     {
  75.         wchar_t ch = text[i];

  76.         if (ch == L'\n')
  77.         {
  78.             /* 换行 -> 发送回车键 */
  79.             SendVirtualKey(VK_RETURN);
  80.         }
  81.         else if (ch == L'\t')
  82.         {
  83.             /* Tab -> 发送 Tab 键 */
  84.             SendVirtualKey(VK_TAB);
  85.         }
  86.         else if (ch == L'\r')
  87.         {
  88.             /* 忽略 \r(Windows 换行符的一部分) */
  89.             continue;
  90.         }
  91.         else
  92.         {
  93.             /* 普通字符(含中文)-> Unicode 方式发送 */
  94.             SendUnicodeChar(ch);
  95.         }

  96.         /* 字符间延迟,模拟真人打字节奏 */
  97.         if (charDelay > 0)
  98.             Sleep(charDelay);
  99.     }
  100. }

  101. /*
  102. * 读取 UTF-8 编码的文本文件,返回宽字符字符串
  103. * 自动处理 BOM(字节顺序标记)
  104. */
  105. wchar_t* ReadTxtFileToWString(const char* filePath)
  106. {
  107.     FILE* fp = fopen(filePath, "rb");  /* 二进制模式打开 */
  108.     if (fp == NULL)
  109.     {
  110.         printf("[错误] 无法打开文件: %s\n", filePath);
  111.         return NULL;
  112.     }

  113.     /* 获取文件大小 */
  114.     fseek(fp, 0, SEEK_END);
  115.     long fileSize = ftell(fp);
  116.     fseek(fp, 0, SEEK_SET);

  117.     if (fileSize <= 0)
  118.     {
  119.         printf("[错误] 文件为空或读取失败\n");
  120.         fclose(fp);
  121.         return NULL;
  122.     }

  123.     /* 读取全部字节 */
  124.     char* utf8Bytes = (char*)malloc(fileSize + 1);
  125.     if (utf8Bytes == NULL)
  126.     {
  127.         printf("[错误] 内存分配失败\n");
  128.         fclose(fp);
  129.         return NULL;
  130.     }
  131.     fread(utf8Bytes, 1, fileSize, fp);
  132.     utf8Bytes[fileSize] = '\0';
  133.     fclose(fp);

  134.     /* 跳过 UTF-8 BOM(EF BB BF) */
  135.     char* ptr = utf8Bytes;
  136.     if (fileSize >= 3 &&
  137.         (unsigned char)utf8Bytes == 0xEF &&
  138.         (unsigned char)utf8Bytes == 0xBB &&
  139.         (unsigned char)utf8Bytes == 0xBF)
  140.     {
  141.         ptr += 3;
  142.     }

  143.     /* 计算转换后的宽字符长度 */
  144.     int wideLen = MultiByteToWideChar(CP_UTF8, 0, ptr, -1, NULL, 0);
  145.     if (wideLen == 0)
  146.     {
  147.         printf("[错误] UTF-8 转宽字符失败(错误码: %lu)\n", GetLastError());
  148.         free(utf8Bytes);
  149.         return NULL;
  150.     }

  151.     /* 分配并转换 */
  152.     wchar_t* wideStr = (wchar_t*)malloc(wideLen * sizeof(wchar_t));
  153.     if (wideStr == NULL)
  154.     {
  155.         printf("[错误] 宽字符内存分配失败\n");
  156.         free(utf8Bytes);
  157.         return NULL;
  158.     }
  159.     MultiByteToWideChar(CP_UTF8, 0, ptr, -1, wideStr, wideLen);

  160.     free(utf8Bytes);
  161.     return wideStr;
  162. }

  163. /*
  164. * 倒计时提示
  165. */
  166. void Countdown(int seconds)
  167. {
  168.     for (int i = seconds; i > 0; i--)
  169.     {
  170.         printf("\r倒计时 %2d 秒 —— 请切换到目标网页输入框...", i);
  171.         fflush(stdout);
  172.         Sleep(1000);
  173.     }
  174.     printf("\r倒计时结束!开始输入!                          \n");
  175. }

  176. /*
  177. * 清屏后打印程序标题
  178. */
  179. void PrintBanner(void)
  180. {
  181.     printf("\n");
  182.     printf("========================================\n");
  183.     printf("=      TypingSimulator - 模拟键盘输入工具 v1.0          =\n");
  184.     printf("=                                                       =\n");
  185.     printf("= 原理: SendInput + KEYEVENTF_UNICODE                   =\n");
  186.     printf("= 效果: 模拟真实键盘逐字输入,绕过网页禁止粘贴限制       =\n");
  187.     printf("= 适用: Windows 11 (华为/联想/戴尔等所有品牌)           =\n");
  188.     printf("========================================================\n\n");
  189. }

  190. int main(int argc, char* argv[])
  191. {
  192.     PrintBanner();

  193.     /* ---------- 1. 获取文件路径 ---------- */
  194.     char filePath[MAX_PATH];

  195.     if (argc > 1)
  196.     {
  197.         strcpy(filePath, argv);
  198.     }
  199.     else
  200.     {
  201.         printf("请输入 TXT 文件路径(支持拖拽文件到窗口)\n> ");
  202.         fgets(filePath, MAX_PATH, stdin);
  203.         /* 去掉换行符 */
  204.         filePath[strcspn(filePath, "\r\n")] = '\0';
  205.     }

  206.     if (strlen(filePath) == 0)
  207.     {
  208.         printf("[错误] 文件路径为空\n");
  209.         system("pause");
  210.         return 1;
  211.     }

  212.     /* ---------- 2. 读取文件内容 ---------- */
  213.     printf("\n[信息] 正在读取文件: %s\n", filePath);

  214.     wchar_t* text = ReadTxtFileToWString(filePath);
  215.     if (text == NULL)
  216.     {
  217.         system("pause");
  218.         return 1;
  219.     }

  220.     /* 统计字符数 */
  221.     int charCount = wcslen(text);
  222.     printf("[信息] 文件读取成功,共 %d 个字符\n", charCount);

  223.     /* 预览前 100 个字符 */
  224.     wprintf(L"[预览] %.*s%s\n",
  225.             (charCount > 50 ? 50 : charCount), text,
  226.             (charCount > 50 ? L"..." : L""));

  227.     /* ---------- 3. 获取输入速度 ---------- */
  228.     int delayMs = 50;  /* 默认 50ms */
  229.     printf("\n请输入每个字符之间的延迟(毫秒),默认 50,回车确认\n> ");
  230.     char delayInput;
  231.     fgets(delayInput, 32, stdin);
  232.     delayInput[strcspn(delayInput, "\r\n")] = '\0';
  233.     if (strlen(delayInput) > 0)
  234.     {
  235.         int parsed = atoi(delayInput);
  236.         if (parsed >= 0 && parsed <= 5000)
  237.             delayMs = parsed;
  238.     }
  239.     printf("[信息] 延迟设置为 %d ms/字符\n", delayMs);

  240.     /* ---------- 4. 倒计时 ---------- */
  241.     printf("\n[提示] 你有 5 秒钟时间切换到目标网页,并点击输入框让光标闪烁\n");
  242.     Countdown(5);

  243.     /* ---------- 5. 开始模拟输入 ---------- */
  244.     printf("[运行] 正在模拟键盘输入...\n");

  245.     /*
  246.      * 先检查一下当前前台窗口,提醒用户
  247.      */
  248.     HWND fg = GetForegroundWindow();
  249.     if (fg == NULL)
  250.         printf("[警告] 未检测到前台窗口,请确认已切换到目标网页\n");

  251.     /*
  252.      * 核心调用:逐字模拟输入
  253.      * 使用 Unicode 方式,中文/英文/标点全部兼容
  254.      */
  255.     SimulateTyping(text, delayMs);

  256.     printf("[完成] 输入完毕!共输入 %d 个字符\n", charCount);

  257.     /* 清理 */
  258.     free(text);

  259.     printf("\n按任意键退出...");
  260.     system("pause >nul");
  261.     return 0;
  262. }
复制代码
修改版不能运行但是乱码版正常运行,修改版仅仅修改了注释和中文部分
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:12 | 显示全部楼层
本帖最后由 磊之茶 于 2026-8-19 15:21 编辑

我的求助是改一下,使其能够正常运行,还有为什么乱码可以运行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:28 | 显示全部楼层
对了,其实我自己知道乱码版看起来就一堆问题,疑似有严重bug比如%s 不能用于 wchar_t*
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 昨天 15:30 | 显示全部楼层
本帖最后由 磊之茶 于 2026-8-19 18:31 编辑

报错是:254        13        C:\Users\67428\Desktop\超级无敌大输入啊啊.c        [Error] converting to execution character set: Illegal byte sequence
还有55        5        C:\Users\67428\Desktop\驶入输入.c        [Error] incompatible type for argument 2 of 'SendInput'
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 21 小时前 | 显示全部楼层
乱码是因为 Windows 的编码问题吧?是使用 VSCode 吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2 小时前 | 显示全部楼层
本帖最后由 磊之茶 于 2026-8-20 21:09 编辑
小甲鱼的二师兄 发表于 2026-8-20 01:29
乱码是因为 Windows 的编码问题吧?是使用 VSCode 吗?


应该是编码问题,我用DevC++编译器,而且经过排查,wprintf(L"[棰勮] %.*s%s\n",
            (charCount > 50 ? 50 : charCount), text,
            (charCount > 50 ? L"..." : L""));是唯一一句不能改掉乱码的,而且这句不能运行,我怀疑是DEVC++的环境问题导致我看似只动了一个字,实则破坏了中文的编码,但是我就算不动也不能实现这个预览要复制的文本的前50个文字的功能,请问我推测得对吗以及能否修复预览要复制的文本的前50个文字的功能
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-8-20 23:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表