将字符串传递到 SendInput() (C++) [英] Passing a string into SendInput() (C++)

查看:17
本文介绍了将字符串传递到 SendInput() (C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于不便,我已将此项目转移到 C#,无需进一步评论

我在开发 C++ 花絮时遇到了一个问题,它根据用户输入发送击键.

I've encountered an issue during the development of a C++ tidbit, which sends keystrokes based on user input.

在尝试发送实际击键时,我遇到了错误.

Upon trying to send the actual keystrokes, I am greeted with an error.

<error-type> message

我在下面发布了一个代码片段.非常感谢任何帮助.

I have posted a code snippet down below. Any help is very much appreciated.

/*Code snippet from program that handles actual keypresses*/
string message;
getline(cin, message);

SendInput(message);

推荐答案

查看 SendInput

您必须设置INPUT 结构,然后传递INPUT 数组.

You have to setup the INPUT structure, then pass array of INPUT.

#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>

int main()
{
    std::wstring msg = L"ΨΦΩ, ελη, ABC, abc, 123";
    //or std::string msg = "ABCD - abcd - 1234";

    std::vector<INPUT> vec;
    for(auto ch : msg)
    {
        INPUT input = { 0 };
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = KEYEVENTF_UNICODE;
        input.ki.wScan = ch;
        vec.push_back(input);

        input.ki.dwFlags |= KEYEVENTF_KEYUP;
        vec.push_back(input);
    }

    //Find a notepad window or another window for send
    HWND hwnd = FindWindow("Notepad", 0);
    if (hwnd)
        SetForegroundWindow(hwnd);
    else
        std::cout << "no window!\n";

    SendInput(vec.size(), vec.data(), sizeof(INPUT));
    return 0;
}

请注意,ANSI 在 Windows 中已被弃用.此带有 std::string 的代码仅适用于 ASCII 字符.对于 UNICODE 兼容性,请使用 std::wstring

Note that ANSI is deprecated in Windows. This code with std::string works only for ASCII characters. For UNICODE compatibility use std::wstring

这篇关于将字符串传递到 SendInput() (C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆