Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KeyboardManager] Fix shift on numpad #35890

Merged
merged 14 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down
22 changes: 22 additions & 0 deletions src/modules/keyboardmanager/common/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/modules/keyboardmanager/common/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/modules/keyboardmanager/common/MappingConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void MappingConfiguration::ClearOSLevelShortcuts()
void MappingConfiguration::ClearSingleKeyRemaps()
{
singleKeyReMap.clear();
scanMap.clear();
}

// Function to clear the Keys remapping table.
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/modules/keyboardmanager/common/MappingConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class MappingConfiguration
// 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;

Expand Down
Loading