Repository: microsoft/terminal
File: src/host/inputBuffer.cpp (C++)

Code before:
// - Handler for inserting key sequences into the buffer when the terminal emulation layer
//   has determined a key can be converted appropriately into a sequence of inputs
// Arguments:
// - inEvents - Series of input records to insert into the buffer
// Return Value:
// - <none>
void InputBuffer::_HandleTerminalInputCallback(const TerminalInput::StringType& text)
{
    try
    {
        if (text.empty())
        {
            return;
        }

        for (const auto& wch : text)
        {
            if (wch == UNICODE_NULL)
            {
                // Convert null byte back to input event with proper control state
                const auto zeroKey = OneCoreSafeVkKeyScanW(0);
                uint32_t ctrlState = 0;
                if (WI_IsFlagSet(zeroKey, 0x100))
                {
                    ctrlState |= SHIFT_PRESSED;
                }
                if (WI_IsFlagSet(zeroKey, 0x200))
                {
                    ctrlState |= CTRL_PRESSED;
                }
                if (WI_IsFlagSet(zeroKey, 0x400))
                {
                    ctrlState |= ALT_PRESSED;
                }
                _storage.push_back(SynthesizeKeyEvent(true, 1, LOBYTE(zeroKey), 0, wch, ctrlState));
                continue;
            }
            _storage.push_back(SynthesizeKeyEvent(true, 1, 0, 0, wch, 0));
        }

        if (!_vtInputShouldSuppress)
        {
            ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
            WakeUpReadersWaitingForData();
        }
    }
    catch (...)
    {
        LOG_HR(wil::ResultFromCaughtException());
    }
}

Review comment (suggestion):
Just a nit, but I recently discovered there's `WI_SetFlagIf` macro that makes this kind of code a little neater. You can compress these conditions into one line each with something like:

```
WI_SetFlagIf(ctrlState, SHIFT_PRESSED, WI_IsFlagSet(zeroKey, 0x100));
```

It only works with single flags though, so I think you'd have to use `LEFT_CTRL_PRESSED` and `LEFT_ALT_PRESSED` for the other two, but that should still work. It's not important though. Just an idea.

Code after:
// - Handler for inserting key sequences into the buffer when the terminal emulation layer
//   has determined a key can be converted appropriately into a sequence of inputs
// Arguments:
// - inEvents - Series of input records to insert into the buffer
// Return Value:
// - <none>
void InputBuffer::_HandleTerminalInputCallback(const TerminalInput::StringType& text)
{
    try
    {
        if (text.empty())
        {
            return;
        }

        for (const auto& wch : text)
        {
            if (wch == UNICODE_NULL)
            {
                // Convert null byte back to input event with proper control state
                const auto zeroKey = OneCoreSafeVkKeyScanW(0);
                uint32_t ctrlState = 0;
                WI_SetFlagIf(ctrlState, SHIFT_PRESSED, WI_IsFlagSet(zeroKey, 0x100));
                WI_SetFlagIf(ctrlState, LEFT_CTRL_PRESSED, WI_IsFlagSet(zeroKey, 0x200));
                WI_SetFlagIf(ctrlState, LEFT_ALT_PRESSED, WI_IsFlagSet(zeroKey, 0x400));
                _storage.push_back(SynthesizeKeyEvent(true, 1, LOBYTE(zeroKey), 0, wch, ctrlState));
                continue;
            }
            _storage.push_back(SynthesizeKeyEvent(true, 1, 0, 0, wch, 0));
        }

        if (!_vtInputShouldSuppress)
        {
            ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
            WakeUpReadersWaitingForData();
        }
    }
    catch (...)
    {
        LOG_HR(wil::ResultFromCaughtException());
    }
}

TerminalInput& InputBuffer::GetTerminalInput()
{
    return _termInput;
}