From 471cc632b2f63c164a30b075eab7786df66a2738 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Wed, 18 Jan 2023 12:08:39 +0800 Subject: [PATCH] feat: add change hotkeys func --- STranslate/Helper/HotkeysHelper.cs | 103 ++++++++----------------- STranslate/Model/ConfigModel.cs | 79 +++++++++++++++++++ STranslate/View/MainWindow.xaml.cs | 5 +- STranslate/View/SettingsWindow.xaml | 41 ++++++---- STranslate/View/SettingsWindow.xaml.cs | 81 ++++++++++++++++++- STranslate/ViewModel/MainVM.cs | 36 +++++++++ 6 files changed, 260 insertions(+), 85 deletions(-) diff --git a/STranslate/Helper/HotkeysHelper.cs b/STranslate/Helper/HotkeysHelper.cs index 9ab02f4..5156123 100644 --- a/STranslate/Helper/HotkeysHelper.cs +++ b/STranslate/Helper/HotkeysHelper.cs @@ -6,41 +6,6 @@ using static STranslate.Helper.NativeMethodHelper; namespace STranslate.Helper { - public static class Hotkeys - { - public static class InputTranslate - { - public static byte Modifiers = (byte)KeyModifiers.MOD_ALT; - public static int Key = 65; - public static String Text = "A"; - public static bool Conflict = false; - } - - public static class CrosswordTranslate - { - public static byte Modifiers = (byte)KeyModifiers.MOD_ALT; - public static int Key = 68; - public static String Text = "D"; - public static bool Conflict = false; - } - -#if true - public static class ScreenShotTranslate - { - public static byte Modifiers = (byte)KeyModifiers.MOD_ALT; - public static int Key = 83; - public static String Text = "S"; - public static bool Conflict = false; - } -#endif - public static class OpenMainWindow - { - public static byte Modifiers = (byte)KeyModifiers.MOD_ALT; - public static int Key = 71; - public static String Text = "G"; - public static bool Conflict = false; - } - } internal class HotkeysHelper { @@ -107,31 +72,31 @@ namespace STranslate.Helper { HotkeysHelper.mainFormHandle = mainFormHandle; - InputTranslateModifiers = Hotkeys.InputTranslate.Modifiers; - InputTranslateKey = Hotkeys.InputTranslate.Key; - CrosswordTranslateModifiers = Hotkeys.CrosswordTranslate.Modifiers; - CrosswordTranslateKey = Hotkeys.CrosswordTranslate.Key; + InputTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Modifiers; + InputTranslateKey = ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key; + CrosswordTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Modifiers; + CrosswordTranslateKey = ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key; #if true - ScreenShotTranslateModifiers = Hotkeys.ScreenShotTranslate.Modifiers; - ScreenShotTranslateKey = Hotkeys.ScreenShotTranslate.Key; - if (Hotkeys.ScreenShotTranslate.Key != 0) + ScreenShotTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Modifiers; + ScreenShotTranslateKey = ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key; + if (ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key != 0) { - Hotkeys.ScreenShotTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, Hotkeys.ScreenShotTranslate.Modifiers, Hotkeys.ScreenShotTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key); } #endif - if (Hotkeys.InputTranslate.Key != 0) + if (ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key != 0) { - Hotkeys.InputTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, InputTranslateId, Hotkeys.InputTranslate.Modifiers, Hotkeys.InputTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, InputTranslateId, ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key); } - if (Hotkeys.CrosswordTranslate.Key != 0) + if (ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key != 0) { - Hotkeys.CrosswordTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, CrosswordTranslateId, Hotkeys.CrosswordTranslate.Modifiers, Hotkeys.CrosswordTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, CrosswordTranslateId, ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key); } - if (Hotkeys.OpenMainWindow.Key != 0) + if (ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key != 0) { - Hotkeys.OpenMainWindow.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, OpenMainWindowId, Hotkeys.OpenMainWindow.Modifiers, Hotkeys.OpenMainWindow.Key); + ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, OpenMainWindowId, ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Modifiers, ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key); } } @@ -153,59 +118,59 @@ namespace STranslate.Helper /// public static void ReRegisterHotKey() { - if (Hotkeys.InputTranslate.Key == 0) + if (ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key == 0) { UnregisterHotKey(mainFormHandle, InputTranslateId); } - else if (InputTranslateModifiers != Hotkeys.InputTranslate.Modifiers || InputTranslateKey != Hotkeys.InputTranslate.Key) + else if (InputTranslateModifiers != ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Modifiers || InputTranslateKey != ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key) { { UnregisterHotKey(mainFormHandle, InputTranslateId); - Hotkeys.InputTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, InputTranslateId, Hotkeys.InputTranslate.Modifiers, Hotkeys.InputTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, InputTranslateId, ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key); } } - InputTranslateModifiers = Hotkeys.InputTranslate.Modifiers; - InputTranslateKey = Hotkeys.InputTranslate.Key; + InputTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Modifiers; + InputTranslateKey = ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Key; - if (Hotkeys.CrosswordTranslate.Key == 0) + if (ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key == 0) { UnregisterHotKey(mainFormHandle, CrosswordTranslateId); } - else if (CrosswordTranslateModifiers != Hotkeys.CrosswordTranslate.Modifiers || CrosswordTranslateKey != Hotkeys.CrosswordTranslate.Key) + else if (CrosswordTranslateModifiers != ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Modifiers || CrosswordTranslateKey != ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key) { { UnregisterHotKey(mainFormHandle, CrosswordTranslateId); - Hotkeys.CrosswordTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, CrosswordTranslateId, Hotkeys.CrosswordTranslate.Modifiers, Hotkeys.CrosswordTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, CrosswordTranslateId, ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key); } } - CrosswordTranslateModifiers = Hotkeys.CrosswordTranslate.Modifiers; - CrosswordTranslateKey = Hotkeys.CrosswordTranslate.Key; + CrosswordTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Modifiers; + CrosswordTranslateKey = ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Key; #if true - if (Hotkeys.ScreenShotTranslate.Key == 0) + if (ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key == 0) { UnregisterHotKey(mainFormHandle, ScreenShotTranslateId); } - else if (ScreenShotTranslateModifiers != Hotkeys.ScreenShotTranslate.Modifiers || ScreenShotTranslateKey != Hotkeys.ScreenShotTranslate.Key) + else if (ScreenShotTranslateModifiers != ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Modifiers || ScreenShotTranslateKey != ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key) { UnregisterHotKey(mainFormHandle, ScreenShotTranslateId); - Hotkeys.ScreenShotTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, Hotkeys.ScreenShotTranslate.Modifiers, Hotkeys.ScreenShotTranslate.Key); + ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Modifiers, ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key); } - ScreenShotTranslateModifiers = Hotkeys.ScreenShotTranslate.Modifiers; - ScreenShotTranslateKey = Hotkeys.ScreenShotTranslate.Key; + ScreenShotTranslateModifiers = ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Modifiers; + ScreenShotTranslateKey = ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Key; #endif - if (Hotkeys.OpenMainWindow.Key == 0) + if (ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key == 0) { UnregisterHotKey(mainFormHandle, OpenMainWindowId); } - else if (OpenMainWindowModifiers != Hotkeys.OpenMainWindow.Modifiers || OpenMainWindowKey != Hotkeys.OpenMainWindow.Key) + else if (OpenMainWindowModifiers != ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Modifiers || OpenMainWindowKey != ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key) { UnregisterHotKey(mainFormHandle, OpenMainWindowId); - Hotkeys.OpenMainWindow.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, OpenMainWindowId, Hotkeys.OpenMainWindow.Modifiers, Hotkeys.OpenMainWindow.Key); + ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Conflict = !NativeMethodHelper.RegisterHotKey(mainFormHandle, OpenMainWindowId, ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Modifiers, ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key); } - OpenMainWindowModifiers = Hotkeys.OpenMainWindow.Modifiers; - OpenMainWindowKey = Hotkeys.OpenMainWindow.Key; + OpenMainWindowModifiers = ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Modifiers; + OpenMainWindowKey = ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Key; } } } \ No newline at end of file diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index b3b43b8..57d009e 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -41,6 +41,37 @@ namespace STranslate.Model Name = "local", Api = "http://127.0.0.1:8000/translate" } + }, + Hotkeys = new Hotkeys + { + InputTranslate = new InputTranslate + { + Modifiers = 1, + Key = 65, + Text = "Alt + A", + Conflict = false, + }, + CrosswordTranslate = new CrosswordTranslate + { + Modifiers = 1, + Key = 68, + Text = "Alt + D", + Conflict = false, + }, + ScreenShotTranslate = new ScreenShotTranslate + { + Modifiers = 1, + Key = 83, + Text = "Alt + S", + Conflict = false, + }, + OpenMainWindow = new OpenMainWindow + { + Modifiers = 1, + Key = 71, + Text = "Alt + G", + Conflict = false, + }, } }; } @@ -81,8 +112,56 @@ namespace STranslate.Model [JsonProperty("servers")] public Server[] Servers { get; set; } + /// + /// 热键 + /// + [JsonProperty("hotkeys")] + public Hotkeys Hotkeys { get; set; } + } + public class Hotkeys + { + [JsonProperty("inputTranslate")] + public InputTranslate InputTranslate { get; set; } + + [JsonProperty("crosswordTranslate")] + public CrosswordTranslate CrosswordTranslate { get; set; } + + [JsonProperty("screenShotTranslate")] + public ScreenShotTranslate ScreenShotTranslate { get; set; } + + [JsonProperty("openMainWindow")] + public OpenMainWindow OpenMainWindow { get; set; } + } + public class InputTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class CrosswordTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class ScreenShotTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class OpenMainWindow + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } public class Server { [JsonProperty("name")] diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index e0d1605..0aaab0e 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.cs @@ -53,7 +53,10 @@ namespace STranslate.View vm.OpenMainWin(); }); - if (Hotkeys.InputTranslate.Conflict || Hotkeys.CrosswordTranslate.Conflict || Hotkeys.ScreenShotTranslate.Conflict || Hotkeys.OpenMainWindow.Conflict) + if (ViewModel.MainVM.Instance.NHotkeys.InputTranslate.Conflict + || ViewModel.MainVM.Instance.NHotkeys.CrosswordTranslate.Conflict + || ViewModel.MainVM.Instance.NHotkeys.ScreenShotTranslate.Conflict + || ViewModel.MainVM.Instance.NHotkeys.OpenMainWindow.Conflict) { MessageBox.Show("全局快捷键有冲突,请前往软件首选项中修改..."); } diff --git a/STranslate/View/SettingsWindow.xaml b/STranslate/View/SettingsWindow.xaml index 0703ba2..2b8aa8f 100644 --- a/STranslate/View/SettingsWindow.xaml +++ b/STranslate/View/SettingsWindow.xaml @@ -6,7 +6,8 @@ xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:STranslate.View" mc:Ignorable="d" - SizeToContent="Height" + SizeToContent="WidthAndHeight" + Loaded="window_Loaded" WindowStartupLocation="CenterScreen" Icon="/Images/translate.ico" Title="STranslate 首选项" Height="450" Width="450"> @@ -139,7 +140,8 @@ Grid.Row="1" Grid.Column="0" Margin="0,5,0,0" /> - -