gpt4 book ai didi

c++ - SendInput On C++ 不显示下划线

转载 作者:行者123 更新时间:2023-11-30 03:20:53 24 4
gpt4 key购买 nike

这是我在 stackoverflow 上发布的第一个问题。我一直在研究 C++ 的 SendInput ,以便让我的程序“输入”到另一个程序中。我决定首先在终端中“输入”几个带有下划线的单词。我发现它输入大写字母和小写字母以及句点没有问题。但是在到达下划线之后,输入下划线字母的数字 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 上编译的。

我希望这足够彻底,提前谢谢您!

最佳答案

虚拟按键代码0x3A不是句点字符。事实上, per Microsoft's documentation , 0x3A 根本就没有定义。对于句点字符,您必须使用 VK_OEM_PERIOD 代替:

VK_OEM_PERIOD
0xBE

For any country/region, the '.' key

话虽如此,使用 cInputs=1 调用 SendInput() 通常是一个逻辑错误。当您连续发送多个输入事件时,就像您的示例代码所做的那样,当然总是会出现错误。 SendInput() 存在的全部原因是为了替换 keybd_event() (和 mouse_event()),它只能发送一个输入事件一次。模拟多个事件时,您不希望在事件之间注入(inject)其他事件,反之亦然。 SendInput() 对于其他输入机制来说是原子性的,但是当发送多个事件时,只有在一次发送所有事件时才能保证原子性。

您应该将 INPUT 放入数组中,并调用 SendInput() 一次,并将 cInputs 设置为 的总数>输入您正在发送的内容。

此外,在发送文本字符的按键输入时,需要使用 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;
}

关于c++ - SendInput On C++ 不显示下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com