Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControl.cpp (C++)

Code before:
                buf[buf_len] = L'\0';
                _core.SendInput(std::wstring_view{ &buf[0], buf_len });
            }

            s = {};
            return true;
        }
        // As a continuation of the above, this handles the key-down case.
        if (modifiers.IsAltPressed())
        {
            // The OS code seems to reset the composition if shift is pressed, but I couldn't
            // figure out how exactly it worked. We'll simply ignore any such inputs.
            static constexpr DWORD permittedModifiers =
                RIGHT_ALT_PRESSED |
                LEFT_ALT_PRESSED |
                NUMLOCK_ON |
                SCROLLLOCK_ON |
                CAPSLOCK_ON;

            if ((modifiers.Value() & ~permittedModifiers) == 0)
            {
                auto& s = _altNumpadState;

                if (vkey == VK_ADD)
                {
                    if (keyDown)
                    {
                        // Alt '+' <number> is used to input Unicode code points.
                        // Every time you press + it resets the entire state
                        // in the original OS implementation as well.
                        s.encoding = AltNumpadEncoding::Unicode;
                        s.accumulator = 0;
                        s.active = true;
                    }
                }
                else if (vkey == VK_NUMPAD0 && s.encoding == AltNumpadEncoding::OEM && s.accumulator == 0)
                {
                    if (keyDown)
                    {
                        // Alt '0' <number> is used to input ANSI code points.
                        // Otherwise, they're OEM codepoints.
                        s.encoding = AltNumpadEncoding::ANSI;
                        s.active = true;
                    }
                }
                else
                {
                    if (keyDown)
                    {
                        // Otherwise, append the pressed key to the accumulator.
                        const uint32_t base = s.encoding == AltNumpadEncoding::Unicode ? 16 : 10;

Review comment (suggestion):
I had to move keyDown into these places so we could stash the keyUp event in case composition was active.

This makes `ALTdown KP+down KP+up ALTup` work.

Code after:
                buf[buf_len] = L'\0';
                _core.SendInput(std::wstring_view{ &buf[0], buf_len });
            }

            s = {};
            return handled;
        }
        // As a continuation of the above, this handles the key-down case.
        if (modifiers.IsAltPressed())
        {
            // The OS code seems to reset the composition if shift is pressed, but I couldn't
            // figure out how exactly it worked. We'll simply ignore any such inputs.
            static constexpr DWORD permittedModifiers =
                RIGHT_ALT_PRESSED |
                LEFT_ALT_PRESSED |
                NUMLOCK_ON |
                SCROLLLOCK_ON |
                CAPSLOCK_ON;

            if ((modifiers.Value() & ~permittedModifiers) == 0)
            {
                auto& s = _altNumpadState;

                if (keyDown)
                {
                    if (vkey == VK_ADD)
                    {
                        // Alt '+' <number> is used to input Unicode code points.
                        // Every time you press + it resets the entire state
                        // in the original OS implementation as well.
                        s.encoding = AltNumpadEncoding::Unicode;
                        s.accumulator = 0;
                        s.active = true;
                    }
                    else if (vkey == VK_NUMPAD0 && s.encoding == AltNumpadEncoding::OEM && s.accumulator == 0)
                    {
                        // Alt '0' <number> is used to input ANSI code points.
                        // Otherwise, they're OEM codepoints.
                        s.encoding = AltNumpadEncoding::ANSI;
                        s.active = true;
                    }
                    else
                    {
                        // Otherwise, append the pressed key to the accumulator.
                        const uint32_t base = s.encoding == AltNumpadEncoding::Unicode ? 16 : 10;
                        uint32_t add = 0xffffff;

                        if (vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9)
                        {
                            add = vkey - VK_NUMPAD0;
                        }