diff --git a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Helpers/RemappingHelper.cs b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Helpers/RemappingHelper.cs new file mode 100644 index 0000000000..7af839971f --- /dev/null +++ b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Helpers/RemappingHelper.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using KeyboardManagerEditorUI.Interop; +using ManagedCommon; + +namespace KeyboardManagerEditorUI.Helpers +{ + public static class RemappingHelper + { + public static bool SaveMapping(KeyboardMappingService mappingService, List originalKeys, List remappedKeys, bool isAppSpecific, string appName) + { + if (mappingService == null) + { + Logger.LogError("Mapping service is null, cannot save mapping"); + return false; + } + + try + { + if (originalKeys == null || originalKeys.Count == 0 || remappedKeys == null || remappedKeys.Count == 0) + { + return false; + } + + if (originalKeys.Count == 1) + { + int originalKey = mappingService.GetKeyCodeFromName(originalKeys[0]); + if (originalKey != 0) + { + if (remappedKeys.Count == 1) + { + int targetKey = mappingService.GetKeyCodeFromName(remappedKeys[0]); + if (targetKey != 0) + { + mappingService.AddSingleKeyMapping(originalKey, targetKey); + } + } + else + { + string targetKeys = string.Join(";", remappedKeys.Select(k => mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture))); + mappingService.AddSingleKeyMapping(originalKey, targetKeys); + } + } + } + else + { + string originalKeysString = string.Join(";", originalKeys.Select(k => mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture))); + string targetKeysString = string.Join(";", remappedKeys.Select(k => mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture))); + + if (isAppSpecific && !string.IsNullOrEmpty(appName)) + { + mappingService.AddShortcutMapping(originalKeysString, targetKeysString, appName); + } + else + { + mappingService.AddShortcutMapping(originalKeysString, targetKeysString); + } + } + + mappingService.SaveSettings(); + return true; + } + catch (Exception ex) + { + Logger.LogError("Error saving mapping: " + ex.Message); + return false; + } + } + } +} diff --git a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardMappingService.cs b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardMappingService.cs index 5878c0b292..749df49764 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardMappingService.cs +++ b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardMappingService.cs @@ -131,6 +131,16 @@ namespace KeyboardManagerEditorUI.Interop return keyName.ToString(); } + public int GetKeyCodeFromName(string keyName) + { + if (string.IsNullOrEmpty(keyName)) + { + return 0; + } + + return KeyboardManagerInterop.GetKeyCodeFromName(keyName); + } + public bool AddSingleKeyMapping(int originalKey, int targetKey) { return KeyboardManagerInterop.AddSingleKeyRemap(_configHandle, originalKey, targetKey); diff --git a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Pages/Remappings.xaml.cs b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Pages/Remappings.xaml.cs index 23f5d1029e..19d74682fd 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Pages/Remappings.xaml.cs +++ b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Pages/Remappings.xaml.cs @@ -270,7 +270,7 @@ namespace KeyboardManagerEditorUI.Pages } // If no errors, proceed to save the remapping - bool saved = SaveCurrentMapping(); + bool saved = RemappingHelper.SaveMapping(_mappingService!, originalKeys, remappedKeys, isAppSpecific, appName); if (saved) { // Display the remapping in the list after saving @@ -306,7 +306,7 @@ namespace KeyboardManagerEditorUI.Pages DeleteRemapping(_editingRemapping); } - bool saved = SaveCurrentMapping(); + bool saved = RemappingHelper.SaveMapping(_mappingService!, RemappingControl.GetOriginalKeys(), RemappingControl.GetRemappedKeys(), RemappingControl.GetIsAppSpecific(), RemappingControl.GetAppName()); if (saved) { KeyDialog.Hide(); @@ -354,71 +354,6 @@ namespace KeyboardManagerEditorUI.Pages return KeyboardManagerInterop.GetKeyCodeFromName(keyName); } - private bool SaveCurrentMapping() - { - if (_mappingService == null) - { - Logger.LogError("Mapping service is null, cannot save mapping"); - return false; - } - - try - { - List originalKeys = RemappingControl.GetOriginalKeys(); - List remappedKeys = RemappingControl.GetRemappedKeys(); - bool isAppSpecific = RemappingControl.GetIsAppSpecific(); - string appName = RemappingControl.GetAppName(); - - if (originalKeys == null || originalKeys.Count == 0 || remappedKeys == null || remappedKeys.Count == 0) - { - return false; - } - - if (originalKeys.Count == 1) - { - int originalKey = GetKeyCode(originalKeys[0]); - if (originalKey != 0) - { - if (remappedKeys.Count == 1) - { - int targetKey = GetKeyCode(remappedKeys[0]); - if (targetKey != 0) - { - _mappingService.AddSingleKeyMapping(originalKey, targetKey); - } - } - else - { - string targetKeys = string.Join(";", remappedKeys.Select(k => GetKeyCode(k).ToString(CultureInfo.InvariantCulture))); - _mappingService.AddSingleKeyMapping(originalKey, targetKeys); - } - } - } - else - { - string originalKeysString = string.Join(";", originalKeys.Select(k => GetKeyCode(k).ToString(CultureInfo.InvariantCulture))); - string targetKeysString = string.Join(";", remappedKeys.Select(k => GetKeyCode(k).ToString(CultureInfo.InvariantCulture))); - - if (isAppSpecific && !string.IsNullOrEmpty(appName)) - { - _mappingService.AddShortcutMapping(originalKeysString, targetKeysString, appName); - } - else - { - _mappingService.AddShortcutMapping(originalKeysString, targetKeysString); - } - } - - _mappingService.SaveSettings(); - return true; - } - catch (Exception ex) - { - Logger.LogError("Error saving shortcut mapping: " + ex.Message); - return false; - } - } - private void DeleteButton_Click(object sender, RoutedEventArgs e) { if (sender is Button button && button.DataContext is Remapping remapping)