Gav*_*ard 2 c++ winapi sendinput
这是我在stackoverflow上发布的第一个问题。我一直在寻找SendInputC++,以便将我的程序“输入”到另一个程序中。我决定首先在终端中“输入”几个带有下划线的单词。我发现输入大写和小写字母以及句点没有问题。但是到了下划线后,输入数字 id 95作为下划线字母,下划线没有显示,完全就像那个字母没有被按下一样。这是我从 cplusplus.com 获得的代码,我基于它,它功能齐全:
#include <iostream>
#include <windows.h>
using namespace std;
/* HWND = "Window Handle" */
HWND GameWindow = FindWindow(0, "Command Prompt");
/* 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() {
/*
SetForegroundWindow will give the window focus for the
keyboard/mouse! In other words, you don't have to have
the game opened upfront in order to emulate key/mouse
presses, it's very helpful if it's a game that runs
in fullscreen mode, like StarCraft: Brood War does
*/
SetForegroundWindow(GameWindow);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('I', FALSE);
GenerateKey(' ', FALSE);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('A', FALSE);
GenerateKey('M', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('C', FALSE);
GenerateKey('O', FALSE);
GenerateKey('O', FALSE);
GenerateKey('L', FALSE);
GenerateKey('E', FALSE);
GenerateKey('R', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('T', FALSE);
GenerateKey('H', FALSE);
GenerateKey('A', FALSE);
GenerateKey('N', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('Y', FALSE);
GenerateKey('O', FALSE);
GenerateKey('U', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('W', FALSE);
GenerateKey('I', FALSE);
GenerateKey('L', FALSE);
GenerateKey('L', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('E', FALSE);
GenerateKey('V', FALSE);
GenerateKey('E', FALSE);
GenerateKey('R', FALSE);
GenerateKey(' ', FALSE);
GenerateKey('B', FALSE);
GenerateKey('E', FALSE);
GenerateKey('n', FALSE);
GenerateKey(' ', FALSE);
GenerateKey(0x3A, FALSE); /* period key */
GenerateKey(0x0D, FALSE); /* enter key */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我编写的运行不正确的代码:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
/* HWND = "Window Handle" */
HWND GameWindow = FindWindow(0, "Command Prompt");
/* 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() {
/*
SetForegroundWindow will give the window focus for the
keyboard/mouse! In other words, you don't have to have
the game opened upfront in order to emulate key/mouse
presses, it's very helpful if it's a game that runs
in fullscreen mode, like StarCraft: Brood War does
*/
SetForegroundWindow(GameWindow);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('N', FALSE);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('I', FALSE);
GenerateKey('N', FALSE);
GenerateKey('J', FALSE);
GenerateKey('A', FALSE);
GenerateKey(0xBE, FALSE); // GenerateKey(0x3A, FALSE); did not work
GenerateKey(' ', FALSE);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('H', FALSE);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('I', FALSE);
GenerateKey('1', FALSE);
GenerateKey('2', FALSE);
GenerateKey('3', FALSE);
GenerateKey(95 , FALSE); // GenerateKey('_', FALSE); did not work either
GenerateKey('4', FALSE);
GenerateKey('5', FALSE);
GenerateKey('6', FALSE);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这输出Ninja. Hi123456而不是Ninja. Hi123_456.
其他值得注意的事情:
1)。对于被“输入”的句点 ('.'),工作 ID0xBE不是0x3A.
2)。这是使用 Mingw 在 Windows 10 上编译的。
我希望这足够彻底,在此先感谢您!
虚拟键码0x3A不是句点字符。事实上,根据微软的文档,0x3A甚至根本没有定义。对于句点字符,您必须VK_OEM_PERIOD改用:
VK_OEM_PERIOD
0xBE对于任何国家/地区,“.” 钥匙
话虽这么说,称SendInput()与cInputs=1是通常一个逻辑错误。当您背靠背发送多个输入事件时,当然总是一个错误,就像您的示例代码所做的那样。SendInput()存在的全部原因是替换keybd_event()(和mouse_event()),它一次只能发送一个输入事件。在模拟多个事件时,您不希望在您的事件之间注入其他事件,反之亦然。 SendInput()与其他输入机制是原子的,但是当发送多个事件时,只有在一次发送所有事件时才能保证原子性。
您应该将您的INPUTs 放入一个数组中并调用SendInput()ONCE,并cInputs设置为INPUT您发送的s的总数。
此外,当发送文本字符的键输入时,使用需要使用VkKeyScan/Ex()来获取正确的虚拟键代码和转换状态,尽管使用KEYEVENTF_UNICODE标志更容易,因此您可以发送实际的 Unicode 字符而不是虚拟键代码。
尝试更像这样的事情:
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
/* HWND = "Window Handle" */
HWND GameWindow = FindWindow(NULL, TEXT("Command Prompt"));
void GenerateKeyDown(std::vector<INPUT> &inputQueue, int vk, bool bExtended = false)
{
INPUT in = {};
in.type = INPUT_KEYBOARD;
in.ki.wVk = vk;
if (bExtended) in.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
inputQueue.push_back(in);
}
void GenerateKeyUp(std::vector<INPUT> &inputQueue, int vk, bool bExtended = false)
{
INPUT in = {};
in.type = INPUT_KEYBOARD;
in.ki.wVk = vk;
if (bExtended) in.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
in.ki.dwFlags |= KEYEVENTF_KEYUP;
inputQueue.push_back(in);
}
void GenerateKey(std::vector<INPUT> &inputQueue, int vk, bool bExtended = false)
{
INPUT in[2] = {};
in[0].type = INPUT_KEYBOARD;
in[0].ki.wVk = vk;
if (bExtended) in[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
in[1] = in[0];
in[1].ki.dwFlags |= KEYEVENTF_KEYUP;
inputQueue.insert(inputQueue.end(), in, in+1);
}
void GenerateString(std::vector<INPUT> &inputQueue, const std::wstring &str)
{
int len = str.length();
if (len == 0) return;
inputQueue.reserve(inputQueue.size()+(len*2));
INPUT in = {};
in.type = INPUT_KEYBOARD;
in.ki.dwFlags = KEYEVENTF_UNICODE;
int i = 0;
while (i < len)
{
WORD ch = (WORD) str[i++];
if ((ch < 0xD800) || (ch > 0xDFFF))
{
in.ki.wScan = ch;
in.ki.dwFlags &= ~KEYEVENTF_KEYUP;
inputQueue.push_back(in);
in.ki.dwFlags |= KEYEVENTF_KEYUP;
inputQueue.push_back(in);
}
else
{
WORD ch2 = (WORD) str[i++];
in.ki.wScan = ch;
in.ki.dwFlags &= ~KEYEVENTF_KEYUP;
inputQueue.push_back(in);
in.ki.wScan = ch2;
inputQueue.push_back(in);
in.ki.wScan = ch;
in.ki.dwFlags |= KEYEVENTF_KEYUP;
inputQueue.push_back(in);
in.ki.wScan = ch2;
inputQueue.push_back(in);
}
}
}
int main()
{
/*
SetForegroundWindow will give the window focus for the
keyboard/mouse! In other words, you don't have to have
the game opened upfront in order to emulate key/mouse
presses, it's very helpful if it's a game that runs
in fullscreen mode, like StarCraft: Brood War does
*/
SetForegroundWindow(GameWindow);
std::vector<INPUT> inputQueue;
/*
GenerateString(inputQueue, L"I Am cooler than you will ever ben .");
GenerateKey(inputQueue, VK_RETURN);
*/
GenerateString(inputQueue, L"NInja. HI123_456");
/* alternatively:
GenerateString(inputQueue, L"NInja");
GenerateKey(inputQueue, VK_OEM_PERIOD);
GenerateString(inputQueue, L" HI123");
// see why using KEYEVENTF_UNICODE is easier?
SHORT ret = VkKeyScanW(L'_');
BYTE vk = LOBYTE(ret);
BYTE shift = HIBYTE(ret);
if (vk != -1)
{
SHORT state = GetKeyState(VK_SHIFT);
bool bIsDown = (state & 0x800);
if (shift & 1)
{
if (!bIsDown)
GenerateKeyDown(inputQueue, VK_SHIFT);
}
else
{
if (bIsDown)
GenerateKeyUp(inputQueue, VK_SHIFT);
}
GenerateKey(inputQueue, vk);
if (shift & 1)
{
if (!bIsDown)
GenerateKeyUp(inputQueue, VK_SHIFT);
}
else
{
if (bIsDown)
GenerateKeyDown(inputQueue, VK_SHIFT);
}
}
GenerateString(inputQueue, L"456");
*/
SendInput(inputQueue.size(), &inputQueue[0], sizeof(INPUT));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
298 次 |
| 最近记录: |