mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
[KBM] decoupling editor and engine (#11133)
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace KeyboardManagerHelper
|
||||
{
|
||||
// Type to store codes for different errors
|
||||
enum class ErrorType
|
||||
{
|
||||
NoError,
|
||||
SameKeyPreviouslyMapped,
|
||||
MapToSameKey,
|
||||
ConflictingModifierKey,
|
||||
SameShortcutPreviouslyMapped,
|
||||
MapToSameShortcut,
|
||||
ConflictingModifierShortcut,
|
||||
WinL,
|
||||
CtrlAltDel,
|
||||
RemapUnsuccessful,
|
||||
SaveFailed,
|
||||
ShortcutStartWithModifier,
|
||||
ShortcutCannotHaveRepeatedModifier,
|
||||
ShortcutAtleast2Keys,
|
||||
ShortcutOneActionKey,
|
||||
ShortcutNotMoreThanOneActionKey,
|
||||
ShortcutMaxShortcutSizeOneActionKey,
|
||||
ShortcutDisableAsActionKey
|
||||
};
|
||||
}
|
||||
@@ -5,27 +5,10 @@
|
||||
#include <common/interop/shared_constants.h>
|
||||
#include <common/utils/process_path.h>
|
||||
|
||||
#include "ErrorTypes.h"
|
||||
#include "KeyboardManagerConstants.h"
|
||||
|
||||
using namespace winrt::Windows::Foundation;
|
||||
|
||||
namespace KeyboardManagerHelper
|
||||
namespace Helpers
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
std::wstringstream ss(input);
|
||||
std::wstring item;
|
||||
std::vector<std::wstring> splittedStrings;
|
||||
while (std::getline(ss, item, delimiter))
|
||||
{
|
||||
splittedStrings.push_back(item);
|
||||
}
|
||||
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
// Function to check if the key is a modifier key
|
||||
bool IsModifierKey(DWORD key)
|
||||
{
|
||||
@@ -85,33 +68,6 @@ namespace KeyboardManagerHelper
|
||||
}
|
||||
}
|
||||
|
||||
// Function to check if two keys are equal or cover the same set of keys. Return value depends on type of overlap
|
||||
ErrorType DoKeysOverlap(DWORD first, DWORD second)
|
||||
{
|
||||
// If the keys are same
|
||||
if (first == second)
|
||||
{
|
||||
return ErrorType::SameKeyPreviouslyMapped;
|
||||
}
|
||||
else if ((GetKeyType(first) == GetKeyType(second)) && GetKeyType(first) != KeyType::Action)
|
||||
{
|
||||
// If the keys are of the same modifier type and overlapping, i.e. one is L/R and other is common
|
||||
if (((first == VK_LWIN && second == VK_RWIN) || (first == VK_RWIN && second == VK_LWIN)) || ((first == VK_LCONTROL && second == VK_RCONTROL) || (first == VK_RCONTROL && second == VK_LCONTROL)) || ((first == VK_LMENU && second == VK_RMENU) || (first == VK_RMENU && second == VK_LMENU)) || ((first == VK_LSHIFT && second == VK_RSHIFT) || (first == VK_RSHIFT && second == VK_LSHIFT)))
|
||||
{
|
||||
return ErrorType::NoError;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ErrorType::ConflictingModifierKey;
|
||||
}
|
||||
}
|
||||
// If no overlap
|
||||
else
|
||||
{
|
||||
return ErrorType::NoError;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to set the value of a key event based on the arguments
|
||||
void SetKeyEvent(LPINPUT keyEventArray, int index, DWORD inputType, WORD keyCode, DWORD flags, ULONG_PTR extraInfo)
|
||||
{
|
||||
@@ -207,22 +163,22 @@ namespace KeyboardManagerHelper
|
||||
// If shortcutToCompare is non-empty, then the key event is sent only if both shortcut's don't have the same modifier key. If keyToBeReleased is non-NULL, then the key event is sent if either the shortcuts don't have the same modifier or if the shortcutToBeSent's modifier matches the keyToBeReleased
|
||||
if (shortcutToBeSent.GetWinKey(winKeyInvoked) != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetWinKey(winKeyInvoked) != shortcutToCompare.GetWinKey(winKeyInvoked)) && (keyToBeReleased == NULL || !shortcutToBeSent.CheckWinKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetWinKey(winKeyInvoked), 0, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetWinKey(winKeyInvoked), 0, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetCtrlKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetCtrlKey() != shortcutToCompare.GetCtrlKey()) && (keyToBeReleased == NULL || !shortcutToBeSent.CheckCtrlKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetCtrlKey(), 0, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetCtrlKey(), 0, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetAltKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetAltKey() != shortcutToCompare.GetAltKey()) && (keyToBeReleased == NULL || !shortcutToBeSent.CheckAltKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetAltKey(), 0, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetAltKey(), 0, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetShiftKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetShiftKey() != shortcutToCompare.GetShiftKey()) && (keyToBeReleased == NULL || !shortcutToBeSent.CheckShiftKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetShiftKey(), 0, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetShiftKey(), 0, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
@@ -233,22 +189,22 @@ namespace KeyboardManagerHelper
|
||||
// If shortcutToCompare is non-empty, then the key event is sent only if both shortcut's don't have the same modifier key. If keyToBeReleased is non-NULL, then the key event is sent if either the shortcuts don't have the same modifier or if the shortcutToBeSent's modifier matches the keyToBeReleased
|
||||
if (shortcutToBeSent.GetShiftKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetShiftKey() != shortcutToCompare.GetShiftKey() || shortcutToBeSent.CheckShiftKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetShiftKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetShiftKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetAltKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetAltKey() != shortcutToCompare.GetAltKey() || shortcutToBeSent.CheckAltKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetAltKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetAltKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetCtrlKey() != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetCtrlKey() != shortcutToCompare.GetCtrlKey() || shortcutToBeSent.CheckCtrlKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetCtrlKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetCtrlKey(), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
if (shortcutToBeSent.GetWinKey(winKeyInvoked) != NULL && (shortcutToCompare.IsEmpty() || shortcutToBeSent.GetWinKey(winKeyInvoked) != shortcutToCompare.GetWinKey(winKeyInvoked) || shortcutToBeSent.CheckWinKey(keyToBeReleased)))
|
||||
{
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetWinKey(winKeyInvoked), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
Helpers::SetKeyEvent(keyEventArray, index, INPUT_KEYBOARD, (WORD)shortcutToBeSent.GetWinKey(winKeyInvoked), KEYEVENTF_KEYUP, extraInfoFlag);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
@@ -274,18 +230,4 @@ namespace KeyboardManagerHelper
|
||||
return first.Size() > second.Size();
|
||||
});
|
||||
}
|
||||
|
||||
// Function to check if a modifier has been repeated in the previous drop downs
|
||||
bool CheckRepeatedModifier(const std::vector<int32_t>& currentKeys, int selectedKeyCode)
|
||||
{
|
||||
// Count the number of keys that are equal to 'selectedKeyCode'
|
||||
int numberOfSameType = 0;
|
||||
for (int i = 0; i < currentKeys.size(); i++)
|
||||
{
|
||||
numberOfSameType += KeyboardManagerHelper::GetKeyType(selectedKeyCode) == KeyboardManagerHelper::GetKeyType(currentKeys[i]);
|
||||
}
|
||||
|
||||
// If we have at least two keys equal to 'selectedKeyCode' than modifier was repeated
|
||||
return numberOfSameType > 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
class LayoutMap;
|
||||
|
||||
namespace KeyboardManagerHelper
|
||||
namespace Helpers
|
||||
{
|
||||
// Type to distinguish between keys
|
||||
enum class KeyType
|
||||
@@ -15,29 +15,12 @@ namespace KeyboardManagerHelper
|
||||
Action
|
||||
};
|
||||
|
||||
// Enum type to store possible decision for input in the low level hook
|
||||
enum class KeyboardHookDecision
|
||||
{
|
||||
ContinueExec,
|
||||
Suppress,
|
||||
SkipHook
|
||||
};
|
||||
|
||||
// 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);
|
||||
|
||||
// Function to return if the key is an extended key which requires the use of the extended key flag
|
||||
bool IsExtendedKey(DWORD key);
|
||||
|
||||
// Function to check if the key is a modifier key
|
||||
bool IsModifierKey(DWORD key);
|
||||
|
||||
// Function to get the type of the key
|
||||
KeyType GetKeyType(DWORD key);
|
||||
|
||||
// Function to check if two keys are equal or cover the same set of keys. Return value depends on type of overlap
|
||||
ErrorType DoKeysOverlap(DWORD first, DWORD second);
|
||||
|
||||
// Function to set the value of a key event based on the arguments
|
||||
void SetKeyEvent(LPINPUT keyEventArray, int index, DWORD inputType, WORD keyCode, DWORD flags, ULONG_PTR extraInfo);
|
||||
|
||||
@@ -58,7 +41,4 @@ namespace KeyboardManagerHelper
|
||||
|
||||
// Function to sort a vector of shortcuts based on it's size
|
||||
void SortShortcutVectorBasedOnSize(std::vector<Shortcut>& shortcutVector);
|
||||
|
||||
// Function to check if a modifier has been repeated in the previous drop downs
|
||||
bool CheckRepeatedModifier(const std::vector<int32_t>& currentKeys, int selectedKeyCodes);
|
||||
}
|
||||
@@ -24,7 +24,7 @@ namespace KeyboardManagerInput
|
||||
// Function to get the foreground process name
|
||||
void GetForegroundProcess(_Out_ std::wstring& foregroundProcess)
|
||||
{
|
||||
foregroundProcess = KeyboardManagerHelper::GetCurrentApplication(false);
|
||||
foregroundProcess = Helpers::GetCurrentApplication(false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
#include "pch.h"
|
||||
#include "KeyDelay.h"
|
||||
|
||||
// NOTE: The destructor should never be called on the DelayThread, i.e. from any of shortPress, longPress or longPressReleased, as it will re-enter the mutex. Even if the mutex is removed it will deadlock because of the join statement
|
||||
KeyDelay::~KeyDelay()
|
||||
{
|
||||
std::unique_lock<std::mutex> l(_queueMutex);
|
||||
_quit = true;
|
||||
_cv.notify_all();
|
||||
l.unlock();
|
||||
_delayThread.join();
|
||||
}
|
||||
|
||||
void KeyDelay::KeyEvent(LowlevelKeyboardEvent* ev)
|
||||
{
|
||||
std::lock_guard guard(_queueMutex);
|
||||
_queue.push({ ev->lParam->time, ev->wParam });
|
||||
_cv.notify_all();
|
||||
}
|
||||
|
||||
KeyTimedEvent KeyDelay::NextEvent()
|
||||
{
|
||||
auto ev = _queue.front();
|
||||
_queue.pop();
|
||||
return ev;
|
||||
}
|
||||
|
||||
bool KeyDelay::CheckIfMillisHaveElapsed(DWORD64 first, DWORD64 last, DWORD64 duration)
|
||||
{
|
||||
if (first < last && first <= first + duration)
|
||||
{
|
||||
return first + duration < last;
|
||||
}
|
||||
else
|
||||
{
|
||||
first += ULLONG_MAX / 2;
|
||||
last += ULLONG_MAX / 2;
|
||||
return first + duration < last;
|
||||
}
|
||||
}
|
||||
|
||||
bool KeyDelay::HasNextEvent()
|
||||
{
|
||||
return !_queue.empty();
|
||||
}
|
||||
|
||||
bool KeyDelay::HandleRelease()
|
||||
{
|
||||
while (HasNextEvent())
|
||||
{
|
||||
auto ev = NextEvent();
|
||||
switch (ev.message)
|
||||
{
|
||||
case WM_KEYDOWN:
|
||||
case WM_SYSKEYDOWN:
|
||||
_state = KeyDelayState::ON_HOLD;
|
||||
_initialHoldKeyDown = ev.time;
|
||||
return false;
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYUP:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool KeyDelay::HandleOnHold(std::unique_lock<std::mutex>& cvLock)
|
||||
{
|
||||
while (HasNextEvent())
|
||||
{
|
||||
auto ev = NextEvent();
|
||||
switch (ev.message)
|
||||
{
|
||||
case WM_KEYDOWN:
|
||||
case WM_SYSKEYDOWN:
|
||||
break;
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYUP:
|
||||
if (CheckIfMillisHaveElapsed(_initialHoldKeyDown, ev.time, LONG_PRESS_DELAY_MILLIS))
|
||||
{
|
||||
if (_onLongPressDetected != nullptr)
|
||||
{
|
||||
_onLongPressDetected(_key);
|
||||
}
|
||||
if (_onLongPressReleased != nullptr)
|
||||
{
|
||||
_onLongPressReleased(_key);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_onShortPress != nullptr)
|
||||
{
|
||||
_onShortPress(_key);
|
||||
}
|
||||
}
|
||||
_state = KeyDelayState::RELEASED;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (CheckIfMillisHaveElapsed(_initialHoldKeyDown, GetTickCount64(), LONG_PRESS_DELAY_MILLIS))
|
||||
{
|
||||
if (_onLongPressDetected != nullptr)
|
||||
{
|
||||
_onLongPressDetected(_key);
|
||||
}
|
||||
_state = KeyDelayState::ON_HOLD_TIMEOUT;
|
||||
}
|
||||
else
|
||||
{
|
||||
_cv.wait_for(cvLock, std::chrono::milliseconds(ON_HOLD_WAIT_TIMEOUT_MILLIS));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KeyDelay::HandleOnHoldTimeout()
|
||||
{
|
||||
while (HasNextEvent())
|
||||
{
|
||||
auto ev = NextEvent();
|
||||
switch (ev.message)
|
||||
{
|
||||
case WM_KEYDOWN:
|
||||
case WM_SYSKEYDOWN:
|
||||
break;
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYUP:
|
||||
if (_onLongPressReleased != nullptr)
|
||||
{
|
||||
_onLongPressReleased(_key);
|
||||
}
|
||||
_state = KeyDelayState::RELEASED;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void KeyDelay::DelayThread()
|
||||
{
|
||||
std::unique_lock<std::mutex> qLock(_queueMutex);
|
||||
bool shouldWait = true;
|
||||
while (!_quit)
|
||||
{
|
||||
if (shouldWait)
|
||||
{
|
||||
_cv.wait(qLock);
|
||||
}
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case KeyDelayState::RELEASED:
|
||||
shouldWait = HandleRelease();
|
||||
break;
|
||||
case KeyDelayState::ON_HOLD:
|
||||
shouldWait = HandleOnHold(qLock);
|
||||
break;
|
||||
case KeyDelayState::ON_HOLD_TIMEOUT:
|
||||
shouldWait = HandleOnHoldTimeout();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
|
||||
#include <common/hooks/LowlevelKeyboardEvent.h>
|
||||
// Available states for the KeyDelay state machine.
|
||||
enum class KeyDelayState
|
||||
{
|
||||
RELEASED,
|
||||
ON_HOLD,
|
||||
ON_HOLD_TIMEOUT,
|
||||
};
|
||||
|
||||
// Virtual key + timestamp (in millis since Windows startup)
|
||||
struct KeyTimedEvent
|
||||
{
|
||||
DWORD64 time;
|
||||
WPARAM message;
|
||||
};
|
||||
|
||||
// Handles delayed key inputs.
|
||||
// Implemented as a state machine running on its own thread.
|
||||
// Thread stops on destruction.
|
||||
class KeyDelay
|
||||
{
|
||||
public:
|
||||
KeyDelay(
|
||||
DWORD key,
|
||||
std::function<void(DWORD)> onShortPress,
|
||||
std::function<void(DWORD)> onLongPressDetected,
|
||||
std::function<void(DWORD)> onLongPressReleased) :
|
||||
_quit(false),
|
||||
_state(KeyDelayState::RELEASED),
|
||||
_initialHoldKeyDown(0),
|
||||
_key(key),
|
||||
_onShortPress(onShortPress),
|
||||
_onLongPressDetected(onLongPressDetected),
|
||||
_onLongPressReleased(onLongPressReleased),
|
||||
_delayThread(&KeyDelay::DelayThread, this){};
|
||||
|
||||
// Enque new KeyTimedEvent and notify the condition variable.
|
||||
void KeyEvent(LowlevelKeyboardEvent* ev);
|
||||
~KeyDelay();
|
||||
|
||||
private:
|
||||
// Runs the state machine, waits if there is no events to process.
|
||||
// Checks for _quit condition.
|
||||
void DelayThread();
|
||||
|
||||
// Manage state transitions and trigger callbacks on certain events.
|
||||
// Returns whether or not the thread should wait on new events.
|
||||
bool HandleRelease();
|
||||
bool HandleOnHold(std::unique_lock<std::mutex>& cvLock);
|
||||
bool HandleOnHoldTimeout();
|
||||
|
||||
// Get next key event in queue.
|
||||
KeyTimedEvent NextEvent();
|
||||
bool HasNextEvent();
|
||||
|
||||
// Check if <duration> milliseconds passed since <first> millisecond.
|
||||
// Also checks for overflow conditions.
|
||||
bool CheckIfMillisHaveElapsed(DWORD64 first, DWORD64 last, DWORD64 duration);
|
||||
|
||||
bool _quit;
|
||||
KeyDelayState _state;
|
||||
|
||||
// Callback functions, the key provided in the constructor is passed as an argument.
|
||||
std::function<void(DWORD)> _onLongPressDetected;
|
||||
std::function<void(DWORD)> _onLongPressReleased;
|
||||
std::function<void(DWORD)> _onShortPress;
|
||||
|
||||
// Queue holding key events that are not processed yet. Should be kept synchronized
|
||||
// using _queueMutex
|
||||
std::queue<KeyTimedEvent> _queue;
|
||||
std::mutex _queueMutex;
|
||||
|
||||
// DelayThread waits on this condition variable when there is no events to process.
|
||||
std::condition_variable _cv;
|
||||
|
||||
// Keeps track of the time at which the initial KEY_DOWN event happened.
|
||||
DWORD64 _initialHoldKeyDown;
|
||||
|
||||
// Virtual Key provided in the constructor. Passed to callback functions.
|
||||
DWORD _key;
|
||||
|
||||
// Declare _delayThread after all other members so that it is the last to be initialized by the constructor
|
||||
std::thread _delayThread;
|
||||
|
||||
static const DWORD64 LONG_PRESS_DELAY_MILLIS = 900;
|
||||
static const DWORD64 ON_HOLD_WAIT_TIMEOUT_MILLIS = 50;
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "pch.h"
|
||||
#include "KeyboardEventHandlers.h"
|
||||
#include <keyboardmanager/common/KeyboardManagerState.h>
|
||||
#include <keyboardmanager/common/InputInterface.h>
|
||||
#include <keyboardmanager/common/Helpers.h>
|
||||
#include <keyboardmanager/common/KeyboardManagerConstants.h>
|
||||
|
||||
namespace KeyboardEventHandlers
|
||||
{
|
||||
@@ -16,8 +16,8 @@ namespace KeyboardEventHandlers
|
||||
memset(keyEventList, 0, sizeof(keyEventList));
|
||||
|
||||
// Use the suppress flag to ensure these are not intercepted by any remapped keys or shortcuts
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventList, 0, INPUT_KEYBOARD, VK_NUMLOCK, KEYEVENTF_KEYUP, KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG);
|
||||
KeyboardManagerHelper::SetKeyEvent(keyEventList, 1, INPUT_KEYBOARD, VK_NUMLOCK, 0, KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG);
|
||||
Helpers::SetKeyEvent(keyEventList, 0, INPUT_KEYBOARD, VK_NUMLOCK, KEYEVENTF_KEYUP, KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG);
|
||||
Helpers::SetKeyEvent(keyEventList, 1, INPUT_KEYBOARD, VK_NUMLOCK, 0, KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG);
|
||||
UINT res = ii.SendVirtualInput((UINT)key_count, keyEventList, sizeof(INPUT));
|
||||
delete[] keyEventList;
|
||||
}
|
||||
|
||||
@@ -46,36 +46,28 @@
|
||||
<ClCompile Include="..\..\..\common\interop\keyboard_layout.cpp" />
|
||||
<ClCompile Include="Helpers.cpp" />
|
||||
<ClCompile Include="KeyboardEventHandlers.cpp" />
|
||||
<ClCompile Include="KeyboardManagerState.cpp" />
|
||||
<ClCompile Include="KeyDelay.cpp" />
|
||||
<ClCompile Include="MappingConfiguration.cpp" />
|
||||
<ClCompile Include="pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SettingsHelper.cpp" />
|
||||
<ClCompile Include="Shortcut.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ErrorTypes.h" />
|
||||
<ClInclude Include="Input.h" />
|
||||
<ClInclude Include="KeyboardEventHandlers.h" />
|
||||
<ClInclude Include="MappingConfiguration.h" />
|
||||
<ClInclude Include="ModifierKey.h" />
|
||||
<ClInclude Include="InputInterface.h" />
|
||||
<ClInclude Include="Helpers.h" />
|
||||
<ClInclude Include="KeyboardManagerConstants.h" />
|
||||
<ClInclude Include="KeyboardManagerState.h" />
|
||||
<ClInclude Include="KeyDelay.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
<ClInclude Include="RemapShortcut.h" />
|
||||
<ClInclude Include="SettingsHelper.h" />
|
||||
<ClInclude Include="Shortcut.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\common\COMUtils\COMUtils.vcxproj">
|
||||
<Project>{7319089e-46d6-4400-bc65-e39bdf1416ee}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\Display\Display.vcxproj">
|
||||
<Project>{caba8dfb-823b-4bf2-93ac-3f31984150d9}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -15,9 +15,6 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="KeyboardManagerState.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Helpers.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@@ -27,23 +24,17 @@
|
||||
<ClCompile Include="Shortcut.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KeyDelay.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\common\interop\keyboard_layout.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SettingsHelper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KeyboardEventHandlers.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MappingConfiguration.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="KeyboardManagerState.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Helpers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@@ -56,9 +47,6 @@
|
||||
<ClInclude Include="RemapShortcut.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KeyDelay.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KeyboardManagerConstants.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@@ -68,16 +56,13 @@
|
||||
<ClInclude Include="ModifierKey.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SettingsHelper.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KeyboardEventHandlers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Input.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ErrorTypes.h">
|
||||
<ClInclude Include="MappingConfiguration.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -40,67 +40,11 @@ namespace KeyboardManagerConstants
|
||||
// Name of the default configuration.
|
||||
inline const std::wstring DefaultConfiguration = L"default";
|
||||
|
||||
// Name of the dummy update file.
|
||||
inline const std::wstring DummyUpdateFileName = L"settings-updated.json";
|
||||
|
||||
// Minimum and maximum size of a shortcut
|
||||
inline const long MinShortcutSize = 2;
|
||||
inline const long MaxShortcutSize = 3;
|
||||
|
||||
// Default window sizes
|
||||
inline const int DefaultEditKeyboardWindowWidth = 800;
|
||||
inline const int DefaultEditKeyboardWindowHeight = 600;
|
||||
|
||||
// Increasing the min size can cause issues when moving the window between
|
||||
// monitors with different DPI scaling factor
|
||||
inline const int MinimumEditKeyboardWindowWidth = 200;
|
||||
inline const int MinimumEditKeyboardWindowHeight = 200;
|
||||
|
||||
inline const int EditKeyboardTableMinWidth = 700;
|
||||
|
||||
inline const int DefaultEditShortcutsWindowWidth = 1050;
|
||||
inline const int DefaultEditShortcutsWindowHeight = 600;
|
||||
|
||||
// Increasing the min size can cause issues when moving the window between
|
||||
// monitors with different DPI scaling factor
|
||||
inline const int MinimumEditShortcutsWindowWidth = 200;
|
||||
inline const int MinimumEditShortcutsWindowHeight = 200;
|
||||
|
||||
inline const int EditShortcutsTableMinWidth = 1000;
|
||||
|
||||
// Key Remap table constants
|
||||
inline const long RemapTableColCount = 4;
|
||||
inline const long RemapTableHeaderCount = 2;
|
||||
inline const long RemapTableOriginalColIndex = 0;
|
||||
inline const long RemapTableArrowColIndex = 1;
|
||||
inline const long RemapTableNewColIndex = 2;
|
||||
inline const long RemapTableRemoveColIndex = 3;
|
||||
inline const DWORD64 RemapTableDropDownWidth = 110;
|
||||
|
||||
// Shortcut table constants
|
||||
inline const long ShortcutTableColCount = 5;
|
||||
inline const long ShortcutTableHeaderCount = 3;
|
||||
inline const long ShortcutTableOriginalColIndex = 0;
|
||||
inline const long ShortcutTableArrowColIndex = 1;
|
||||
inline const long ShortcutTableNewColIndex = 2;
|
||||
inline const long ShortcutTableTargetAppColIndex = 3;
|
||||
inline const long ShortcutTableRemoveColIndex = 4;
|
||||
inline const long ShortcutArrowColumnWidth = 90;
|
||||
inline const DWORD64 ShortcutTableDropDownWidth = 110;
|
||||
inline const DWORD64 ShortcutTableDropDownSpacing = 10;
|
||||
inline const long ShortcutOriginColumnWidth = 3 * ShortcutTableDropDownWidth + 2 * ShortcutTableDropDownSpacing;
|
||||
inline const long ShortcutTargetColumnWidth = 3 * ShortcutTableDropDownWidth + 2 * ShortcutTableDropDownSpacing + 25;
|
||||
|
||||
// Drop down height used for both Edit Keyboard and Edit Shortcuts
|
||||
inline const DWORD64 TableDropDownHeight = 200;
|
||||
inline const DWORD64 TableArrowColWidth = 230;
|
||||
inline const DWORD64 TableRemoveColWidth = 20;
|
||||
inline const DWORD64 TableWarningColWidth = 20;
|
||||
inline const DWORD64 TableTargetAppColWidth = ShortcutTableDropDownWidth + TableRemoveColWidth * 2;
|
||||
|
||||
// Shared style constants for both Remap Table and Shortcut Table
|
||||
inline const DWORD64 HeaderButtonWidth = 100;
|
||||
|
||||
// Flags used for distinguishing key events sent by Keyboard Manager
|
||||
inline const ULONG_PTR KEYBOARDMANAGER_SINGLEKEY_FLAG = 0x11; // Single key remaps
|
||||
inline const ULONG_PTR KEYBOARDMANAGER_SHORTCUT_FLAG = 0x101; // Shortcut remaps
|
||||
|
||||
@@ -1,624 +0,0 @@
|
||||
#include "pch.h"
|
||||
#include "KeyboardManagerState.h"
|
||||
#include "Shortcut.h"
|
||||
#include "RemapShortcut.h"
|
||||
#include <common/SettingsAPI/settings_helpers.h>
|
||||
#include "KeyDelay.h"
|
||||
#include "Helpers.h"
|
||||
#include <common/logger/logger.h>
|
||||
|
||||
// Constructor
|
||||
KeyboardManagerState::KeyboardManagerState() :
|
||||
uiState(KeyboardManagerUIState::Deactivated), currentUIWindow(nullptr), currentShortcutUI1(nullptr), currentShortcutUI2(nullptr), currentSingleKeyUI(nullptr), detectedRemapKey(NULL), remappingsEnabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
// Destructor
|
||||
KeyboardManagerState::~KeyboardManagerState()
|
||||
{
|
||||
}
|
||||
|
||||
// Function to check the if the UI state matches the argument state. For states with detect windows it also checks if the window is in focus.
|
||||
bool KeyboardManagerState::CheckUIState(KeyboardManagerUIState state)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(uiState_mutex);
|
||||
if (uiState == state)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(currentUIWindow_mutex);
|
||||
if (uiState == KeyboardManagerUIState::Deactivated || uiState == KeyboardManagerUIState::EditKeyboardWindowActivated || uiState == KeyboardManagerUIState::EditShortcutsWindowActivated)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// If the UI state is a detect window then we also have to ensure that the UI window is in focus.
|
||||
// GetForegroundWindow can be used here since we only need to check the main parent window and not the sub windows within the content dialog. Using GUIThreadInfo will give more specific sub-windows within the XAML window which is not needed.
|
||||
else if (currentUIWindow == GetForegroundWindow())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// If we are checking for EditKeyboardWindowActivated then it's possible the state could be DetectSingleKeyRemapWindowActivated but not in focus
|
||||
else if (state == KeyboardManagerUIState::EditKeyboardWindowActivated && uiState == KeyboardManagerUIState::DetectSingleKeyRemapWindowActivated)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// If we are checking for EditShortcutsWindowActivated then it's possible the state could be DetectShortcutWindowActivated but not in focus
|
||||
else if (state == KeyboardManagerUIState::EditShortcutsWindowActivated && uiState == KeyboardManagerUIState::DetectShortcutWindowActivated)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to set the window handle of the current UI window that is activated
|
||||
void KeyboardManagerState::SetCurrentUIWindow(HWND windowHandle)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentUIWindow_mutex);
|
||||
currentUIWindow = windowHandle;
|
||||
}
|
||||
|
||||
// Function to set the UI state. When a window is activated, the handle to the window can be passed in the windowHandle argument.
|
||||
void KeyboardManagerState::SetUIState(KeyboardManagerUIState state, HWND windowHandle)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(uiState_mutex);
|
||||
uiState = state;
|
||||
SetCurrentUIWindow(windowHandle);
|
||||
}
|
||||
|
||||
// Function to reset the UI state members
|
||||
void KeyboardManagerState::ResetUIState()
|
||||
{
|
||||
SetUIState(KeyboardManagerUIState::Deactivated);
|
||||
|
||||
// Reset the shortcut UI stored variables
|
||||
std::unique_lock<std::mutex> currentShortcutUI_lock(currentShortcutUI_mutex);
|
||||
currentShortcutUI1 = nullptr;
|
||||
currentShortcutUI2 = nullptr;
|
||||
currentShortcutUI_lock.unlock();
|
||||
|
||||
std::unique_lock<std::mutex> detectedShortcut_lock(detectedShortcut_mutex);
|
||||
detectedShortcut.Reset();
|
||||
detectedShortcut_lock.unlock();
|
||||
|
||||
std::unique_lock<std::mutex> currentShortcut_lock(currentShortcut_mutex);
|
||||
currentShortcut.Reset();
|
||||
currentShortcut_lock.unlock();
|
||||
|
||||
// Reset all the single key remap UI stored variables.
|
||||
std::unique_lock<std::mutex> currentSingleKeyUI_lock(currentSingleKeyUI_mutex);
|
||||
currentSingleKeyUI = nullptr;
|
||||
currentSingleKeyUI_lock.unlock();
|
||||
|
||||
std::unique_lock<std::mutex> detectedRemapKey_lock(detectedRemapKey_mutex);
|
||||
detectedRemapKey = NULL;
|
||||
detectedRemapKey_lock.unlock();
|
||||
}
|
||||
|
||||
// Function to clear the OS Level shortcut remapping table
|
||||
void KeyboardManagerState::ClearOSLevelShortcuts()
|
||||
{
|
||||
osLevelShortcutReMap.clear();
|
||||
osLevelShortcutReMapSortedKeys.clear();
|
||||
}
|
||||
|
||||
// Function to clear the Keys remapping table.
|
||||
void KeyboardManagerState::ClearSingleKeyRemaps()
|
||||
{
|
||||
singleKeyReMap.clear();
|
||||
}
|
||||
|
||||
// Function to clear the App specific shortcut remapping table
|
||||
void KeyboardManagerState::ClearAppSpecificShortcuts()
|
||||
{
|
||||
appSpecificShortcutReMap.clear();
|
||||
appSpecificShortcutReMapSortedKeys.clear();
|
||||
}
|
||||
|
||||
// Function to add a new OS level shortcut remapping
|
||||
bool KeyboardManagerState::AddOSLevelShortcut(const Shortcut& originalSC, const KeyShortcutUnion& newSC)
|
||||
{
|
||||
// Check if the shortcut is already remapped
|
||||
auto it = osLevelShortcutReMap.find(originalSC);
|
||||
if (it != osLevelShortcutReMap.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
osLevelShortcutReMap[originalSC] = RemapShortcut(newSC);
|
||||
osLevelShortcutReMapSortedKeys.push_back(originalSC);
|
||||
KeyboardManagerHelper::SortShortcutVectorBasedOnSize(osLevelShortcutReMapSortedKeys);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to add a new single key to key/shortcut remapping
|
||||
bool KeyboardManagerState::AddSingleKeyRemap(const DWORD& originalKey, const KeyShortcutUnion& newRemapKey)
|
||||
{
|
||||
// Check if the key is already remapped
|
||||
auto it = singleKeyReMap.find(originalKey);
|
||||
if (it != singleKeyReMap.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
singleKeyReMap[originalKey] = newRemapKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to add a new App specific shortcut remapping
|
||||
bool KeyboardManagerState::AddAppSpecificShortcut(const std::wstring& app, const Shortcut& originalSC, const KeyShortcutUnion& newSC)
|
||||
{
|
||||
// Convert app name to lower case
|
||||
std::wstring process_name;
|
||||
process_name.resize(app.length());
|
||||
std::transform(app.begin(), app.end(), process_name.begin(), towlower);
|
||||
|
||||
// Check if there are any app specific shortcuts for this app
|
||||
auto appIt = appSpecificShortcutReMap.find(process_name);
|
||||
if (appIt != appSpecificShortcutReMap.end())
|
||||
{
|
||||
// Check if the shortcut is already remapped
|
||||
auto shortcutIt = appSpecificShortcutReMap[process_name].find(originalSC);
|
||||
if (shortcutIt != appSpecificShortcutReMap[process_name].end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
appSpecificShortcutReMapSortedKeys[process_name] = std::vector<Shortcut>();
|
||||
}
|
||||
|
||||
appSpecificShortcutReMap[process_name][originalSC] = RemapShortcut(newSC);
|
||||
appSpecificShortcutReMapSortedKeys[process_name].push_back(originalSC);
|
||||
KeyboardManagerHelper::SortShortcutVectorBasedOnSize(appSpecificShortcutReMapSortedKeys[process_name]);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to get the iterator of a single key remap given the source key. Returns nullopt if it isn't remapped
|
||||
std::optional<SingleKeyRemapTable::iterator> KeyboardManagerState::GetSingleKeyRemap(const DWORD& originalKey)
|
||||
{
|
||||
auto it = singleKeyReMap.find(originalKey);
|
||||
if (it != singleKeyReMap.end())
|
||||
{
|
||||
return it;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool KeyboardManagerState::CheckShortcutRemapInvoked(const std::optional<std::wstring>& appName)
|
||||
{
|
||||
// Assumes appName exists in the app-specific remap table
|
||||
ShortcutRemapTable& currentRemapTable = appName ? appSpecificShortcutReMap[*appName] : osLevelShortcutReMap;
|
||||
for (auto& it : currentRemapTable)
|
||||
{
|
||||
if (it.second.isShortcutInvoked)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<Shortcut>& KeyboardManagerState::GetSortedShortcutRemapVector(const std::optional<std::wstring>& appName)
|
||||
{
|
||||
// Assumes appName exists in the app-specific remap table
|
||||
return appName ? appSpecificShortcutReMapSortedKeys[*appName] : osLevelShortcutReMapSortedKeys;
|
||||
}
|
||||
|
||||
// Function to get the source and target of a shortcut remap given the source shortcut. Returns nullopt if it isn't remapped
|
||||
ShortcutRemapTable& KeyboardManagerState::GetShortcutRemapTable(const std::optional<std::wstring>& appName)
|
||||
{
|
||||
if (appName)
|
||||
{
|
||||
auto itTable = appSpecificShortcutReMap.find(*appName);
|
||||
if (itTable != appSpecificShortcutReMap.end())
|
||||
{
|
||||
return itTable->second;
|
||||
}
|
||||
}
|
||||
|
||||
return osLevelShortcutReMap;
|
||||
}
|
||||
|
||||
// Function to set the textblock of the detect shortcut UI so that it can be accessed by the hook
|
||||
void KeyboardManagerState::ConfigureDetectShortcutUI(const StackPanel& textBlock1, const StackPanel& textBlock2)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentShortcutUI_mutex);
|
||||
currentShortcutUI1 = textBlock1.as<winrt::Windows::Foundation::IInspectable>();
|
||||
currentShortcutUI2 = textBlock2.as<winrt::Windows::Foundation::IInspectable>();
|
||||
}
|
||||
|
||||
// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
|
||||
void KeyboardManagerState::ConfigureDetectSingleKeyRemapUI(const StackPanel& textBlock)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentSingleKeyUI_mutex);
|
||||
currentSingleKeyUI = textBlock.as<winrt::Windows::Foundation::IInspectable>();
|
||||
}
|
||||
|
||||
void KeyboardManagerState::AddKeyToLayout(const StackPanel& panel, const hstring& key)
|
||||
{
|
||||
// Textblock to display the detected key
|
||||
TextBlock remapKey;
|
||||
Border border;
|
||||
|
||||
border.Padding({ 20, 10, 20, 10 });
|
||||
border.Margin({ 0, 0, 10, 0 });
|
||||
// Use the base low brush to be consistent with the theme
|
||||
border.Background(Windows::UI::Xaml::Application::Current().Resources().Lookup(box_value(L"SystemControlBackgroundBaseLowBrush")).as<Windows::UI::Xaml::Media::SolidColorBrush>());
|
||||
remapKey.FontSize(20);
|
||||
border.HorizontalAlignment(HorizontalAlignment::Left);
|
||||
border.Child(remapKey);
|
||||
|
||||
remapKey.Text(key);
|
||||
panel.Children().Append(border);
|
||||
}
|
||||
|
||||
// Function to update the detect shortcut UI based on the entered keys
|
||||
void KeyboardManagerState::UpdateDetectShortcutUI()
|
||||
{
|
||||
std::lock_guard<std::mutex> currentShortcutUI_lock(currentShortcutUI_mutex);
|
||||
if (currentShortcutUI1 == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> detectedShortcut_lock(detectedShortcut_mutex);
|
||||
std::unique_lock<std::mutex> currentShortcut_lock(currentShortcut_mutex);
|
||||
// Save the latest displayed shortcut
|
||||
currentShortcut = detectedShortcut;
|
||||
auto detectedShortcutCopy = detectedShortcut;
|
||||
currentShortcut_lock.unlock();
|
||||
detectedShortcut_lock.unlock();
|
||||
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
|
||||
currentShortcutUI1.as<StackPanel>().Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this, detectedShortcutCopy]() {
|
||||
std::vector<hstring> shortcut = detectedShortcutCopy.GetKeyVector(keyboardMap);
|
||||
currentShortcutUI1.as<StackPanel>().Children().Clear();
|
||||
currentShortcutUI2.as<StackPanel>().Children().Clear();
|
||||
|
||||
// The second row should be hidden if there are 3 keys or lesser to avoid an extra margin
|
||||
if (shortcut.size() > 3)
|
||||
{
|
||||
currentShortcutUI2.as<StackPanel>().Visibility(Visibility::Visible);
|
||||
}
|
||||
else
|
||||
{
|
||||
currentShortcutUI2.as<StackPanel>().Visibility(Visibility::Collapsed);
|
||||
}
|
||||
|
||||
for (int i = 0; i < shortcut.size(); i++)
|
||||
{
|
||||
if (i < 3)
|
||||
{
|
||||
AddKeyToLayout(currentShortcutUI1.as<StackPanel>(), shortcut[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddKeyToLayout(currentShortcutUI2.as<StackPanel>(), shortcut[i]);
|
||||
}
|
||||
}
|
||||
currentShortcutUI1.as<StackPanel>().UpdateLayout();
|
||||
currentShortcutUI2.as<StackPanel>().UpdateLayout();
|
||||
});
|
||||
}
|
||||
|
||||
// Function to update the detect remap key UI based on the entered key.
|
||||
void KeyboardManagerState::UpdateDetectSingleKeyRemapUI()
|
||||
{
|
||||
std::lock_guard<std::mutex> currentSingleKeyUI_lock(currentSingleKeyUI_mutex);
|
||||
if (currentSingleKeyUI == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
|
||||
currentSingleKeyUI.as<StackPanel>().Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
|
||||
currentSingleKeyUI.as<StackPanel>().Children().Clear();
|
||||
hstring key = winrt::to_hstring(keyboardMap.GetKeyName(detectedRemapKey).c_str());
|
||||
AddKeyToLayout(currentSingleKeyUI.as<StackPanel>(), key);
|
||||
currentSingleKeyUI.as<StackPanel>().UpdateLayout();
|
||||
});
|
||||
}
|
||||
|
||||
// Function to return the currently detected shortcut which is displayed on the UI
|
||||
Shortcut KeyboardManagerState::GetDetectedShortcut()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentShortcut_mutex);
|
||||
return currentShortcut;
|
||||
}
|
||||
|
||||
// Function to return the currently detected remap key which is displayed on the UI
|
||||
DWORD KeyboardManagerState::GetDetectedSingleRemapKey()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectedRemapKey_mutex);
|
||||
return detectedRemapKey;
|
||||
}
|
||||
|
||||
void KeyboardManagerState::SelectDetectedRemapKey(DWORD key)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(detectedRemapKey_mutex);
|
||||
detectedRemapKey = key;
|
||||
UpdateDetectSingleKeyRemapUI();
|
||||
return;
|
||||
}
|
||||
|
||||
void KeyboardManagerState::SelectDetectedShortcut(DWORD key)
|
||||
{
|
||||
// Set the new key and store if a change occurred
|
||||
std::unique_lock<std::mutex> lock(detectedShortcut_mutex);
|
||||
bool updateUI = detectedShortcut.SetKey(key);
|
||||
lock.unlock();
|
||||
|
||||
if (updateUI)
|
||||
{
|
||||
// Update the UI. This function is called here because it should store the set of keys pressed till the last key which was pressed down.
|
||||
UpdateDetectShortcutUI();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void KeyboardManagerState::ResetDetectedShortcutKey(DWORD key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectedShortcut_mutex);
|
||||
detectedShortcut.ResetKey(key);
|
||||
}
|
||||
|
||||
// Function which can be used in HandleKeyboardHookEvent before the single key remap event to use the UI and suppress events while the remap window is active.
|
||||
KeyboardManagerHelper::KeyboardHookDecision KeyboardManagerState::DetectSingleRemapKeyUIBackend(LowlevelKeyboardEvent* data)
|
||||
{
|
||||
// Check if the detect key UI window has been activated
|
||||
if (CheckUIState(KeyboardManagerUIState::DetectSingleKeyRemapWindowActivated))
|
||||
{
|
||||
if (HandleKeyDelayEvent(data))
|
||||
{
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::Suppress;
|
||||
}
|
||||
// detect the key if it is pressed down
|
||||
if (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN)
|
||||
{
|
||||
SelectDetectedRemapKey(data->lParam->vkCode);
|
||||
}
|
||||
|
||||
// Suppress the keyboard event
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::Suppress;
|
||||
}
|
||||
|
||||
// If the settings window is up, remappings should not be applied, but we should not suppress events in the hook
|
||||
else if (CheckUIState(KeyboardManagerUIState::EditKeyboardWindowActivated))
|
||||
{
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::SkipHook;
|
||||
}
|
||||
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::ContinueExec;
|
||||
}
|
||||
|
||||
// Function which can be used in HandleKeyboardHookEvent before the os level shortcut remap event to use the UI and suppress events while the remap window is active.
|
||||
KeyboardManagerHelper::KeyboardHookDecision KeyboardManagerState::DetectShortcutUIBackend(LowlevelKeyboardEvent* data, bool isRemapKey)
|
||||
{
|
||||
// Check if the detect shortcut UI window has been activated
|
||||
if ((!isRemapKey && CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated)) || (isRemapKey && CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated)))
|
||||
{
|
||||
if (HandleKeyDelayEvent(data))
|
||||
{
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::Suppress;
|
||||
}
|
||||
|
||||
// Add the key if it is pressed down
|
||||
if (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN)
|
||||
{
|
||||
SelectDetectedShortcut(data->lParam->vkCode);
|
||||
}
|
||||
// Remove the key if it has been released
|
||||
else if (data->wParam == WM_KEYUP || data->wParam == WM_SYSKEYUP)
|
||||
{
|
||||
ResetDetectedShortcutKey(data->lParam->vkCode);
|
||||
}
|
||||
|
||||
// Suppress the keyboard event
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::Suppress;
|
||||
}
|
||||
|
||||
// If the detect shortcut UI window is not activated, then clear the shortcut buffer if it isn't empty
|
||||
else if (!CheckUIState(KeyboardManagerUIState::DetectShortcutWindowActivated) && !CheckUIState(KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated))
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(detectedShortcut_mutex);
|
||||
if (!detectedShortcut.IsEmpty())
|
||||
{
|
||||
detectedShortcut.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
// If the settings window is up, shortcut remappings should not be applied, but we should not suppress events in the hook
|
||||
if (!isRemapKey && (CheckUIState(KeyboardManagerUIState::EditShortcutsWindowActivated)) || (isRemapKey && uiState == KeyboardManagerUIState::DetectShortcutWindowInEditKeyboardWindowActivated))
|
||||
{
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::SkipHook;
|
||||
}
|
||||
|
||||
return KeyboardManagerHelper::KeyboardHookDecision::ContinueExec;
|
||||
}
|
||||
|
||||
void KeyboardManagerState::RegisterKeyDelay(
|
||||
DWORD key,
|
||||
std::function<void(DWORD)> onShortPress,
|
||||
std::function<void(DWORD)> onLongPressDetected,
|
||||
std::function<void(DWORD)> onLongPressReleased)
|
||||
{
|
||||
std::lock_guard l(keyDelays_mutex);
|
||||
|
||||
if (keyDelays.find(key) != keyDelays.end())
|
||||
{
|
||||
throw std::invalid_argument("This key was already registered.");
|
||||
}
|
||||
|
||||
keyDelays[key] = std::make_unique<KeyDelay>(key, onShortPress, onLongPressDetected, onLongPressReleased);
|
||||
}
|
||||
|
||||
void KeyboardManagerState::UnregisterKeyDelay(DWORD key)
|
||||
{
|
||||
std::lock_guard l(keyDelays_mutex);
|
||||
|
||||
auto deleted = keyDelays.erase(key);
|
||||
if (deleted == 0)
|
||||
{
|
||||
throw std::invalid_argument("The key was not previously registered.");
|
||||
}
|
||||
}
|
||||
|
||||
// Function to clear all the registered key delays
|
||||
void KeyboardManagerState::ClearRegisteredKeyDelays()
|
||||
{
|
||||
std::lock_guard l(keyDelays_mutex);
|
||||
keyDelays.clear();
|
||||
}
|
||||
|
||||
bool KeyboardManagerState::HandleKeyDelayEvent(LowlevelKeyboardEvent* ev)
|
||||
{
|
||||
if (currentUIWindow != GetForegroundWindow())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard l(keyDelays_mutex);
|
||||
|
||||
if (keyDelays.find(ev->lParam->vkCode) == keyDelays.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
keyDelays[ev->lParam->vkCode]->KeyEvent(ev);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Save the updated configuration.
|
||||
bool KeyboardManagerState::SaveConfigToFile()
|
||||
{
|
||||
bool result = true;
|
||||
json::JsonObject configJson;
|
||||
json::JsonObject remapShortcuts;
|
||||
json::JsonObject remapKeys;
|
||||
json::JsonArray inProcessRemapKeysArray;
|
||||
json::JsonArray appSpecificRemapShortcutsArray;
|
||||
json::JsonArray globalRemapShortcutsArray;
|
||||
for (const auto& it : singleKeyReMap)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(winrt::to_hstring((unsigned int)it.first)));
|
||||
|
||||
// For key to key remapping
|
||||
if (it.second.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(it.second))));
|
||||
}
|
||||
|
||||
// For key to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(it.second).ToHstringVK()));
|
||||
}
|
||||
|
||||
inProcessRemapKeysArray.Append(keys);
|
||||
}
|
||||
|
||||
for (const auto& it : osLevelShortcutReMap)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(it.first.ToHstringVK()));
|
||||
|
||||
// For shortcut to key remapping
|
||||
if (it.second.targetShortcut.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(it.second.targetShortcut))));
|
||||
}
|
||||
|
||||
// For shortcut to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(it.second.targetShortcut).ToHstringVK()));
|
||||
}
|
||||
|
||||
globalRemapShortcutsArray.Append(keys);
|
||||
}
|
||||
|
||||
for (const auto& itApp : appSpecificShortcutReMap)
|
||||
{
|
||||
// Iterate over apps
|
||||
for (const auto& itKeys : itApp.second)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(itKeys.first.ToHstringVK()));
|
||||
|
||||
// For shortcut to key remapping
|
||||
if (itKeys.second.targetShortcut.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(itKeys.second.targetShortcut))));
|
||||
}
|
||||
|
||||
// For shortcut to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(itKeys.second.targetShortcut).ToHstringVK()));
|
||||
}
|
||||
|
||||
keys.SetNamedValue(KeyboardManagerConstants::TargetAppSettingName, json::value(itApp.first));
|
||||
|
||||
appSpecificRemapShortcutsArray.Append(keys);
|
||||
}
|
||||
}
|
||||
|
||||
remapShortcuts.SetNamedValue(KeyboardManagerConstants::GlobalRemapShortcutsSettingName, globalRemapShortcutsArray);
|
||||
remapShortcuts.SetNamedValue(KeyboardManagerConstants::AppSpecificRemapShortcutsSettingName, appSpecificRemapShortcutsArray);
|
||||
remapKeys.SetNamedValue(KeyboardManagerConstants::InProcessRemapKeysSettingName, inProcessRemapKeysArray);
|
||||
configJson.SetNamedValue(KeyboardManagerConstants::RemapKeysSettingName, remapKeys);
|
||||
configJson.SetNamedValue(KeyboardManagerConstants::RemapShortcutsSettingName, remapShortcuts);
|
||||
|
||||
try
|
||||
{
|
||||
json::to_file((PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName) + L"\\" + GetCurrentConfigName() + L".json"), configJson);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
result = false;
|
||||
Logger::error(L"Failed to save the settings");
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
auto hEvent = CreateEvent(nullptr, false, false, KeyboardManagerConstants::SettingsEventName.c_str());
|
||||
if (hEvent)
|
||||
{
|
||||
SetEvent(hEvent);
|
||||
Logger::trace(L"Signaled {} event", KeyboardManagerConstants::SettingsEventName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::error(L"Failed to signal {} event", KeyboardManagerConstants::SettingsEventName);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void KeyboardManagerState::SetCurrentConfigName(const std::wstring& configName)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentConfig_mutex);
|
||||
currentConfig = configName;
|
||||
}
|
||||
|
||||
std::wstring KeyboardManagerState::GetCurrentConfigName()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(currentConfig_mutex);
|
||||
return currentConfig;
|
||||
}
|
||||
|
||||
// Sets the activated target application in app-specific shortcut
|
||||
void KeyboardManagerState::SetActivatedApp(const std::wstring& appName)
|
||||
{
|
||||
activatedAppSpecificShortcutTarget = appName;
|
||||
}
|
||||
|
||||
// Gets the activated target application in app-specific shortcut
|
||||
std::wstring KeyboardManagerState::GetActivatedApp()
|
||||
{
|
||||
return activatedAppSpecificShortcutTarget;
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
#pragma once
|
||||
#include <mutex>
|
||||
#include "KeyboardManagerConstants.h"
|
||||
#include <common/interop/keyboard_layout.h>
|
||||
#include "../common/hooks/LowlevelKeyboardEvent.h"
|
||||
#include <functional>
|
||||
#include <variant>
|
||||
#include "Shortcut.h"
|
||||
#include "RemapShortcut.h"
|
||||
|
||||
class KeyDelay;
|
||||
|
||||
namespace KeyboardManagerHelper
|
||||
{
|
||||
enum class KeyboardHookDecision;
|
||||
}
|
||||
|
||||
namespace winrt::Windows::UI::Xaml::Controls
|
||||
{
|
||||
struct StackPanel;
|
||||
}
|
||||
|
||||
using SingleKeyRemapTable = std::unordered_map<DWORD, KeyShortcutUnion>;
|
||||
using ShortcutRemapTable = std::map<Shortcut, RemapShortcut>;
|
||||
using AppSpecificShortcutRemapTable = std::map<std::wstring, ShortcutRemapTable>;
|
||||
|
||||
// Enum type to store different states of the UI
|
||||
enum class KeyboardManagerUIState
|
||||
{
|
||||
// If set to this value then there is no keyboard manager window currently active that requires a hook
|
||||
Deactivated,
|
||||
// If set to this value then the detect key window is currently active and it requires a hook
|
||||
DetectSingleKeyRemapWindowActivated,
|
||||
// If set to this value then the detect shortcut window in edit keyboard window is currently active and it requires a hook
|
||||
DetectShortcutWindowInEditKeyboardWindowActivated,
|
||||
// If set to this value then the edit keyboard window is currently active and remaps should not be applied
|
||||
EditKeyboardWindowActivated,
|
||||
// If set to this value then the detect shortcut window is currently active and it requires a hook
|
||||
DetectShortcutWindowActivated,
|
||||
// If set to this value then the edit shortcuts window is currently active and remaps should not be applied
|
||||
EditShortcutsWindowActivated
|
||||
};
|
||||
|
||||
// Class to store the shared state of the keyboard manager between the UI and the hook
|
||||
class KeyboardManagerState
|
||||
{
|
||||
private:
|
||||
// State variable used to store which UI window is currently active that requires interaction with the hook
|
||||
KeyboardManagerUIState uiState;
|
||||
std::mutex uiState_mutex;
|
||||
|
||||
// Window handle for the current UI window which is active. Should be set to nullptr if UI is deactivated
|
||||
HWND currentUIWindow;
|
||||
std::mutex currentUIWindow_mutex;
|
||||
|
||||
// Object to store the shortcut detected in the detect shortcut UI window. Gets cleared on releasing keys. This is used in both the backend and the UI.
|
||||
Shortcut detectedShortcut;
|
||||
std::mutex detectedShortcut_mutex;
|
||||
|
||||
// Object to store the shortcut state displayed in the UI window. Always stores last displayed shortcut irrespective of releasing keys. This is used in both the backend and the UI.
|
||||
Shortcut currentShortcut;
|
||||
std::mutex currentShortcut_mutex;
|
||||
|
||||
// Store detected remap key in the remap UI window. This is used in both the backend and the UI.
|
||||
DWORD detectedRemapKey;
|
||||
std::mutex detectedRemapKey_mutex;
|
||||
|
||||
// Stores the UI element which is to be updated based on the remap key entered.
|
||||
winrt::Windows::Foundation::IInspectable currentSingleKeyUI;
|
||||
std::mutex currentSingleKeyUI_mutex;
|
||||
|
||||
// Stores the UI element which is to be updated based on the shortcut entered (each stackpanel represents a row of keys)
|
||||
winrt::Windows::Foundation::IInspectable currentShortcutUI1;
|
||||
winrt::Windows::Foundation::IInspectable currentShortcutUI2;
|
||||
std::mutex currentShortcutUI_mutex;
|
||||
|
||||
// Stores the current configuration name.
|
||||
std::wstring currentConfig = KeyboardManagerConstants::DefaultConfiguration;
|
||||
std::mutex currentConfig_mutex;
|
||||
|
||||
// Registered KeyDelay objects, used to notify delayed key events.
|
||||
std::map<DWORD, std::unique_ptr<KeyDelay>> keyDelays;
|
||||
std::mutex keyDelays_mutex;
|
||||
|
||||
// Stores the activated target application in app-specific shortcut
|
||||
std::wstring activatedAppSpecificShortcutTarget;
|
||||
|
||||
// Thread safe boolean value to check if remappings are currently enabled. This is used to disable remappings while the remap tables are being updated by the UI thread
|
||||
std::atomic_bool remappingsEnabled;
|
||||
|
||||
// Display a key by appending a border Control as a child of the panel.
|
||||
void AddKeyToLayout(const winrt::Windows::UI::Xaml::Controls::StackPanel& panel, const winrt::hstring& key);
|
||||
|
||||
public:
|
||||
// The map members and their mutexes are left as public since the maps are used extensively in dllmain.cpp.
|
||||
// Maps which store the remappings for each of the features. The bool fields should be initialized to false. They are used to check the current state of the shortcut (i.e is that particular shortcut currently pressed down or not).
|
||||
// Stores single key remappings
|
||||
std::unordered_map<DWORD, KeyShortcutUnion> singleKeyReMap;
|
||||
|
||||
/* This feature has not been enabled (code from proof of concept stage)
|
||||
*
|
||||
// Stores keys which need to be changed from toggle behavior to modifier behavior. Eg. Caps Lock
|
||||
std::unordered_map<DWORD, bool> singleKeyToggleToMod;
|
||||
*/
|
||||
|
||||
// Stores the os level shortcut remappings
|
||||
ShortcutRemapTable osLevelShortcutReMap;
|
||||
std::vector<Shortcut> osLevelShortcutReMapSortedKeys;
|
||||
|
||||
// Stores the app-specific shortcut remappings. Maps application name to the shortcut map
|
||||
AppSpecificShortcutRemapTable appSpecificShortcutReMap;
|
||||
std::map<std::wstring, std::vector<Shortcut>> appSpecificShortcutReMapSortedKeys;
|
||||
|
||||
// Stores the keyboard layout
|
||||
LayoutMap keyboardMap;
|
||||
|
||||
// Constructor
|
||||
KeyboardManagerState();
|
||||
|
||||
// Destructor
|
||||
~KeyboardManagerState();
|
||||
|
||||
// Function to reset the UI state members
|
||||
void ResetUIState();
|
||||
|
||||
// Function to check the if the UI state matches the argument state. For states with detect windows it also checks if the window is in focus.
|
||||
bool CheckUIState(KeyboardManagerUIState state);
|
||||
|
||||
// Function to set the window handle of the current UI window that is activated
|
||||
void SetCurrentUIWindow(HWND windowHandle);
|
||||
|
||||
// Function to set the UI state. When a window is activated, the handle to the window can be passed in the windowHandle argument.
|
||||
void SetUIState(KeyboardManagerUIState state, HWND windowHandle = nullptr);
|
||||
|
||||
// Function to clear the OS Level shortcut remapping table
|
||||
void ClearOSLevelShortcuts();
|
||||
|
||||
// Function to clear the Keys remapping table
|
||||
void ClearSingleKeyRemaps();
|
||||
|
||||
// Function to clear the App specific shortcut remapping table
|
||||
void ClearAppSpecificShortcuts();
|
||||
|
||||
// Function to add a new single key to key remapping
|
||||
bool AddSingleKeyRemap(const DWORD& originalKey, const KeyShortcutUnion& newRemapKey);
|
||||
|
||||
// Function to add a new OS level shortcut remapping
|
||||
bool AddOSLevelShortcut(const Shortcut& originalSC, const KeyShortcutUnion& newSC);
|
||||
|
||||
// Function to add a new App specific level shortcut remapping
|
||||
bool AddAppSpecificShortcut(const std::wstring& app, const Shortcut& originalSC, const KeyShortcutUnion& newSC);
|
||||
|
||||
// Function to get the iterator of a single key remap given the source key. Returns nullopt if it isn't remapped
|
||||
std::optional<SingleKeyRemapTable::iterator> GetSingleKeyRemap(const DWORD& originalKey);
|
||||
|
||||
bool CheckShortcutRemapInvoked(const std::optional<std::wstring>& appName);
|
||||
|
||||
std::vector<Shortcut>& GetSortedShortcutRemapVector(const std::optional<std::wstring>& appName);
|
||||
|
||||
// Function to get the source and target of a shortcut remap given the source shortcut. Returns nullopt if it isn't remapped
|
||||
ShortcutRemapTable& GetShortcutRemapTable(const std::optional<std::wstring>& appName);
|
||||
|
||||
// Function to set the textblock of the detect shortcut UI so that it can be accessed by the hook
|
||||
void ConfigureDetectShortcutUI(const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock1, const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock2);
|
||||
|
||||
// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
|
||||
void ConfigureDetectSingleKeyRemapUI(const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock);
|
||||
|
||||
// Function to update the detect shortcut UI based on the entered keys
|
||||
void UpdateDetectShortcutUI();
|
||||
|
||||
// Function to update the detect remap key UI based on the entered key.
|
||||
void UpdateDetectSingleKeyRemapUI();
|
||||
|
||||
// Function to return the currently detected shortcut which is displayed on the UI
|
||||
Shortcut GetDetectedShortcut();
|
||||
|
||||
// Function to return the currently detected remap key which is displayed on the UI
|
||||
DWORD GetDetectedSingleRemapKey();
|
||||
|
||||
// Function which can be used in HandleKeyboardHookEvent before the single key remap event to use the UI and suppress events while the remap window is active.
|
||||
KeyboardManagerHelper::KeyboardHookDecision DetectSingleRemapKeyUIBackend(LowlevelKeyboardEvent* data);
|
||||
|
||||
// Function which can be used in HandleKeyboardHookEvent before the os level shortcut remap event to use the UI and suppress events while the remap window is active.
|
||||
KeyboardManagerHelper::KeyboardHookDecision DetectShortcutUIBackend(LowlevelKeyboardEvent* data, bool isRemapKey);
|
||||
|
||||
// Add a KeyDelay object to get delayed key presses events for a given virtual key
|
||||
// NOTE: this will throw an exception if a virtual key is registered twice.
|
||||
// NOTE*: the virtual key should represent the original, unmapped virtual key.
|
||||
void RegisterKeyDelay(
|
||||
DWORD key,
|
||||
std::function<void(DWORD)> onShortPress,
|
||||
std::function<void(DWORD)> onLongPressDetected,
|
||||
std::function<void(DWORD)> onLongPressReleased);
|
||||
|
||||
// Remove a KeyDelay.
|
||||
// NOTE: this method will throw if the virtual key is not registered beforehand.
|
||||
// NOTE*: the virtual key should represent the original, unmapped virtual key.
|
||||
void UnregisterKeyDelay(DWORD key);
|
||||
|
||||
// Function to clear all the registered key delays
|
||||
void ClearRegisteredKeyDelays();
|
||||
|
||||
// Handle a key event, for a delayed key.
|
||||
bool HandleKeyDelayEvent(LowlevelKeyboardEvent* ev);
|
||||
|
||||
// Update the currently selected single key remap
|
||||
void SelectDetectedRemapKey(DWORD key);
|
||||
|
||||
// Update the currently selected shortcut.
|
||||
void SelectDetectedShortcut(DWORD key);
|
||||
|
||||
// Reset the shortcut (backend) state after releasing a key.
|
||||
void ResetDetectedShortcutKey(DWORD key);
|
||||
|
||||
// Save the updated configuration.
|
||||
bool SaveConfigToFile();
|
||||
|
||||
// Sets the Current Active Configuration Name.
|
||||
void SetCurrentConfigName(const std::wstring& configName);
|
||||
|
||||
// Gets the Current Active Configuration Name.
|
||||
std::wstring GetCurrentConfigName();
|
||||
|
||||
// Sets the activated target application in app-specific shortcut
|
||||
void SetActivatedApp(const std::wstring& appName);
|
||||
|
||||
// Gets the activated target application in app-specific shortcut
|
||||
std::wstring GetActivatedApp();
|
||||
};
|
||||
393
src/modules/keyboardmanager/common/MappingConfiguration.cpp
Normal file
393
src/modules/keyboardmanager/common/MappingConfiguration.cpp
Normal file
@@ -0,0 +1,393 @@
|
||||
#include "pch.h"
|
||||
#include "MappingConfiguration.h"
|
||||
|
||||
#include <common/SettingsAPI/settings_objects.h>
|
||||
#include <common/SettingsAPI/settings_helpers.h>
|
||||
#include <common/logger/logger.h>
|
||||
|
||||
#include "KeyboardManagerConstants.h"
|
||||
#include "Shortcut.h"
|
||||
#include "RemapShortcut.h"
|
||||
#include "Helpers.h"
|
||||
|
||||
// Function to clear the OS Level shortcut remapping table
|
||||
void MappingConfiguration::ClearOSLevelShortcuts()
|
||||
{
|
||||
osLevelShortcutReMap.clear();
|
||||
osLevelShortcutReMapSortedKeys.clear();
|
||||
}
|
||||
|
||||
|
||||
// Function to clear the Keys remapping table.
|
||||
void MappingConfiguration::ClearSingleKeyRemaps()
|
||||
{
|
||||
singleKeyReMap.clear();
|
||||
}
|
||||
|
||||
// Function to clear the App specific shortcut remapping table
|
||||
void MappingConfiguration::ClearAppSpecificShortcuts()
|
||||
{
|
||||
appSpecificShortcutReMap.clear();
|
||||
appSpecificShortcutReMapSortedKeys.clear();
|
||||
}
|
||||
|
||||
// Function to add a new OS level shortcut remapping
|
||||
bool MappingConfiguration::AddOSLevelShortcut(const Shortcut& originalSC, const KeyShortcutUnion& newSC)
|
||||
{
|
||||
// Check if the shortcut is already remapped
|
||||
auto it = osLevelShortcutReMap.find(originalSC);
|
||||
if (it != osLevelShortcutReMap.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
osLevelShortcutReMap[originalSC] = RemapShortcut(newSC);
|
||||
osLevelShortcutReMapSortedKeys.push_back(originalSC);
|
||||
Helpers::SortShortcutVectorBasedOnSize(osLevelShortcutReMapSortedKeys);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to add a new single key to key/shortcut remapping
|
||||
bool MappingConfiguration::AddSingleKeyRemap(const DWORD& originalKey, const KeyShortcutUnion& newRemapKey)
|
||||
{
|
||||
// Check if the key is already remapped
|
||||
auto it = singleKeyReMap.find(originalKey);
|
||||
if (it != singleKeyReMap.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
singleKeyReMap[originalKey] = newRemapKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to add a new App specific shortcut remapping
|
||||
bool MappingConfiguration::AddAppSpecificShortcut(const std::wstring& app, const Shortcut& originalSC, const KeyShortcutUnion& newSC)
|
||||
{
|
||||
// Convert app name to lower case
|
||||
std::wstring process_name;
|
||||
process_name.resize(app.length());
|
||||
std::transform(app.begin(), app.end(), process_name.begin(), towlower);
|
||||
|
||||
// Check if there are any app specific shortcuts for this app
|
||||
auto appIt = appSpecificShortcutReMap.find(process_name);
|
||||
if (appIt != appSpecificShortcutReMap.end())
|
||||
{
|
||||
// Check if the shortcut is already remapped
|
||||
auto shortcutIt = appSpecificShortcutReMap[process_name].find(originalSC);
|
||||
if (shortcutIt != appSpecificShortcutReMap[process_name].end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
appSpecificShortcutReMapSortedKeys[process_name] = std::vector<Shortcut>();
|
||||
}
|
||||
|
||||
appSpecificShortcutReMap[process_name][originalSC] = RemapShortcut(newSC);
|
||||
appSpecificShortcutReMapSortedKeys[process_name].push_back(originalSC);
|
||||
Helpers::SortShortcutVectorBasedOnSize(appSpecificShortcutReMapSortedKeys[process_name]);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool MappingConfiguration::LoadSingleKeyRemaps(const json::JsonObject& jsonData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto remapKeysData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapKeysSettingName);
|
||||
ClearSingleKeyRemaps();
|
||||
|
||||
if (remapKeysData)
|
||||
{
|
||||
auto inProcessRemapKeys = remapKeysData.GetNamedArray(KeyboardManagerConstants::InProcessRemapKeysSettingName);
|
||||
for (const auto& it : inProcessRemapKeys)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKey).find(L";") != std::string::npos)
|
||||
{
|
||||
AddSingleKeyRemap(std::stoul(originalKey.c_str()), Shortcut(newRemapKey.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
AddSingleKeyRemap(std::stoul(originalKey.c_str()), std::stoul(newRemapKey.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next remap.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for single key remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool MappingConfiguration::LoadAppSpecificShortcutRemaps(const json::JsonObject& remapShortcutsData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto appSpecificRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::AppSpecificRemapShortcutsSettingName);
|
||||
for (const auto& it : appSpecificRemapShortcuts)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
auto targetApp = it.GetObjectW().GetNamedString(KeyboardManagerConstants::TargetAppSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||||
{
|
||||
AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool MappingConfiguration::LoadShortcutRemaps(const json::JsonObject& jsonData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto remapShortcutsData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapShortcutsSettingName);
|
||||
ClearOSLevelShortcuts();
|
||||
ClearAppSpecificShortcuts();
|
||||
if (remapShortcutsData)
|
||||
{
|
||||
// Load os level shortcut remaps
|
||||
try
|
||||
{
|
||||
auto globalRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::GlobalRemapShortcutsSettingName);
|
||||
for (const auto& it : globalRemapShortcuts)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||||
{
|
||||
AddOSLevelShortcut(Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
AddOSLevelShortcut(Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
// Load app specific shortcut remaps
|
||||
result = result && LoadAppSpecificShortcutRemaps(remapShortcutsData);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
MappingConfiguration::MappingConfiguration()
|
||||
{
|
||||
}
|
||||
|
||||
bool MappingConfiguration::LoadSettings()
|
||||
{
|
||||
Logger::trace(L"SettingsHelper::LoadSettings()");
|
||||
try
|
||||
{
|
||||
PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::load_from_settings_file(KeyboardManagerConstants::ModuleName);
|
||||
auto current_config = settings.get_string_value(KeyboardManagerConstants::ActiveConfigurationSettingName);
|
||||
|
||||
if (!current_config)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
currentConfig = *current_config;
|
||||
|
||||
// Read the config file and load the remaps.
|
||||
auto configFile = json::from_file(PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName) + L"\\" + *current_config + L".json");
|
||||
if (!configFile)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = LoadSingleKeyRemaps(*configFile);
|
||||
result = result && LoadShortcutRemaps(*configFile);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"SettingsHelper::LoadSettings() failed");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Save the updated configuration.
|
||||
bool MappingConfiguration::SaveSettingsToFile()
|
||||
{
|
||||
bool result = true;
|
||||
json::JsonObject configJson;
|
||||
json::JsonObject remapShortcuts;
|
||||
json::JsonObject remapKeys;
|
||||
json::JsonArray inProcessRemapKeysArray;
|
||||
json::JsonArray appSpecificRemapShortcutsArray;
|
||||
json::JsonArray globalRemapShortcutsArray;
|
||||
for (const auto& it : singleKeyReMap)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(winrt::to_hstring((unsigned int)it.first)));
|
||||
|
||||
// For key to key remapping
|
||||
if (it.second.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(it.second))));
|
||||
}
|
||||
|
||||
// For key to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(it.second).ToHstringVK()));
|
||||
}
|
||||
|
||||
inProcessRemapKeysArray.Append(keys);
|
||||
}
|
||||
|
||||
for (const auto& it : osLevelShortcutReMap)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(it.first.ToHstringVK()));
|
||||
|
||||
// For shortcut to key remapping
|
||||
if (it.second.targetShortcut.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(it.second.targetShortcut))));
|
||||
}
|
||||
|
||||
// For shortcut to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(it.second.targetShortcut).ToHstringVK()));
|
||||
}
|
||||
|
||||
globalRemapShortcutsArray.Append(keys);
|
||||
}
|
||||
|
||||
for (const auto& itApp : appSpecificShortcutReMap)
|
||||
{
|
||||
// Iterate over apps
|
||||
for (const auto& itKeys : itApp.second)
|
||||
{
|
||||
json::JsonObject keys;
|
||||
keys.SetNamedValue(KeyboardManagerConstants::OriginalKeysSettingName, json::value(itKeys.first.ToHstringVK()));
|
||||
|
||||
// For shortcut to key remapping
|
||||
if (itKeys.second.targetShortcut.index() == 0)
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(winrt::to_hstring((unsigned int)std::get<DWORD>(itKeys.second.targetShortcut))));
|
||||
}
|
||||
|
||||
// For shortcut to shortcut remapping
|
||||
else
|
||||
{
|
||||
keys.SetNamedValue(KeyboardManagerConstants::NewRemapKeysSettingName, json::value(std::get<Shortcut>(itKeys.second.targetShortcut).ToHstringVK()));
|
||||
}
|
||||
|
||||
keys.SetNamedValue(KeyboardManagerConstants::TargetAppSettingName, json::value(itApp.first));
|
||||
|
||||
appSpecificRemapShortcutsArray.Append(keys);
|
||||
}
|
||||
}
|
||||
|
||||
remapShortcuts.SetNamedValue(KeyboardManagerConstants::GlobalRemapShortcutsSettingName, globalRemapShortcutsArray);
|
||||
remapShortcuts.SetNamedValue(KeyboardManagerConstants::AppSpecificRemapShortcutsSettingName, appSpecificRemapShortcutsArray);
|
||||
remapKeys.SetNamedValue(KeyboardManagerConstants::InProcessRemapKeysSettingName, inProcessRemapKeysArray);
|
||||
configJson.SetNamedValue(KeyboardManagerConstants::RemapKeysSettingName, remapKeys);
|
||||
configJson.SetNamedValue(KeyboardManagerConstants::RemapShortcutsSettingName, remapShortcuts);
|
||||
|
||||
try
|
||||
{
|
||||
json::to_file((PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName) + L"\\" + currentConfig + L".json"), configJson);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
result = false;
|
||||
Logger::error(L"Failed to save the settings");
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
auto hEvent = CreateEvent(nullptr, false, false, KeyboardManagerConstants::SettingsEventName.c_str());
|
||||
if (hEvent)
|
||||
{
|
||||
SetEvent(hEvent);
|
||||
Logger::trace(L"Signaled {} event", KeyboardManagerConstants::SettingsEventName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::error(L"Failed to signal {} event", KeyboardManagerConstants::SettingsEventName);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
65
src/modules/keyboardmanager/common/MappingConfiguration.h
Normal file
65
src/modules/keyboardmanager/common/MappingConfiguration.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/utils/json.h>
|
||||
|
||||
#include <keyboardmanager/common/KeyboardManagerConstants.h>
|
||||
#include <keyboardmanager/common/Shortcut.h>
|
||||
#include <keyboardmanager/common/RemapShortcut.h>
|
||||
|
||||
using SingleKeyRemapTable = std::unordered_map<DWORD, KeyShortcutUnion>;
|
||||
using ShortcutRemapTable = std::map<Shortcut, RemapShortcut>;
|
||||
using AppSpecificShortcutRemapTable = std::map<std::wstring, ShortcutRemapTable>;
|
||||
|
||||
class MappingConfiguration
|
||||
{
|
||||
public:
|
||||
MappingConfiguration();
|
||||
|
||||
~MappingConfiguration() = default;
|
||||
|
||||
// Load the configuration.
|
||||
bool LoadSettings();
|
||||
|
||||
// Save the updated configuration.
|
||||
bool SaveSettingsToFile();
|
||||
|
||||
// Function to clear the OS Level shortcut remapping table
|
||||
void ClearOSLevelShortcuts();
|
||||
|
||||
// Function to clear the Keys remapping table
|
||||
void ClearSingleKeyRemaps();
|
||||
|
||||
// Function to clear the App specific shortcut remapping table
|
||||
void ClearAppSpecificShortcuts();
|
||||
|
||||
// Function to add a new single key to key remapping
|
||||
bool AddSingleKeyRemap(const DWORD& originalKey, const KeyShortcutUnion& newRemapKey);
|
||||
|
||||
// Function to add a new OS level shortcut remapping
|
||||
bool AddOSLevelShortcut(const Shortcut& originalSC, const KeyShortcutUnion& newSC);
|
||||
|
||||
// Function to add a new App specific level shortcut remapping
|
||||
bool AddAppSpecificShortcut(const std::wstring& app, const Shortcut& originalSC, const KeyShortcutUnion& newSC);
|
||||
|
||||
// The map members and their mutexes are left as public since the maps are used extensively in dllmain.cpp.
|
||||
// Maps which store the remappings for each of the features. The bool fields should be initialized to false. They are used to check the current state of the shortcut (i.e is that particular shortcut currently pressed down or not).
|
||||
// Stores single key remappings
|
||||
std::unordered_map<DWORD, KeyShortcutUnion> singleKeyReMap;
|
||||
|
||||
// Stores the os level shortcut remappings
|
||||
ShortcutRemapTable osLevelShortcutReMap;
|
||||
std::vector<Shortcut> osLevelShortcutReMapSortedKeys;
|
||||
|
||||
// Stores the app-specific shortcut remappings. Maps application name to the shortcut map
|
||||
AppSpecificShortcutRemapTable appSpecificShortcutReMap;
|
||||
std::map<std::wstring, std::vector<Shortcut>> appSpecificShortcutReMapSortedKeys;
|
||||
|
||||
// Stores the current configuration name.
|
||||
std::wstring currentConfig = KeyboardManagerConstants::DefaultConfiguration;
|
||||
|
||||
|
||||
private:
|
||||
bool LoadSingleKeyRemaps(const json::JsonObject& jsonData);
|
||||
bool LoadShortcutRemaps(const json::JsonObject& jsonData);
|
||||
bool LoadAppSpecificShortcutRemaps(const json::JsonObject& remapShortcutsData);
|
||||
};
|
||||
@@ -1,194 +0,0 @@
|
||||
#include "pch.h"
|
||||
#include "SettingsHelper.h"
|
||||
|
||||
#include <common/SettingsAPI/settings_objects.h>
|
||||
#include <common/SettingsAPI/settings_helpers.h>
|
||||
#include <common/logger/logger.h>
|
||||
|
||||
#include <common/KeyboardManagerConstants.h>
|
||||
|
||||
bool LoadSingleKeyRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& jsonData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto remapKeysData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapKeysSettingName);
|
||||
keyboardManagerState.ClearSingleKeyRemaps();
|
||||
|
||||
if (remapKeysData)
|
||||
{
|
||||
auto inProcessRemapKeys = remapKeysData.GetNamedArray(KeyboardManagerConstants::InProcessRemapKeysSettingName);
|
||||
for (const auto& it : inProcessRemapKeys)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKey).find(L";") != std::string::npos)
|
||||
{
|
||||
keyboardManagerState.AddSingleKeyRemap(std::stoul(originalKey.c_str()), Shortcut(newRemapKey.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
keyboardManagerState.AddSingleKeyRemap(std::stoul(originalKey.c_str()), std::stoul(newRemapKey.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next remap.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for single key remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LoadAppSpecificShortcutRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& remapShortcutsData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto appSpecificRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::AppSpecificRemapShortcutsSettingName);
|
||||
for (const auto& it : appSpecificRemapShortcuts)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
auto targetApp = it.GetObjectW().GetNamedString(KeyboardManagerConstants::TargetAppSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||||
{
|
||||
keyboardManagerState.AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
keyboardManagerState.AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LoadShortcutRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& jsonData)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
auto remapShortcutsData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapShortcutsSettingName);
|
||||
keyboardManagerState.ClearOSLevelShortcuts();
|
||||
keyboardManagerState.ClearAppSpecificShortcuts();
|
||||
if (remapShortcutsData)
|
||||
{
|
||||
// Load os level shortcut remaps
|
||||
try
|
||||
{
|
||||
auto globalRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::GlobalRemapShortcutsSettingName);
|
||||
for (const auto& it : globalRemapShortcuts)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||||
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||||
|
||||
// If remapped to a shortcut
|
||||
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||||
{
|
||||
keyboardManagerState.AddOSLevelShortcut(Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||||
}
|
||||
|
||||
// If remapped to a key
|
||||
else
|
||||
{
|
||||
keyboardManagerState.AddOSLevelShortcut(Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
// Load app specific shortcut remaps
|
||||
result = result && LoadAppSpecificShortcutRemaps(keyboardManagerState, remapShortcutsData);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"Improper JSON format for shortcut remaps. Skip to next remap type");
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SettingsHelper::LoadSettings(KeyboardManagerState& keyboardManagerState)
|
||||
{
|
||||
Logger::trace(L"SettingsHelper::LoadSettings()");
|
||||
try
|
||||
{
|
||||
PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::load_from_settings_file(KeyboardManagerConstants::ModuleName);
|
||||
auto current_config = settings.get_string_value(KeyboardManagerConstants::ActiveConfigurationSettingName);
|
||||
|
||||
if (!current_config)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
keyboardManagerState.SetCurrentConfigName(*current_config);
|
||||
|
||||
// Read the config file and load the remaps.
|
||||
auto configFile = json::from_file(PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName) + L"\\" + *current_config + L".json");
|
||||
if (!configFile)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = LoadSingleKeyRemaps(keyboardManagerState, *configFile);
|
||||
result = result && LoadShortcutRemaps(keyboardManagerState, *configFile);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Logger::error(L"SettingsHelper::LoadSettings() failed");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <keyboardmanager/common/KeyboardManagerState.h>
|
||||
|
||||
class SettingsHelper
|
||||
{
|
||||
public:
|
||||
static bool LoadSettings(KeyboardManagerState& state);
|
||||
};
|
||||
@@ -2,15 +2,29 @@
|
||||
#include "Shortcut.h"
|
||||
#include <common/interop/keyboard_layout.h>
|
||||
#include <common/interop/shared_constants.h>
|
||||
#include "ErrorTypes.h"
|
||||
#include "Helpers.h"
|
||||
#include "InputInterface.h"
|
||||
|
||||
|
||||
// Function to split a wstring based on a delimiter and return a vector of split strings
|
||||
std::vector<std::wstring> Shortcut::splitwstring(const std::wstring& input, wchar_t delimiter)
|
||||
{
|
||||
std::wstringstream ss(input);
|
||||
std::wstring item;
|
||||
std::vector<std::wstring> splittedStrings;
|
||||
while (std::getline(ss, item, delimiter))
|
||||
{
|
||||
splittedStrings.push_back(item);
|
||||
}
|
||||
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
// Constructor to initialize Shortcut from it's virtual key code string representation.
|
||||
Shortcut::Shortcut(const std::wstring& shortcutVK) :
|
||||
winKey(ModifierKey::Disabled), ctrlKey(ModifierKey::Disabled), altKey(ModifierKey::Disabled), shiftKey(ModifierKey::Disabled), actionKey(NULL)
|
||||
{
|
||||
auto keys = KeyboardManagerHelper::splitwstring(shortcutVK, ';');
|
||||
auto keys = splitwstring(shortcutVK, ';');
|
||||
for (auto it : keys)
|
||||
{
|
||||
auto vkKeyCode = std::stoul(it);
|
||||
@@ -75,20 +89,6 @@ void Shortcut::Reset()
|
||||
actionKey = NULL;
|
||||
}
|
||||
|
||||
// Function to return true if the shortcut is valid. A valid shortcut has atleast one modifier, as well as an action key
|
||||
bool Shortcut::IsValidShortcut() const
|
||||
{
|
||||
if (actionKey != NULL)
|
||||
{
|
||||
if (winKey != ModifierKey::Disabled || ctrlKey != ModifierKey::Disabled || altKey != ModifierKey::Disabled || shiftKey != ModifierKey::Disabled)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to return the action key
|
||||
DWORD Shortcut::GetActionKey() const
|
||||
{
|
||||
@@ -418,33 +418,6 @@ void Shortcut::ResetKey(const DWORD& input)
|
||||
}
|
||||
}
|
||||
|
||||
// Function to return a vector of hstring for each key in the display order
|
||||
std::vector<winrt::hstring> Shortcut::GetKeyVector(LayoutMap& keyboardMap) const
|
||||
{
|
||||
std::vector<winrt::hstring> keys;
|
||||
if (winKey != ModifierKey::Disabled)
|
||||
{
|
||||
keys.push_back(winrt::to_hstring(keyboardMap.GetKeyName(GetWinKey(ModifierKey::Both)).c_str()));
|
||||
}
|
||||
if (ctrlKey != ModifierKey::Disabled)
|
||||
{
|
||||
keys.push_back(winrt::to_hstring(keyboardMap.GetKeyName(GetCtrlKey()).c_str()));
|
||||
}
|
||||
if (altKey != ModifierKey::Disabled)
|
||||
{
|
||||
keys.push_back(winrt::to_hstring(keyboardMap.GetKeyName(GetAltKey()).c_str()));
|
||||
}
|
||||
if (shiftKey != ModifierKey::Disabled)
|
||||
{
|
||||
keys.push_back(winrt::to_hstring(keyboardMap.GetKeyName(GetShiftKey()).c_str()));
|
||||
}
|
||||
if (actionKey != NULL)
|
||||
{
|
||||
keys.push_back(winrt::to_hstring(keyboardMap.GetKeyName(actionKey).c_str()));
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
// Function to return the string representation of the shortcut in virtual key codes appended in a string by ";" separator.
|
||||
winrt::hstring Shortcut::ToHstringVK() const
|
||||
{
|
||||
@@ -842,55 +815,3 @@ int Shortcut::GetCommonModifiersCount(const Shortcut& input) const
|
||||
|
||||
return commonElements;
|
||||
}
|
||||
|
||||
// Function to check if the two shortcuts are equal or cover the same set of keys. Return value depends on type of overlap
|
||||
KeyboardManagerHelper::ErrorType Shortcut::DoKeysOverlap(const Shortcut& first, const Shortcut& second)
|
||||
{
|
||||
if (first.IsValidShortcut() && second.IsValidShortcut())
|
||||
{
|
||||
// If the shortcuts are equal
|
||||
if (first == second)
|
||||
{
|
||||
return KeyboardManagerHelper::ErrorType::SameShortcutPreviouslyMapped;
|
||||
}
|
||||
// action keys match
|
||||
else if (first.actionKey == second.actionKey)
|
||||
{
|
||||
// corresponding modifiers are either both disabled or both not disabled - this ensures that both match in types of modifiers i.e. Ctrl(l/r/c) Shift (l/r/c) A matches Ctrl(l/r/c) Shift (l/r/c) A
|
||||
if (((first.winKey != ModifierKey::Disabled && second.winKey != ModifierKey::Disabled) || (first.winKey == ModifierKey::Disabled && second.winKey == ModifierKey::Disabled)) &&
|
||||
((first.ctrlKey != ModifierKey::Disabled && second.ctrlKey != ModifierKey::Disabled) || (first.ctrlKey == ModifierKey::Disabled && second.ctrlKey == ModifierKey::Disabled)) &&
|
||||
((first.altKey != ModifierKey::Disabled && second.altKey != ModifierKey::Disabled) || (first.altKey == ModifierKey::Disabled && second.altKey == ModifierKey::Disabled)) &&
|
||||
((first.shiftKey != ModifierKey::Disabled && second.shiftKey != ModifierKey::Disabled) || (first.shiftKey == ModifierKey::Disabled && second.shiftKey == ModifierKey::Disabled)))
|
||||
{
|
||||
// If one of the modifier is common
|
||||
if ((first.winKey == ModifierKey::Both || second.winKey == ModifierKey::Both) ||
|
||||
(first.ctrlKey == ModifierKey::Both || second.ctrlKey == ModifierKey::Both) ||
|
||||
(first.altKey == ModifierKey::Both || second.altKey == ModifierKey::Both) ||
|
||||
(first.shiftKey == ModifierKey::Both || second.shiftKey == ModifierKey::Both))
|
||||
{
|
||||
return KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return KeyboardManagerHelper::ErrorType::NoError;
|
||||
}
|
||||
|
||||
// Function to check if the shortcut is illegal (i.e. Win+L or Ctrl+Alt+Del)
|
||||
KeyboardManagerHelper::ErrorType Shortcut::IsShortcutIllegal() const
|
||||
{
|
||||
// Win+L
|
||||
if (winKey != ModifierKey::Disabled && ctrlKey == ModifierKey::Disabled && altKey == ModifierKey::Disabled && shiftKey == ModifierKey::Disabled && actionKey == 0x4C)
|
||||
{
|
||||
return KeyboardManagerHelper::ErrorType::WinL;
|
||||
}
|
||||
|
||||
// Ctrl+Alt+Del
|
||||
if (winKey == ModifierKey::Disabled && ctrlKey != ModifierKey::Disabled && altKey != ModifierKey::Disabled && shiftKey == ModifierKey::Disabled && actionKey == VK_DELETE)
|
||||
{
|
||||
return KeyboardManagerHelper::ErrorType::CtrlAltDel;
|
||||
}
|
||||
|
||||
return KeyboardManagerHelper::ErrorType::NoError;
|
||||
}
|
||||
|
||||
@@ -7,21 +7,20 @@ namespace KeyboardManagerInput
|
||||
class InputInterface;
|
||||
}
|
||||
class LayoutMap;
|
||||
namespace KeyboardManagerHelper
|
||||
{
|
||||
enum class ErrorType;
|
||||
}
|
||||
|
||||
class Shortcut
|
||||
{
|
||||
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;
|
||||
|
||||
public:
|
||||
// By default create an empty shortcut
|
||||
Shortcut() :
|
||||
winKey(ModifierKey::Disabled), ctrlKey(ModifierKey::Disabled), altKey(ModifierKey::Disabled), shiftKey(ModifierKey::Disabled), actionKey(NULL)
|
||||
@@ -111,9 +110,6 @@ public:
|
||||
// Function to reset all the keys in the shortcut
|
||||
void Reset();
|
||||
|
||||
// Function to return true if the shortcut is valid. A valid shortcut has atleast one modifier, as well as an action key
|
||||
bool IsValidShortcut() const;
|
||||
|
||||
// Function to return the action key
|
||||
DWORD GetActionKey() const;
|
||||
|
||||
@@ -150,9 +146,6 @@ public:
|
||||
// Function to return the string representation of the shortcut in virtual key codes appended in a string by ";" separator.
|
||||
winrt::hstring ToHstringVK() const;
|
||||
|
||||
// Function to return a vector of hstring for each key in the display order
|
||||
std::vector<winrt::hstring> GetKeyVector(LayoutMap& keyboardMap) const;
|
||||
|
||||
// Function to return a vector of key codes in the display order
|
||||
std::vector<DWORD> GetKeyCodes();
|
||||
|
||||
@@ -167,12 +160,6 @@ public:
|
||||
|
||||
// Function to get the number of modifiers that are common between the current shortcut and the shortcut in the argument
|
||||
int GetCommonModifiersCount(const Shortcut& input) const;
|
||||
|
||||
// Function to check if the two shortcuts are equal or cover the same set of keys. Return value depends on type of overlap
|
||||
static KeyboardManagerHelper::ErrorType DoKeysOverlap(const Shortcut& first, const Shortcut& second);
|
||||
|
||||
// Function to check if the shortcut is illegal (i.e. Win+L or Ctrl+Alt+Del)
|
||||
KeyboardManagerHelper::ErrorType IsShortcutIllegal() const;
|
||||
};
|
||||
|
||||
using KeyShortcutUnion = std::variant<DWORD, Shortcut>;
|
||||
|
||||
@@ -2,17 +2,8 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <winrt/base.h>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Graphics.h>
|
||||
#include <winrt/Windows.system.h>
|
||||
#include <winrt/Windows.Foundation.Collections.h>
|
||||
#include "winrt/Windows.Foundation.Numerics.h"
|
||||
#include <winrt/windows.ui.xaml.controls.h>
|
||||
#include "winrt/Windows.UI.Core.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <stdlib.h>
|
||||
#include <ProjectTelemetry.h>
|
||||
|
||||
using namespace winrt;
|
||||
using namespace Windows::Foundation::Numerics;
|
||||
using namespace Windows::UI::Xaml;
|
||||
using namespace Windows::UI::Xaml::Controls;
|
||||
#include <ProjectTelemetry.h>
|
||||
Reference in New Issue
Block a user