[KeyboardManager]Fix mapping shift to numpad (#35890)

* Keyboard Manger fix numpad as shift

Fixed shift not being released if a numpad key as shift.

* Added comments

* Fix typo

* Fix the numpad unlocked key not working if the locked version is overridden by shift

* Fix spelling check.

* Revert the VK_CLEAR change.

---------

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
This commit is contained in:
Ionuț Manța 2024-12-04 19:41:12 +02:00 коммит произвёл GitHub
Родитель 89be43e15d
Коммит e7f17495bb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 90 добавлений и 0 удалений

Просмотреть файл

@ -32,6 +32,58 @@ namespace
{
return data->lParam->dwExtraInfo & CommonSharedConstants::KEYBOARDMANAGER_INJECTED_FLAG;
}
void UpdateNumpadWithShift(LowlevelKeyboardEvent* data, State& state)
{
//Function for fixing numpad when used as shift https://github.com/microsoft/PowerToys/issues/22346
//VK_CLEAR is not encoded in IsNumpadOriginated
if (Helpers::IsNumpadOriginated(data->lParam->vkCode) || data->lParam->vkCode == VK_CLEAR)
{
// Decode it. If it is VK_CLEAR it will do nothing
DWORD decodedKey = Helpers::ClearKeyNumpadOrigin(data->lParam->vkCode);
//check if we already have a stored scanID
auto scanKey = MapVirtualKey(decodedKey, MAPVK_VK_TO_VSC);
auto it = state.scanMap.find(scanKey);
if (it != state.scanMap.end())
{
auto keyIt = state.GetSingleKeyRemap(it->second);
if (keyIt)
{
//if key is stored as shift replace it with the numpad key
auto keyValue = keyIt.value();
if (keyValue->second.index() == 0)
{
auto key = std::get<DWORD>(keyValue->second);
if (key == VK_LSHIFT || key == VK_RSHIFT || key == VK_SHIFT)
{
if (state.numpadKeyPressed[it->second])
{
//replace it with original numpad
data->lParam->vkCode = it->second;
}
}
}
if (keyValue->second.index() == 1)
{
auto key = std::get<Shortcut>(keyValue->second);
if (key.shiftKey != ModifierKey::Disabled)
{
if (state.numpadKeyPressed[it->second])
{
//replace it with original numpad
data->lParam->vkCode = it->second;
}
}
}
}
}
}
if (Helpers::IsNumpadKeyThatIsAffectedByShift(data->lParam->vkCode))
{
// store if the Numpad key was pressed or not. If numpad numbers were pressed but then we get the same key KEY UP but with Numpad unlocked we will replace it.
state.numpadKeyPressed[data->lParam->vkCode] = (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN);
}
}
}
namespace KeyboardEventHandlers
@ -42,6 +94,7 @@ namespace KeyboardEventHandlers
// Check if the key event was generated by KeyboardManager to avoid remapping events generated by us.
if (!GeneratedByKBM(data))
{
UpdateNumpadWithShift(data, state);
const auto remapping = state.GetSingleKeyRemap(data->lParam->vkCode);
if (remapping)
{

Просмотреть файл

@ -47,6 +47,28 @@ namespace Helpers
{
return !!(key & GetNumpadOriginEncodingBit());
}
// Check if it's one of the numpad keys that the shift key can affect, so we can introduce a workaround when they are mapped to shift.
bool IsNumpadKeyThatIsAffectedByShift(const DWORD vkCode)
{
switch (vkCode)
{
case VK_NUMPAD0:
case VK_NUMPAD1:
case VK_NUMPAD2:
case VK_NUMPAD3:
case VK_NUMPAD4:
case VK_NUMPAD5:
case VK_NUMPAD6:
case VK_NUMPAD7:
case VK_NUMPAD8:
case VK_NUMPAD9:
case VK_DECIMAL:
return true;
}
return false;
}
DWORD GetNumpadOriginEncodingBit()
{
// Intentionally do not mimic KF_EXTENDED to avoid confusion, because it's not the same thing

Просмотреть файл

@ -19,6 +19,7 @@ namespace Helpers
DWORD EncodeKeyNumpadOrigin(const DWORD key, const bool extended);
DWORD ClearKeyNumpadOrigin(const DWORD key);
bool IsNumpadOriginated(const DWORD key);
bool IsNumpadKeyThatIsAffectedByShift(const DWORD vkCode);
DWORD GetNumpadOriginEncodingBit();
// Function to check if the key is a modifier key

Просмотреть файл

@ -21,6 +21,7 @@ void MappingConfiguration::ClearOSLevelShortcuts()
void MappingConfiguration::ClearSingleKeyRemaps()
{
singleKeyReMap.clear();
scanMap.clear();
}
// Function to clear the Keys remapping table.
@ -64,6 +65,15 @@ bool MappingConfiguration::AddSingleKeyRemap(const DWORD& originalKey, const Key
}
singleKeyReMap[originalKey] = newRemapKey;
if (Helpers::IsNumpadKeyThatIsAffectedByShift(originalKey))
{
// Numpad keys might get altered by shift being pressed. We need to save their scancode instead to try and detect that they were unpressed when they are mapped to shift.
auto scanCode = MapVirtualKey(originalKey, MAPVK_VK_TO_VSC);
if (scanCode != 0)
{
scanMap[MapVirtualKey(originalKey, MAPVK_VK_TO_VSC)] = originalKey;
}
}
return true;
}

Просмотреть файл

@ -51,6 +51,10 @@ public:
// Stores single key remappings
SingleKeyRemapTable singleKeyReMap;
std::unordered_map<DWORD, DWORD> scanMap;
std::unordered_map<DWORD, bool> numpadKeyPressed;
// Stores single key to text remappings
SingleKeyToTextRemapTable singleKeyToTextReMap;