void ConsoleInput::appendUtf8Char()

in src/agent/ConsoleInput.cc [601:658]


void ConsoleInput::appendUtf8Char(std::vector<INPUT_RECORD> &records,
                                  const char *charBuffer,
                                  const int charLen,
                                  const bool terminalAltEscape)
{
    const uint32_t codePoint = decodeUtf8(charBuffer);
    if (codePoint == static_cast<uint32_t>(-1)) {
        static bool debugInput = isTracingEnabled() && hasDebugFlag("input");
        if (debugInput) {
            StringBuilder error(64);
            error << "Discarding invalid UTF-8 sequence:";
            for (int i = 0; i < charLen; ++i) {
                error << ' ';
                error << hexOfInt<true, uint8_t>(charBuffer[i]);
            }
            trace("%s", error.c_str());
        }
        return;
    }

    const short charScan = codePoint > 0xFFFF ? -1 : VkKeyScan(codePoint);
    uint16_t virtualKey = 0;
    uint16_t winKeyState = 0;
    uint32_t winCodePointDn = codePoint;
    uint32_t winCodePointUp = codePoint;
    uint16_t vtKeyState = 0;

    if (charScan != -1) {
        virtualKey = charScan & 0xFF;
        if (charScan & 0x100) {
            winKeyState |= SHIFT_PRESSED;
        }
        if (charScan & 0x200) {
            winKeyState |= LEFT_CTRL_PRESSED;
        }
        if (charScan & 0x400) {
            winKeyState |= RIGHT_ALT_PRESSED;
        }
        if (terminalAltEscape && (winKeyState & LEFT_CTRL_PRESSED)) {
            // If the terminal escapes a Ctrl-<Key> with Alt, then set the
            // codepoint to 0.  On the other hand, if a character requires
            // AltGr (like U+00B2 on a German layout), then VkKeyScan will
            // report both Ctrl and Alt pressed, and we should keep the
            // codepoint.  See https://github.com/rprichard/winpty/issues/109.
            winCodePointDn = 0;
            winCodePointUp = 0;
        }
    }
    if (terminalAltEscape) {
        winCodePointUp = 0;
        winKeyState |= LEFT_ALT_PRESSED;
        vtKeyState |= LEFT_ALT_PRESSED;
    }

    appendKeyPress(records, virtualKey,
                   winCodePointDn, winCodePointUp, winKeyState,
                   codePoint, vtKeyState);
}