-1

I have a utf-8 formatted file that contains a character 'ů', when I read it in c++ using ifstream and cout it, the output is '┼»'. I can solve it by adding 'SetConsoleOutputCP( 65001 );', afterwards Im sending the char using 'SendInput();' to another window. But because the SetConsoleOutput() doesnt affect SendInput() the application recieves the corrupted character '┼»'. The question is how to SendInput() accented character?

        ifstream file(path);
        string str;
        while (getline (file, str)) 
        {
            cout << endl << str <<endl;
            for (char &c : str) {
                INPUT ip;
                ip.type = INPUT_KEYBOARD;
                ip.ki.wScan = 0;
                ip.ki.time = 0;
                ip.ki.dwExtraInfo = 0;
                ip.ki.dwFlags = KEYEVENTF_UNICODE;
                if (isupper(c)) {
                    ip.ki.wVk = VK_LSHIFT;
                    SendInput(1, &ip, sizeof(INPUT));
                } else {
                    ip.ki.dwFlags = KEYEVENTF_KEYUP;
                    ip.ki.wVk = VK_LSHIFT;
                    SendInput(1, &ip, sizeof(INPUT));
                    ip.ki.dwFlags = KEYEVENTF_UNICODE;
                } 
                ip.ki.wVk = VkKeyScanA(c);
                SendInput(1, &ip, sizeof(INPUT));
                Sleep(100);
            }
            INPUT enter;
            enter.type = INPUT_KEYBOARD;
            enter.ki.wScan = 0;
            enter.ki.time = 0;
            enter.ki.dwExtraInfo = 0;
            enter.ki.dwFlags = 0;
            enter.ki.wVk = VK_RETURN;
            SendInput(1, &enter, sizeof(INPUT));
            cout << "enter pressed";
        }
Botje
  • 26,269
  • 3
  • 31
  • 41
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/223354/discussion-on-question-by-johny-incognito-reading-utf-8-file-passing-contents-t). – Samuel Liew Oct 20 '20 at 12:15

2 Answers2

2
  1. UTF-8 and Windows are not good friends. Almost everything you are going to do with Windows requires UTF-16. You need to convert your UTF-8 string to UTF-16. On Windows, that would be "wstring", but you should use the right conversion routine. Alas the C++ standard cannot be bothered to give you a working one one right now, so Windows-specific MultiByteToWideChar seems like a good option.

  2.  ip.ki.dwFlags = KEYEVENTF_UNICODE;
     if (isupper(c)) {
                 ip.ki.wVk = VK_LSHIFT;
    

    It doesn't work this way.

    If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.

    Unicode characters cannot be combined with Shift or other modifiers. They are not key codes, they are characters. A and a are different characters, there is no need to send shift presses to differentiate between them. You also don't need VkKeyScanA, because you want to send Unicode characters, not scan codes.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

You need to convert the UTF-8 data to UTF-16 first, such as with MultiByteToWideChar() or equivalent, and then you can pass the individual UTF-16 codeunits to SendInput() using the KEYEVENTF_UNICODE flag. See my answer to Sending Two or more chars using SendInput for an example of sending UTF-16 strings with SendInput().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770