这是我在stackoverflow上发布的第一个问题。我一直在研究SendInput
for C ++,以便将我的程序“键入”到另一个程序中。我决定先在终端输入几个下划线来“键入”它。我发现键入大写和小写字母以及句点都没有问题。但是在到达下划线之后,输入数字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;
}
这是我编写的不正确运行的代码:
#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;
}
这将输出Ninja. Hi123456
而不是Ninja. Hi123_456
。
其他值得注意的事情:
1)。对于被“键入”的句点(。),工作ID为0xBE
而不是0x3A
。
2)。这是使用Mingw在Windows 10上编译的。
我希望这已经足够彻底了,谢谢!!
答案 0 :(得分:2)
虚拟键码0x3A
不是句点字符。实际上,per Microsoft's documentation,0x3A
根本没有定义。对于句点字符,必须改为使用VK_OEM_PERIOD
:
VK_OEM_PERIOD
0xBE对于任何国家/地区,“。”键
话虽这么说,通常用SendInput()
调用cInputs=1
是一个逻辑错误。当您像示例代码那样做背对背发送多个输入事件时,当然总是有一个错误。根本SendInput()
存在的全部原因是要替换keybd_event()
(和mouse_event()
),后者一次只能发送一个输入事件。模拟多个事件时,您不希望在事件之间插入其他事件,反之亦然。 SendInput()
在其他输入机制中是原子的,但是当发送多个事件时,只有一次发送所有事件时才能保证原子性。
您应该将INPUT
放入数组中,并一次调用SendInput()
,并将cInputs
设置为要发送的INPUT
总数。
此外,在发送文本字符的键输入时,请使用VkKeyScan/Ex()
来获取正确的虚拟键代码和转换状态,尽管use the KEYEVENTF_UNICODE
flag容易得多,所以您可以发送实际的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;
}