C++模拟键盘命令的代码
如果相关的内容已经存在,请原谅我创建一个文件,但我搜索了太久,我的头几乎融化了。
那么,说到重点了。有人可以向我描述一下我需要编写的代码,以便让 C++ 应用程序发送与我同时手动按下“ctrl”键和“a”键时相同的信号吗?
我发现了很多其他链接:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=VS.90).aspx
显然,但是,功能不起作用。我尝试使用:
using namespace System::Windows::Forms;
但是,它什么也没做。
显然我需要包含代码。这是我的代码。我了解基础知识,是的,我可以阅读。所以,我已经包括了:
#define _WIN32_WINNT 0x0501
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */
using namespace std;
HWND GameWindow = FindWindow(0, "C:\\Users\\...\\src\\output1.txt");
/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
return;
}
int main() {
system("start C:\\Users\\...\\src\\output1.txt");
SetForegroundWindow(GameWindow);
GenerateKey ('A', FALSE);
GenerateKey (VK_CAPITAL, TRUE);
GenerateKey(0x0D, FALSE); /* enter key */
getch();
return 0;
}
现在我的问题是,程序实际上并没有将此 A 写入该 .txt 文件。
注意:如果从 CMD 运行,它会起作用,在实际的 CMD 上打印“A”。
pardon me for making a file if something relevant already exists but I have searched for too long and my head has almost melted.
So, to the point. Can someone describe to me the code that I need to write to have a c++ application send the same signal as when I manually press the keys 'ctrl' and 'a' at the same time?
I found, among many others, this link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=VS.90).aspx
Apparently, though, function does not work. I tried to use:
using namespace System::Windows::Forms;
But, it does nothing.
Apparently I need to include code. This is my code. I know the basics and yes I can read. So, I have included that:
#define _WIN32_WINNT 0x0501
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */
using namespace std;
HWND GameWindow = FindWindow(0, "C:\\Users\\...\\src\\output1.txt");
/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
return;
}
int main() {
system("start C:\\Users\\...\\src\\output1.txt");
SetForegroundWindow(GameWindow);
GenerateKey ('A', FALSE);
GenerateKey (VK_CAPITAL, TRUE);
GenerateKey(0x0D, FALSE); /* enter key */
getch();
return 0;
}
Now my problem is that, the program is not actually writing this A onto that .txt file.
NOTE: It works if ran from CMD, printing the 'A' on the actual CMD.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Windows API 中的
SendInput
。从 codeguru 论坛 粘贴的示例副本:
用法:
另外,发送键盘输入到另一个进程,您将需要
AttachThreadInput
。Have a look at the
SendInput
from the Windows API.Example copy pasted from codeguru forums:
Usage:
Also, to send keyboard input to another process you will need
AttachThreadInput
.我会查看 AutoHotKey 源。
编辑:添加了 github 链接
I'd look at the AutoHotKey source.
Edit: added github link