void ConsoleInput::appendKeyPress()

in src/agent/ConsoleInput.cc [660:746]


void ConsoleInput::appendKeyPress(std::vector<INPUT_RECORD> &records,
                                  const uint16_t virtualKey,
                                  const uint32_t winCodePointDn,
                                  const uint32_t winCodePointUp,
                                  const uint16_t winKeyState,
                                  const uint32_t vtCodePoint,
                                  const uint16_t vtKeyState)
{
    const bool ctrl = (winKeyState & LEFT_CTRL_PRESSED) != 0;
    const bool leftAlt = (winKeyState & LEFT_ALT_PRESSED) != 0;
    const bool rightAlt = (winKeyState & RIGHT_ALT_PRESSED) != 0;
    const bool shift = (winKeyState & SHIFT_PRESSED) != 0;
    const bool enhanced = (winKeyState & ENHANCED_KEY) != 0;
    bool hasDebugInput = false;

    if (isTracingEnabled()) {
        static bool debugInput = hasDebugFlag("input");
        if (debugInput) {
            hasDebugInput = true;
            InputMap::Key key = { virtualKey, winCodePointDn, winKeyState };
            trace("keypress: %s", key.toString().c_str());
        }
    }

    if (m_escapeInputEnabled &&
            (virtualKey == VK_UP ||
                virtualKey == VK_DOWN ||
                virtualKey == VK_LEFT ||
                virtualKey == VK_RIGHT ||
                virtualKey == VK_HOME ||
                virtualKey == VK_END) &&
            !ctrl && !leftAlt && !rightAlt && !shift) {
        flushInputRecords(records);
        if (hasDebugInput) {
            trace("sending keypress to console HWND");
        }
        sendKeyMessage(m_console.hwnd(), true, virtualKey);
        sendKeyMessage(m_console.hwnd(), false, virtualKey);
        return;
    }

    uint16_t stepKeyState = 0;
    if (ctrl) {
        stepKeyState |= LEFT_CTRL_PRESSED;
        appendInputRecord(records, TRUE, VK_CONTROL, 0, stepKeyState);
    }
    if (leftAlt) {
        stepKeyState |= LEFT_ALT_PRESSED;
        appendInputRecord(records, TRUE, VK_MENU, 0, stepKeyState);
    }
    if (rightAlt) {
        stepKeyState |= RIGHT_ALT_PRESSED;
        appendInputRecord(records, TRUE, VK_MENU, 0, stepKeyState | ENHANCED_KEY);
    }
    if (shift) {
        stepKeyState |= SHIFT_PRESSED;
        appendInputRecord(records, TRUE, VK_SHIFT, 0, stepKeyState);
    }
    if (enhanced) {
        stepKeyState |= ENHANCED_KEY;
    }
    if (m_escapeInputEnabled) {
        reencodeEscapedKeyPress(records, virtualKey, vtCodePoint, vtKeyState);
    } else {
        appendCPInputRecords(records, TRUE, virtualKey, winCodePointDn, stepKeyState);
    }
    appendCPInputRecords(records, FALSE, virtualKey, winCodePointUp, stepKeyState);
    if (enhanced) {
        stepKeyState &= ~ENHANCED_KEY;
    }
    if (shift) {
        stepKeyState &= ~SHIFT_PRESSED;
        appendInputRecord(records, FALSE, VK_SHIFT, 0, stepKeyState);
    }
    if (rightAlt) {
        stepKeyState &= ~RIGHT_ALT_PRESSED;
        appendInputRecord(records, FALSE, VK_MENU, 0, stepKeyState | ENHANCED_KEY);
    }
    if (leftAlt) {
        stepKeyState &= ~LEFT_ALT_PRESSED;
        appendInputRecord(records, FALSE, VK_MENU, 0, stepKeyState);
    }
    if (ctrl) {
        stepKeyState &= ~LEFT_CTRL_PRESSED;
        appendInputRecord(records, FALSE, VK_CONTROL, 0, stepKeyState);
    }
}