[KBM] Distinguish numpad keys (#28097)

This commit is contained in:
Andrey Nekrasov
2023-08-23 18:56:40 +02:00
committed by GitHub
parent c2bb2a8c3a
commit efee03eb99
9 changed files with 145 additions and 136 deletions

View File

@@ -9,6 +9,50 @@
namespace Helpers
{
DWORD EncodeKeyNumpadOrigin(const DWORD key, const bool extended)
{
bool numpad_originated = false;
switch (key)
{
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_INSERT:
case VK_DELETE:
case VK_PRIOR:
case VK_NEXT:
case VK_HOME:
case VK_END:
numpad_originated = !extended;
break;
case VK_RETURN:
case VK_DIVIDE:
numpad_originated = extended;
break;
}
if (numpad_originated)
return key | GetNumpadOriginEncodingBit();
else
return key;
}
DWORD ClearKeyNumpadOrigin(const DWORD key)
{
return (key & ~GetNumpadOriginEncodingBit());
}
bool IsNumpadOriginated(const DWORD key)
{
return !!(key & GetNumpadOriginEncodingBit());
}
DWORD GetNumpadOriginEncodingBit()
{
// Intentionally do not mimic KF_EXTENDED to avoid confusion, because it's not the same thing
// See EncodeKeyNumpadOrigin.
return 1ull << 31;
}
// Function to check if the key is a modifier key
bool IsModifierKey(DWORD key)
{
@@ -18,7 +62,8 @@ namespace Helpers
// Function to get the combined key for modifier keys
DWORD GetCombinedKey(DWORD key)
{
switch (key) {
switch (key)
{
case VK_LWIN:
case VK_RWIN:
return CommonSharedConstants::VK_WIN_BOTH;

View File

@@ -14,6 +14,12 @@ namespace Helpers
Shift,
Action
};
// Functions to encode that a key is originated from numpad
DWORD EncodeKeyNumpadOrigin(const DWORD key, const bool extended);
DWORD ClearKeyNumpadOrigin(const DWORD key);
bool IsNumpadOriginated(const DWORD key);
DWORD GetNumpadOriginEncodingBit();
// Function to check if the key is a modifier key
bool IsModifierKey(DWORD key);

View File

@@ -194,7 +194,7 @@ DWORD Shortcut::GetShiftKey() const
}
// Function to check if the input key matches the win key expected in the shortcut
bool Shortcut::CheckWinKey(const DWORD& input) const
bool Shortcut::CheckWinKey(const DWORD input) const
{
if (winKey == ModifierKey::Disabled)
{
@@ -216,7 +216,7 @@ bool Shortcut::CheckWinKey(const DWORD& input) const
}
// Function to check if the input key matches the ctrl key expected in the shortcut
bool Shortcut::CheckCtrlKey(const DWORD& input) const
bool Shortcut::CheckCtrlKey(const DWORD input) const
{
if (ctrlKey == ModifierKey::Disabled)
{
@@ -238,7 +238,7 @@ bool Shortcut::CheckCtrlKey(const DWORD& input) const
}
// Function to check if the input key matches the alt key expected in the shortcut
bool Shortcut::CheckAltKey(const DWORD& input) const
bool Shortcut::CheckAltKey(const DWORD input) const
{
if (altKey == ModifierKey::Disabled)
{
@@ -260,7 +260,7 @@ bool Shortcut::CheckAltKey(const DWORD& input) const
}
// Function to check if the input key matches the shift key expected in the shortcut
bool Shortcut::CheckShiftKey(const DWORD& input) const
bool Shortcut::CheckShiftKey(const DWORD input) const
{
if (shiftKey == ModifierKey::Disabled)
{
@@ -282,7 +282,7 @@ bool Shortcut::CheckShiftKey(const DWORD& input) const
}
// Function to set a key in the shortcut based on the passed key code argument. Returns false if it is already set to the same value. This can be used to avoid UI refreshing
bool Shortcut::SetKey(const DWORD& input)
bool Shortcut::SetKey(const DWORD input)
{
// Since there isn't a key for a common Win key we use the key code defined by us
if (input == CommonSharedConstants::VK_WIN_BOTH)
@@ -394,7 +394,7 @@ bool Shortcut::SetKey(const DWORD& input)
}
// Function to reset the state of a shortcut key based on the passed key code argument. Since there is no VK_WIN code, use the second argument for setting common win key.
void Shortcut::ResetKey(const DWORD& input)
void Shortcut::ResetKey(const DWORD input)
{
// Since there isn't a key for a common Win key this is handled with a separate argument.
if (input == CommonSharedConstants::VK_WIN_BOTH || input == VK_LWIN || input == VK_RWIN)
@@ -415,7 +415,7 @@ void Shortcut::ResetKey(const DWORD& input)
}
else
{
actionKey = NULL;
actionKey = {};
}
}

View File

@@ -1,5 +1,8 @@
#pragma once
#include "ModifierKey.h"
#include <compare>
#include <tuple>
#include <variant>
namespace KeyboardManagerInput
@@ -14,91 +17,34 @@ private:
// Function to split a wstring based on a delimiter and return a vector of split strings
std::vector<std::wstring> splitwstring(const std::wstring& input, wchar_t delimiter);
public:
ModifierKey winKey;
ModifierKey ctrlKey;
ModifierKey altKey;
ModifierKey shiftKey;
DWORD actionKey;
// By default create an empty shortcut
Shortcut() :
winKey(ModifierKey::Disabled), ctrlKey(ModifierKey::Disabled), altKey(ModifierKey::Disabled), shiftKey(ModifierKey::Disabled), actionKey(NULL)
inline auto comparator() const
{
return std::make_tuple(winKey, ctrlKey, altKey, shiftKey, actionKey);
}
public:
ModifierKey winKey = ModifierKey::Disabled;
ModifierKey ctrlKey = ModifierKey::Disabled;
ModifierKey altKey = ModifierKey::Disabled;
ModifierKey shiftKey = ModifierKey::Disabled;
DWORD actionKey = {};
Shortcut() = default;
// Constructor to initialize Shortcut from it's virtual key code string representation.
Shortcut(const std::wstring& shortcutVK);
// Constructor to initialize shortcut from a list of keys
Shortcut(const std::vector<int32_t>& keys);
// == operator
inline bool operator==(const Shortcut& sc) const
inline friend auto operator<=>(const Shortcut& lhs, const Shortcut& rhs) noexcept
{
return (winKey == sc.winKey && ctrlKey == sc.ctrlKey && altKey == sc.altKey && shiftKey == sc.shiftKey && actionKey == sc.actionKey);
return lhs.comparator() <=> rhs.comparator();
}
// Less than operator must be defined to use with std::map.
inline bool operator<(const Shortcut& sc) const
inline friend bool operator==(const Shortcut& lhs, const Shortcut& rhs) noexcept
{
// Compare win key first
if (winKey < sc.winKey)
{
return true;
}
else if (winKey > sc.winKey)
{
return false;
}
else
{
// If win key is equal, then compare ctrl key
if (ctrlKey < sc.ctrlKey)
{
return true;
}
else if (ctrlKey > sc.ctrlKey)
{
return false;
}
else
{
// If ctrl key is equal, then compare alt key
if (altKey < sc.altKey)
{
return true;
}
else if (altKey > sc.altKey)
{
return false;
}
else
{
// If alt key is equal, then compare shift key
if (shiftKey < sc.shiftKey)
{
return true;
}
else if (shiftKey > sc.shiftKey)
{
return false;
}
else
{
// If shift key is equal, then compare action key
if (actionKey < sc.actionKey)
{
return true;
}
else
{
return false;
}
}
}
}
}
return lhs.comparator() == rhs.comparator();
}
// Function to return the number of keys in the shortcut
@@ -126,22 +72,22 @@ public:
DWORD GetShiftKey() const;
// Function to check if the input key matches the win key expected in the shortcut
bool CheckWinKey(const DWORD& input) const;
bool CheckWinKey(const DWORD input) const;
// Function to check if the input key matches the ctrl key expected in the shortcut
bool CheckCtrlKey(const DWORD& input) const;
bool CheckCtrlKey(const DWORD input) const;
// Function to check if the input key matches the alt key expected in the shortcut
bool CheckAltKey(const DWORD& input) const;
bool CheckAltKey(const DWORD input) const;
// Function to check if the input key matches the shift key expected in the shortcut
bool CheckShiftKey(const DWORD& input) const;
bool CheckShiftKey(const DWORD input) const;
// Function to set a key in the shortcut based on the passed key code argument. Returns false if it is already set to the same value. This can be used to avoid UI refreshing
bool SetKey(const DWORD& input);
bool SetKey(const DWORD input);
// Function to reset the state of a shortcut key based on the passed key code argument
void ResetKey(const DWORD& input);
void ResetKey(const DWORD input);
// Function to return the string representation of the shortcut in virtual key codes appended in a string by ";" separator.
winrt::hstring ToHstringVK() const;