Skip to content

Commit

Permalink
Unregister key delays on closing KBM windows (#6583)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunbalgovind committed Sep 11, 2020
1 parent 1dec809 commit bfbd7b5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/modules/keyboardmanager/common/KeyboardManagerState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ void KeyboardManagerState::UpdateDetectShortcutUI()
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);
Expand Down Expand Up @@ -419,6 +418,7 @@ void KeyboardManagerState::RegisterKeyDelay(
{
throw std::invalid_argument("This key was already registered.");
}

keyDelays[key] = std::make_unique<KeyDelay>(key, onShortPress, onLongPressDetected, onLongPressReleased);
}

Expand All @@ -433,6 +433,13 @@ void KeyboardManagerState::UnregisterKeyDelay(DWORD key)
}
}

// 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())
Expand Down
3 changes: 3 additions & 0 deletions src/modules/keyboardmanager/common/KeyboardManagerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class KeyboardManagerState
// 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);

Expand Down
1 change: 1 addition & 0 deletions src/modules/keyboardmanager/ui/EditKeyboardWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ void createEditKeyboardWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMan
hwndLock.lock();
hwndEditKeyboardNativeWindow = nullptr;
keyboardManagerState.ResetUIState();
keyboardManagerState.ClearRegisteredKeyDelays();

// Cannot be done in WM_DESTROY because that causes crashes due to fatal app exit
xamlBridge.ClearXamlIslands();
Expand Down
1 change: 1 addition & 0 deletions src/modules/keyboardmanager/ui/EditShortcutsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
hwndLock.lock();
hwndEditShortcutsNativeWindow = nullptr;
keyboardManagerState.ResetUIState();
keyboardManagerState.ClearRegisteredKeyDelays();

// Cannot be done in WM_DESTROY because that causes crashes due to fatal app exit
xamlBridge.ClearXamlIslands();
Expand Down
5 changes: 1 addition & 4 deletions src/modules/keyboardmanager/ui/ShortcutControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ void ShortcutControl::createDetectShortcutWindow(winrt::Windows::Foundation::IIn
StackPanel linkedShortcutStackPanel = KeyboardManagerHelper::getSiblingElement(sender).as<StackPanel>();

auto unregisterKeys = [&keyboardManagerState]() {
std::thread t1(&KeyboardManagerState::UnregisterKeyDelay, &keyboardManagerState, VK_ESCAPE);
std::thread t2(&KeyboardManagerState::UnregisterKeyDelay, &keyboardManagerState, VK_RETURN);
t1.detach();
t2.detach();
keyboardManagerState.ClearRegisteredKeyDelays();
};

auto selectDetectedShortcutAndResetKeys = [&keyboardManagerState](DWORD key) {
Expand Down
5 changes: 1 addition & 4 deletions src/modules/keyboardmanager/ui/SingleKeyRemapControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ void SingleKeyRemapControl::createDetectKeyWindow(winrt::Windows::Foundation::II
ComboBox linkedRemapDropDown = KeyboardManagerHelper::getSiblingElement(sender).as<ComboBox>();

auto unregisterKeys = [&keyboardManagerState]() {
std::thread t1(&KeyboardManagerState::UnregisterKeyDelay, &keyboardManagerState, VK_ESCAPE);
std::thread t2(&KeyboardManagerState::UnregisterKeyDelay, &keyboardManagerState, VK_RETURN);
t1.detach();
t2.detach();
keyboardManagerState.ClearRegisteredKeyDelays();
};

auto onPressEnter = [linkedRemapDropDown,
Expand Down

0 comments on commit bfbd7b5

Please sign in to comment.