Enable ctrl+shift to run terminal elevated from context menu (#15137)

This pull request adds the requirement for the shift key to be pressed
in addition to the control key.

References #14810
Implemented in #14873

This is follow up work from my last pull request that was merged that
only required the control key to be pressed to launch the terminal as
admin from the shell context menu. After some discussion it was decided
that the shift key should be required as well as that is the norm on
Windows.

## Validation Steps Performed

Tested all combinations of shift+ctrl and verified that the terminal
only requests elevation when a shift and control key are pressed
together. The shell launches regularly if not.
This commit is contained in:
James Pack
2023-04-17 10:12:45 -04:00
committed by GitHub
parent 52171d2dab
commit e106c095a5
2 changed files with 10 additions and 10 deletions

View File

@@ -27,7 +27,7 @@ HRESULT OpenTerminalHere::Invoke(IShellItemArray* psiItemArray,
IBindCtx* /*pBindContext*/)
try
{
const auto runElevated = IsControlPressed();
const auto runElevated = IsControlAndShiftPressed();
wil::com_ptr_nothrow<IShellItem> psi;
RETURN_IF_FAILED(GetBestLocationFromSelectionOrSite(psiItemArray, psi.put()));
@@ -204,14 +204,14 @@ HRESULT OpenTerminalHere::GetBestLocationFromSelectionOrSite(IShellItemArray* ps
return S_OK;
}
// This method checks if any of the ctrl keys are pressed during activation of the shell extension
bool OpenTerminalHere::IsControlPressed()
// Check is both ctrl and shift keys are pressed during activation of the shell extension
bool OpenTerminalHere::IsControlAndShiftPressed()
{
const auto ControlPressed = 1U;
short control = 0;
short shift = 0;
control = GetAsyncKeyState(VK_CONTROL);
shift = GetAsyncKeyState(VK_SHIFT);
const auto control = GetKeyState(VK_CONTROL);
const auto leftControl = GetKeyState(VK_LCONTROL);
const auto rightControl = GetKeyState(VK_RCONTROL);
return WI_IsFlagSet(control, ControlPressed) || WI_IsFlagSet(leftControl, ControlPressed) || WI_IsFlagSet(rightControl, ControlPressed);
// GetAsyncKeyState returns a value with the most significant bit set to 1 if the key is pressed. This is the sign bit.
return control < 0 && shift < 0;
}

View File

@@ -58,7 +58,7 @@ struct
private:
HRESULT GetLocationFromSite(IShellItem** location) const noexcept;
HRESULT GetBestLocationFromSelectionOrSite(IShellItemArray* psiArray, IShellItem** location) const noexcept;
bool IsControlPressed();
bool IsControlAndShiftPressed();
wil::com_ptr_nothrow<IUnknown> site_;
};