[Shortcut Guide] fix Virtual-Key codes range (#6025)

This commit is contained in:
Enrico Giordani 2020-08-20 12:24:06 +02:00 committed by GitHub
parent 2390368d03
commit 9a36b005a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,18 +8,87 @@ bool winkey_held()
return (left & 0x8000) || (right & 0x8000); return (left & 0x8000) || (right & 0x8000);
} }
// Returns true if the VK code is in the range of valid keys.
// Some VK codes should not be checked because they would return
// false positives when checking if only the "Win" key is pressed.
constexpr bool should_check(int vk)
{
switch (vk)
{
case VK_CANCEL:
case VK_BACK:
case VK_TAB:
case VK_CLEAR:
case VK_ESCAPE:
case VK_APPS:
case VK_SLEEP:
case VK_NUMLOCK:
case VK_SCROLL:
case VK_OEM_102:
return true;
}
if (vk >= VK_SHIFT && vk <= VK_CAPITAL)
{
return true;
}
if (vk >= VK_SPACE && vk <= VK_HELP)
{
return true;
}
// Digits
if (vk >= 0x30 && vk <= 0x39)
{
return true;
}
// Letters
if (vk >= 0x41 && vk <= 0x5A)
{
return true;
}
if (vk >= VK_NUMPAD0 && vk <= VK_F24)
{
return true;
}
if (vk >= VK_LSHIFT && vk <= VK_LAUNCH_APP2)
{
return true;
}
if (vk >= VK_OEM_1 && vk <= VK_OEM_3)
{
return true;
}
if (vk >= VK_OEM_4 && vk <= VK_OEM_8)
{
return true;
}
if (vk >= VK_ATTN && vk <= VK_OEM_CLEAR)
{
return true;
}
return false;
}
bool only_winkey_key_held() bool only_winkey_key_held()
{ {
/* There are situations, when some of the keys are not registered correctly by for (int vk = VK_CANCEL; vk <= VK_OEM_CLEAR; vk++)
GetKeyboardState. The M key can get stuck as "pressed" after Win+M, and
Shift etc. keys are not always reported as expected.
*/
for (int vk = 0; vk <= VK_OEM_CLEAR; ++vk)
{ {
if (vk == VK_LWIN || vk == VK_RWIN) if (should_check(vk))
continue; {
if (GetAsyncKeyState(vk) & 0x8000) if (GetAsyncKeyState(vk) & 0x8000)
return false; {
return false;
}
}
} }
return true; return true;
} }