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

Code before:
                                      const bool keyDown)
    {
        // Update the selection, if it's present
        // GH#6423 - don't dismiss selection if the key that was pressed was a
        // modifier key. We'll wait for a real keystroke to dismiss the
        // GH #7395 - don't dismiss selection when taking PrintScreen
        // selection.
        // GH#8522, GH#3758 - Only modify the selection on key _down_. If we
        // modify on key up, then there's chance that we'll immediately dismiss
        // a selection created by an action bound to a keydown.
        if (HasSelection() &&
            !KeyEvent::IsModifierKey(vkey) &&
            vkey != VK_SNAPSHOT &&
            keyDown)
        {
            if (_terminal->IsInMarkMode())
            {
                if (modifiers.IsCtrlPressed() && vkey == 'A')
                {
                    auto lock = _terminal->LockForWriting();
                    _terminal->SelectAll();
                    _updateSelection();
                    return true;
                }
                else if (_settings->DetectURLs() && vkey == VK_TAB)
                {
                    auto lock = _terminal->LockForWriting();
                    _terminal->SelectHyperlink(!modifiers.IsShiftPressed());
                    _updateSelection();
                    return true;
                }
                else if (_terminal->IsTargetingUrl() && vkey == VK_RETURN)
                {
                    auto lock = _terminal->LockForReading();
                    const auto uri = _terminal->GetHyperlinkAtPosition(_terminal->GetSelectionAnchor());
                    _OpenHyperlinkHandlers(*this, winrt::make<OpenHyperlinkEventArgs>(winrt::hstring{ uri }));
                    return true;
                }
            }

            // try to update the selection
            if (const auto updateSlnParams{ _terminal->ConvertKeyEventToUpdateSelectionParams(modifiers, vkey) })
            {
                auto lock = _terminal->LockForWriting();
                _terminal->UpdateSelection(updateSlnParams->first, updateSlnParams->second, modifiers);
                _updateSelection();
                return true;
            }

            // GH#8791 - don't dismiss selection if Windows key was also pressed as a key-combination.
            if (!modifiers.IsWinPressed())

Review comment (performance):
If you check the `vkey` first the compiler will skip the slower COM call (boolean short circuit evaluation).

Code after:
                                      const bool keyDown)
    {
        // Update the selection, if it's present
        // GH#6423 - don't dismiss selection if the key that was pressed was a
        // modifier key. We'll wait for a real keystroke to dismiss the
        // GH #7395 - don't dismiss selection when taking PrintScreen
        // selection.
        // GH#8522, GH#3758 - Only modify the selection on key _down_. If we
        // modify on key up, then there's chance that we'll immediately dismiss
        // a selection created by an action bound to a keydown.
        if (HasSelection() &&
            !KeyEvent::IsModifierKey(vkey) &&
            vkey != VK_SNAPSHOT &&
            keyDown)
        {
            const auto isInMarkMode = _terminal->SelectionMode() == ::Terminal::SelectionInteractionMode::Mark;
            if (isInMarkMode)
            {
                if (modifiers.IsCtrlPressed() && vkey == 'A')
                {
                    // Ctrl + A --> Select all
                    auto lock = _terminal->LockForWriting();
                    _terminal->SelectAll();
                    _updateSelectionUI();
                    return true;
                }
                else if (vkey == VK_TAB && _settings->DetectURLs())
                {
                    // [Shift +] Tab --> next/previous hyperlink
                    auto lock = _terminal->LockForWriting();
                    const auto direction = modifiers.IsShiftPressed() ? ::Terminal::SearchDirection::Backward : ::Terminal::SearchDirection::Forward;
                    _terminal->SelectHyperlink(direction);
                    _updateSelectionUI();
                    return true;
                }
                else if (vkey == VK_RETURN && modifiers.IsCtrlPressed() && _terminal->IsTargetingUrl())
                {
                    // Ctrl + Enter --> Open URL
                    auto lock = _terminal->LockForReading();
                    const auto uri = _terminal->GetHyperlinkAtBufferPosition(_terminal->GetSelectionAnchor());
                    _OpenHyperlinkHandlers(*this, winrt::make<OpenHyperlinkEventArgs>(winrt::hstring{ uri }));
                    return true;
                }
                else if (vkey == VK_RETURN)
                {
                    // [Shift +] Enter --> copy text
                    // Don't lock here! CopySelectionToClipboard already locks for you!
                    CopySelectionToClipboard(modifiers.IsShiftPressed(), nullptr);
                    _terminal->ClearSelection();
                    _updateSelectionUI();
                    return true;