繁体   English   中英

模拟“。” 在 c ++ (windows.h) 中使用 keybd_event (或任何东西)

[英]simulating "!" with keybd_event (or anything) in c ++ (windows.h)

! Win32的虚拟键码中不存在。 有没有办法我可以使用keybd_event()或其他任何东西来模拟键盘输入导致! ?

这是我第一次在 Discord 赌场(私人)中制作垃圾邮件机器人。

由于keybd_event已被SendInput取代,我建议您改用它。

使用SendInput ,您可以发送许多INPUT结构。 您可以发送鼠标输入、键盘输入和硬件输入。 我将展示如何发送键盘输入。

可以使用扫描码或 Unicode 字符发送键盘输入。 我将使用 Unicode。 为您不知道的内容查找 unicode 字符通常很简单: https://www.google.com/search?q=unicode+exclamation+mark并且您会得到答案(U+0021 表示! ) .

我将从继承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";
}

如果一切按计划进行,它将发送26事件。 WM_KEYDOWN! WM_KEYUP ! ,对于AHello world中的所有字符也是如此。


注意:对于键盘上的键,您可以简化事件序列的构建:

    Inputs x{{
        key_down(u'!'),
        key_up(u'!'),
    }};

使用ASCII码! 这是 33

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM