[英]simulating "!" with keybd_event (or anything) in c ++ (windows.h)
!
does not exist in the virtual key codes of Win32. Win32的虚拟键码中不存在。 Is there a way I could use keybd_event()
, or anything else, to simulate a keyboard entry leading to !
有没有办法我可以使用keybd_event()
或其他任何东西来模拟键盘输入导致!
? ?
It's my first time making a bot to spam in a Discord casino (private).这是我第一次在 Discord 赌场(私人)中制作垃圾邮件机器人。
Since keybd_event
has been superseded by SendInput
, I suggest that you use that instead.由于keybd_event
已被SendInput
取代,我建议您改用它。
With SendInput
you send a number of INPUT
structures.使用SendInput
,您可以发送许多INPUT
结构。 You can send mouse input, keyboard input and hardware input.您可以发送鼠标输入、键盘输入和硬件输入。 I'll show how to send keyboard input.我将展示如何发送键盘输入。
Keyboard input can be sent using scan codes - or Unicode characters.可以使用扫描码或 Unicode 字符发送键盘输入。 I'll use Unicode.我将使用 Unicode。 Finding the unicode character for something you don't know is usually as easy as: https://www.google.com/search?q=unicode+exclamation+mark and you'll get the answer (U+0021 for !
).为您不知道的内容查找 unicode 字符通常很简单: https://www.google.com/search?q=unicode+exclamation+mark并且您会得到答案(U+0021 表示!
) .
I'll start by inheriting the INPUT
structure to make it simpler to instantiate it.我将从继承INPUT
结构开始,以使其更易于实例化。
#include <Windows.h>
#include <iostream>
#include <vector>
struct mINPUT : INPUT {
mINPUT() : INPUT{} {} // make sure it's clean if default constructed.
// this constructor prepares the structure for different kinds of input:
mINPUT(DWORD type) : INPUT{type} {
switch (type) {
case INPUT_MOUSE:
// use mi.
break;
case INPUT_KEYBOARD:
// use ki.
ki.dwFlags = KEYEVENTF_UNICODE; // we'll use unicode
break;
case INPUT_HARDWARE:
// use hi.
break;
}
}
};
// two helper functions to create `mINPUT` structures from unicode values:
mINPUT key_down(char16_t unicode_char) {
mINPUT rv{INPUT_KEYBOARD};
rv.ki.wScan = unicode_char;
return rv;
}
mINPUT key_up(char16_t unicode_char) {
mINPUT rv{INPUT_KEYBOARD};
rv.ki.dwFlags |= KEYEVENTF_KEYUP;
rv.ki.wScan = unicode_char;
return rv;
}
// A helper structure to prepare a sequence of events and functions to
// iteract with it
struct Inputs {
UINT cInputs() const { return static_cast<UINT>(inputs.size()); }
LPINPUT pInputs() { return inputs.data(); }
int cbSize() const { return static_cast<int>(sizeof(INPUT)); }
// A helper function to add both a key down and a key up event:
void add_key_down_up(char16_t unicode_char) {
inputs.push_back(key_down(unicode_char));
inputs.push_back(key_up(unicode_char));
}
// A helper function to add down+up events for a string:
void add_string(const char16_t* str) {
for (; *str; ++str) {
add_key_down_up(*str);
}
}
UINT Send() { // Send the stored events
return SendInput(
cInputs(),
pInputs(),
cbSize()
);
}
std::vector<mINPUT> inputs;
};
int main() {
std::cout << "Switch to Notepad or some other app taking input" << std::endl;
Sleep(5000); // in 5 seconds, you should see the input
// Put some events in an `Inputs` container at construction:
Inputs x{{
key_down(0x0021),
key_up(0x0021),
}};
// Use the helper function to add key down + key up for `A`:
x.add_key_down_up(u'A');
// Add events for a full string
x.add_string(u"Hello world");
// Send the events:
UINT rv = x.Send();
std::cout << "Sent " << rv << " events\n";
}
If everything goes according to plan, it will send 26
events.如果一切按计划进行,它将发送26
事件。 WM_KEYDOWN
for !
WM_KEYDOWN
为!
and WM_KEYUP
for !
和WM_KEYUP
!
, the same for A
and all the characters in Hello world
. ,对于A
和Hello world
中的所有字符也是如此。
Note: For keys you have on your keyboard you can simplify the building of the event sequence:注意:对于键盘上的键,您可以简化事件序列的构建:
Inputs x{{
key_down(u'!'),
key_up(u'!'),
}};
use the ascii code of !使用ASCII码! which is 33这是 33
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.