Skip to content

Commit

Permalink
[KBM]Added option for exact match shortcut (#36000)
Browse files Browse the repository at this point in the history
* Added option for exact match shortcut

* Fix spell-check
  • Loading branch information
mantaionut authored Jan 15, 2025
1 parent 3a10fac commit 162096c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/actions/spell-check/allow/code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ EXSEL
HOLDENTER
HOLDESC
HOLDSPACE
HOLDBACKSPACE
KBDLLHOOKSTRUCT
keyevent
LAlt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@
<data name="Type_HoldSpace" xml:space="preserve">
<value>Hold Space to toggle allow chord</value>
</data>
<data name="Type_HoldBackSpace" xml:space="preserve">
<value>Hold BackSpace to toggle exact match</value>
</data>
<data name="Type_HoldEnter" xml:space="preserve">
<value>Hold Enter to continue</value>
</data>
Expand All @@ -287,6 +290,9 @@
<data name="EditShortcuts_Allow_Chords" xml:space="preserve">
<value>Allow chords</value>
</data>
<data name="EditShortcuts_Exact_Match" xml:space="preserve">
<value>Exact Match</value>
</data>
<data name="EditShortcuts_Path_To_Program" xml:space="preserve">
<value>Path to program</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ class KeyDropDownControl

// Get number of selected keys. Do not count -1 and 0 values as they stand for Not selected and None
static int GetNumberOfSelectedKeys(std::vector<int32_t> keys);

bool exactMatch = false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ namespace KBMEditor
// flag to set if we want to allow building a chord
bool AllowChord = false;

bool exactMatch = false;

// Stores the keyboard layout
LayoutMap keyboardMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ ShortcutControl& ShortcutControl::AddNewShortcutControlRow(StackPanel& parent, s
{
// not sure why by we need to add the current item in here, so we have it even if does not change.
auto newShortcut = std::get<Shortcut>(newKeys);
shortcutRemapBuffer.push_back(RemapBufferRow{ RemapBufferItem{ Shortcut(), newShortcut }, std::wstring(targetAppName) });
shortcutRemapBuffer.push_back(RemapBufferRow{ RemapBufferItem{ originalKeys, newShortcut }, std::wstring(targetAppName) });
}
else
{
shortcutRemapBuffer.push_back(RemapBufferRow{ RemapBufferItem{ Shortcut(), Shortcut() }, std::wstring(targetAppName) });
shortcutRemapBuffer.push_back(RemapBufferRow{ RemapBufferItem{ originalKeys, newKeys }, std::wstring(targetAppName) });
}

KeyDropDownControl::AddShortcutToControl(originalKeys, parent, keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->shortcutDropDownVariableSizedWrapGrid.as<VariableSizedWrapGrid>(), *keyboardManagerState, 0, keyboardRemapControlObjects[keyboardRemapControlObjects.size() - 1][0]->keyDropDownControlObjects, shortcutRemapBuffer, row, targetAppTextBox, false, false);
Expand Down Expand Up @@ -956,11 +956,12 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
shortcut = std::get<Shortcut>(shortcutRemapBuffer[rowIndex].mapping[1]);
}

if (!shortcut.IsEmpty() && shortcut.HasChord())
keyboardManagerState.AllowChord = false;
keyboardManagerState.exactMatch = false;
if (!shortcut.IsEmpty())
{
keyboardManagerState.AllowChord = true;
} else {
keyboardManagerState.AllowChord = false;
keyboardManagerState.AllowChord = shortcut.HasChord();
keyboardManagerState.exactMatch = shortcut.exactMatch;
}
}

Expand All @@ -969,6 +970,7 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
// ContentDialog for detecting shortcuts. This is the parent UI element.
ContentDialog detectShortcutBox;
ToggleSwitch allowChordSwitch;
ToggleSwitch exactMatchSwitch;

// ContentDialog requires manually setting the XamlRoot (https://learn.microsoft.com/uwp/api/windows.ui.xaml.controls.contentdialog#contentdialog-in-appwindow-or-xaml-islands)
detectShortcutBox.XamlRoot(xamlRoot);
Expand Down Expand Up @@ -1038,6 +1040,11 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
allowChordSwitch.IsOn(keyboardManagerState.AllowChord);
};

auto onReleaseBack = [&keyboardManagerState,
exactMatchSwitch] {
keyboardManagerState.exactMatch = !keyboardManagerState.exactMatch;
exactMatchSwitch.IsOn(keyboardManagerState.exactMatch);
};
auto onAccept = [onPressEnter,
onReleaseEnter] {
onPressEnter();
Expand Down Expand Up @@ -1109,6 +1116,17 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
});
},
nullptr);
keyboardManagerState.RegisterKeyDelay(
VK_BACK,
selectDetectedShortcutAndResetKeys,
[onReleaseBack, detectShortcutBox](DWORD) {
detectShortcutBox.Dispatcher().RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Normal,
[onReleaseBack] {
onReleaseBack();
});
},
nullptr);
}

// Cancel button
Expand Down Expand Up @@ -1157,6 +1175,7 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn

// Detect Chord
Windows::UI::Xaml::Controls::StackPanel chordStackPanel;
Windows::UI::Xaml::Controls::StackPanel exactMatchStackPanel;

if (isOrigShortcut)
{
Expand Down Expand Up @@ -1189,6 +1208,32 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
};

allowChordSwitch.Toggled(toggleHandler);

TextBlock exactMatchText;
exactMatchText.Text(GET_RESOURCE_STRING(IDS_EDITSHORTCUTS_EXACT_MATCH));
exactMatchText.FontSize(12);
exactMatchText.Margin({ 0, 12 + verticalMargin, 0, 0 });
exactMatchText.TextAlignment(TextAlignment::Center);
exactMatchStackPanel.VerticalAlignment(VerticalAlignment::Center);
exactMatchStackPanel.Orientation(Orientation::Horizontal);

exactMatchSwitch.OnContent(nullptr);
exactMatchSwitch.OffContent(nullptr);
exactMatchSwitch.Margin({ 12, verticalMargin, 0, 0 });
exactMatchSwitch.IsOn(keyboardManagerState.exactMatch);

exactMatchStackPanel.Children().Append(exactMatchText);
exactMatchStackPanel.Children().Append(exactMatchSwitch);

stackPanel.Children().Append(exactMatchStackPanel);
exactMatchSwitch.IsOn(keyboardManagerState.exactMatch);

auto toggleHandlerExactMatch = [exactMatchSwitch, &keyboardManagerState, rowIndex, &remapBuffer](auto const& sender, auto const& e) {
Shortcut& shortcut = std::get<Shortcut>(remapBuffer[rowIndex].mapping[0]);
shortcut.exactMatch = exactMatchSwitch.IsOn();
};

exactMatchSwitch.Toggled(toggleHandlerExactMatch);
}

TextBlock holdEscInfo;
Expand All @@ -1211,6 +1256,12 @@ void ShortcutControl::CreateDetectShortcutWindow(winrt::Windows::Foundation::IIn
holdSpaceInfo.FontSize(12);
holdSpaceInfo.Margin({ 0, 0, 0, 0 });
stackPanel.Children().Append(holdSpaceInfo);

TextBlock holdCtrInfo;
holdCtrInfo.Text(GET_RESOURCE_STRING(IDS_TYPE_HOLDBACKSPACE));
holdCtrInfo.FontSize(12);
holdCtrInfo.Margin({ 0, 0, 0, 0 });
stackPanel.Children().Append(holdCtrInfo);
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ namespace KeyboardEventHandlers
// if not a mod key, check for chord stuff
if (!resetChordsResults.CurrentKeyIsModifierKey && (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN))
{
if (it->first.exactMatch == true && !it->first.IsKeyboardStateClearExceptShortcut(ii))
{
continue;
}

if (itShortcut.HasChord())
{
if (!resetChordsResults.AnyChordStarted && data->lParam->vkCode == itShortcut.GetActionKey() && !itShortcut.IsChordStarted() && itShortcut.HasChord())
Expand Down
6 changes: 3 additions & 3 deletions src/modules/keyboardmanager/common/KeyboardManagerConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ namespace KeyboardManagerConstants
// Name of the property use to store runProgramFilePath.
inline const std::wstring RunProgramFilePathSettingName = L"runProgramFilePath";

// Name of the property use to store secondKeyOfChord.
inline const std::wstring ShortcutSecondKeyOfChordSettingName = L"secondKeyOfChord";

// Name of the property use to store openUri.
inline const std::wstring ShortcutOpenURI = L"openUri";

// Name of the property use to store shortcutOperationType.
inline const std::wstring ShortcutOperationType = L"operationType";

// Name of the property use to store exactMatch.
inline const std::wstring ShortcutExactMatch = L"exactMatch";

// Name of the property use to store the target application.
inline const std::wstring TargetAppSettingName = L"targetApp";

Expand Down
Loading

0 comments on commit 162096c

Please sign in to comment.