Repository: microsoft/terminal
File: src/terminal/adapter/InteractDispatch.cpp (C++)

Code before:
    gci.GetActiveInputBuffer()->Write(inputEvents);
}

// Method Description:
// - Writes a key event to the host in a fashion that will enable the host to
//   process special keys such as Ctrl-C or Ctrl+Break. The host will then
//   decide what to do with it, including potentially sending an interrupt to a
//   client application.
// Arguments:
// - event: The key to send to the host.
void InteractDispatch::WriteCtrlKey(const INPUT_RECORD& event)
{
    HandleGenericKeyEvent(event, false);
}

// Call this method to write some plain text to the InputBuffer.
//
// Since the hosting terminal for ConPTY may not support win32-input-mode,
// it may send an "A" key as an "A", for which we need to generate up/down events.
// Because of this, we cannot simply call InputBuffer::WriteString directly.
void InteractDispatch::WriteString(const std::wstring_view string)
{
    if (!string.empty())
    {
        const auto codepage = _api.GetConsoleOutputCP();
        InputEventQueue keyEvents;

        for (const auto& wch : string)
        {
            CharToKeyEvents(wch, codepage, keyEvents);
        }

        WriteInput(keyEvents);
    }
}

void InteractDispatch::WriteStringRaw(std::wstring_view string)
{
    const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
    gci.GetActiveInputBuffer()->WriteString(string);
}

//Method Description:
// Window Manipulation - Performs a variety of actions relating to the window,
//      such as moving the window position, resizing the window, querying
//      window state, forcing the window to repaint, etc.
//  This is kept separate from the output version, as there may be
//      codes that are supported in one direction but not the other.
//Arguments:
// - function - An identifier of the WindowManipulation function to perform
// - parameter1 - The first optional parameter for the function

Review comment (bug):
I forgot to mention that I noticed we're using the output code page here to generate input events, which seemed wrong to me. Was that intentional?

That codepage is only used if the `UseNumpadEventsForClipboardInput` feature is enabled (which doesn't appear to be the case), so I don't think it's breaking anything at the moment, but it's still worth fixing if it's incorrect.

Code after:
    gci.GetActiveInputBuffer()->Write(inputEvents);
}

// Method Description:
// - Writes a key event to the host in a fashion that will enable the host to
//   process special keys such as Ctrl-C or Ctrl+Break. The host will then
//   decide what to do with it, including potentially sending an interrupt to a
//   client application.
// Arguments:
// - event: The key to send to the host.
void InteractDispatch::WriteCtrlKey(const INPUT_RECORD& event)
{
    HandleGenericKeyEvent(event, false);
}

// Call this method to write some plain text to the InputBuffer.
//
// Since the hosting terminal for ConPTY may not support win32-input-mode,
// it may send an "A" key as an "A", for which we need to generate up/down events.
// Because of this, we cannot simply call InputBuffer::WriteString directly.
void InteractDispatch::WriteString(const std::wstring_view string)
{
    if (!string.empty())
    {
        const auto codepage = _api.GetOutputCodePage();
        InputEventQueue keyEvents;

        for (const auto& wch : string)
        {
            CharToKeyEvents(wch, codepage, keyEvents);
        }

        WriteInput(keyEvents);
    }
}

void InteractDispatch::WriteStringRaw(std::wstring_view string)
{
    const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
    gci.GetActiveInputBuffer()->WriteString(string);
}

//Method Description:
// Window Manipulation - Performs a variety of actions relating to the window,
//      such as moving the window position, resizing the window, querying
//      window state, forcing the window to repaint, etc.
//  This is kept separate from the output version, as there may be
//      codes that are supported in one direction but not the other.
//Arguments:
// - function - An identifier of the WindowManipulation function to perform
// - parameter1 - The first optional parameter for the function