Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/WindowEmperor.cpp (C++)

Code before:
                    w->OnDirectKeyEvent(vkey, scanCode, keyDown);
                    continue;
                }
            }
        }

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    _finalizeSessionPersistence();

    if (_notificationIconShown)
    {
        Shell_NotifyIconW(NIM_DELETE, &_notificationIcon);
    }

    // There's a mysterious crash in XAML on Windows 10 if you just let _app get destroyed (GH#15410).
    // We also need to ensure that all UI threads exit before WindowEmperor leaves the scope on the main thread (MSFT:46744208).
    // Both problems can be solved and the shutdown accelerated by using TerminateProcess.
    // std::exit(), etc., cannot be used here, because those use ExitProcess for unpackaged applications.
    TerminateProcess(GetCurrentProcess(), gsl::narrow_cast<UINT>(0));
    __assume(false);
}

void WindowEmperor::_dispatchCommandline(winrt::TerminalApp::CommandlineArgs args)
{
    const auto exitCode = args.ExitCode();

    if (const auto msg = args.ExitMessage(); !msg.empty())
    {
        _showMessageBox(msg, exitCode != 0);
        return;
    }

    if (exitCode != 0)
    {
        return;
    }

    const auto parsedTarget = args.TargetWindow();
    WindowingMode windowingBehavior = WindowingMode::UseNew;
    uint64_t windowId = 0;
    winrt::hstring windowName;

    // Figure out the windowing behavior the caller wants
    // and get the sanitized window ID (if any) and window name (if any).
    if (parsedTarget.empty())
    {
        windowingBehavior = _app.Logic().Settings().GlobalSettings().WindowingBehavior();
    }

Review comment (suggestion):
targeted

```suggestion
    // Just in case someone has targeted a specific HWND,
```


Code after:

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    _finalizeSessionPersistence();

    if (_notificationIconShown)
    {
        Shell_NotifyIconW(NIM_DELETE, &_notificationIcon);
    }

    // There's a mysterious crash in XAML on Windows 10 if you just let _app get destroyed (GH#15410).
    // We also need to ensure that all UI threads exit before WindowEmperor leaves the scope on the main thread (MSFT:46744208).
    // Both problems can be solved and the shutdown accelerated by using TerminateProcess.
    // std::exit(), etc., cannot be used here, because those use ExitProcess for unpackaged applications.
    TerminateProcess(GetCurrentProcess(), gsl::narrow_cast<UINT>(0));
    __assume(false);
}

void WindowEmperor::_dispatchSpecialKey(MSG& msg) const
{
    const auto hwnd = msg.hwnd;
    AppHost* window = nullptr;

    // Just in case someone has targed a specific HWND,
    // we'll try to dispatch it to the corresponding class.
    // Usually this will not find anything because under WinUI the hidden CoreInput
    // window is responsible for all input handling (for whatever reason).
    for (const auto& h : _windows)
    {
        const auto w = h->GetWindow();
        if (w && w->GetHandle() == hwnd)
        {
            window = h.get();
            break;
        }
    }

    if (!window)
    {
        window = _mostRecentWindow();
        if (!window)
        {
            return;
        }
    }

    const auto vkey = gsl::narrow_cast<uint32_t>(msg.wParam);
    const auto scanCode = gsl::narrow_cast<uint8_t>(msg.lParam >> 16);
    const bool keyDown = msg.message & 1;