[Keyboard Manager] Enable App-specific shortcuts in the KBM backend (#4628)

* Enable app specific shortcut remapping

* Fixed lowercase function call

* Add test file

* Moved GetForegroundProcess to II and added tests

* Fixed runtime error while testing due to heap allocation across dll boundary

* Renamed function

* Remove unused code

* Changed process checking step to include substrings

* Changed back to exact match included match without file extension
This commit is contained in:
Arjun Balgovind
2020-07-06 16:45:53 -07:00
committed by GitHub
parent 70405045d7
commit 1533c9315f
15 changed files with 241 additions and 52 deletions

View File

@@ -596,18 +596,40 @@ namespace KeyboardEventHandlers
// Check if the key event was generated by KeyboardManager to avoid remapping events generated by us.
if (data->lParam->dwExtraInfo != KeyboardManagerConstants::KEYBOARDMANAGER_SHORTCUT_FLAG)
{
std::wstring process_name = KeyboardManagerHelper::GetCurrentApplication(false);
std::wstring process_name;
// Allocate MAX_PATH amount of memory
process_name.resize(MAX_PATH);
ii.GetForegroundProcess(process_name);
// Remove elements after null character
process_name.erase(std::find(process_name.begin(), process_name.end(), L'\0'), process_name.end());
if (process_name.empty())
{
return 0;
}
// Convert process name to lower case
std::transform(process_name.begin(), process_name.end(), process_name.begin(), towlower);
std::unique_lock<std::mutex> lock(keyboardManagerState.appSpecificShortcutReMap_mutex);
auto it = keyboardManagerState.appSpecificShortcutReMap.find(process_name);
std::wstring query_string = process_name;
auto it = keyboardManagerState.appSpecificShortcutReMap.find(query_string);
// If no entry is found, search for the process name without it's file extension
if (it == keyboardManagerState.appSpecificShortcutReMap.end())
{
// Find index of the file extension
size_t extensionIndex = process_name.find_last_of(L".");
query_string = process_name.substr(0, extensionIndex);
it = keyboardManagerState.appSpecificShortcutReMap.find(query_string);
}
if (it != keyboardManagerState.appSpecificShortcutReMap.end())
{
lock.unlock();
bool result = HandleShortcutRemapEvent(ii, data, keyboardManagerState.appSpecificShortcutReMap[process_name], keyboardManagerState.appSpecificShortcutReMap_mutex);
bool result = HandleShortcutRemapEvent(ii, data, keyboardManagerState.appSpecificShortcutReMap[query_string], keyboardManagerState.appSpecificShortcutReMap_mutex);
return result;
}
}