From 63438cf85ff99c1a3a30fd3ca6382c3a3dca49ad Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Wed, 21 Dec 2022 14:32:09 +0800 Subject: [PATCH] feat: perf shortcuts and perf view --- README.md | 4 +-- STranslate/MainWindow.xaml | 56 +++++--------------------------- STranslate/MainWindow.xaml.cs | 10 ++++-- STranslate/Utils/GetWordsUtil.cs | 30 +++++------------ STranslate/Utils/HotKeysUtil.cs | 2 +- STranslate/Utils/NativeMethod.cs | 10 ++++++ 6 files changed, 38 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index a53ce1d..6591bab 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,12 @@ WPF 开发的一款即用即走的翻译工具 打开软件后会静默在后台,等待调用,全局监听快捷键(日后升级成自定义) - `Alt` + `A` 即可打开软件主界面,输入需要翻译的内容,选择目标语言,如果识别语种不正确则手动指定即可,翻译结束后选择复制或点击一键复制即可 -- `Ctrl` + `D` 即可复制当前鼠标选中内容并翻译 +- `Alt` + `D` 即可复制当前鼠标选中内容并翻译 点击软件外部界面任意处或点击 ESC,Ctrl+Shift+Q 软件则会自动隐藏到后台,即用即走。 ## 参考项目 -部分功能参考如下项目,在此表示感谢 +监听全局快捷键功能参考如下项目,在此表示感谢 - [https://github.com/NPCDW/WpfTool](https://github.com/NPCDW/WpfTool) \ No newline at end of file diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 74b2265..c070977 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -67,19 +67,18 @@ BorderBrush="#000" BorderThickness="1" CornerRadius="4" - Margin="5" - Height="{Binding ElementName=Input, Path=Height}"> + Margin="5"> @@ -139,51 +138,12 @@ IsReadOnly="True" Margin="5" FontSize="18" - Width="{Binding ElementName=BorderOutput, Path=ActualWidth}" - Height="{Binding ElementName=BorderOutput, Path=Height}" - VerticalAlignment="Top" - HorizontalAlignment="Left" TextWrapping="Wrap" - Text="{Binding OutputTxt}" /> + VerticalContentAlignment="Top" + HorizontalScrollBarVisibility="Disabled" + VerticalScrollBarVisibility="Auto" + Text="{Binding OutputTxt}"/> - diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index 3ff77c9..f2e809d 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -17,7 +17,12 @@ namespace STranslate public MainWindow() { InitializeComponent(); - + this.Activate(); + this.TextBoxInput.Focus(); + //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) + //{ + // MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置"); + //} vm = (MainVM)DataContext; } @@ -105,6 +110,7 @@ namespace STranslate /// protected override void OnSourceInitialized(EventArgs e) { + base.OnSourceInitialized(e); IntPtr handle = new WindowInteropHelper(this).Handle; HotKeysUtil.RegisterHotKey(handle); @@ -192,7 +198,7 @@ namespace STranslate /// private void ScreenshotTranslateMenuItem_Click(object sender, RoutedEventArgs e) { - MessageBox.Show("开发中"); + HandyControl.Controls.MessageBox.Show("开发中"); } } } \ No newline at end of file diff --git a/STranslate/Utils/GetWordsUtil.cs b/STranslate/Utils/GetWordsUtil.cs index cadf9dd..9454d19 100644 --- a/STranslate/Utils/GetWordsUtil.cs +++ b/STranslate/Utils/GetWordsUtil.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Windows; +using System.Windows.Input; namespace STranslate.Utils { @@ -18,31 +19,18 @@ namespace STranslate.Utils //IntPtr hWnd = GetForegroundWindow(); //SetForegroundWindow(hWnd); uint KEYEVENTF_KEYUP = 2; + + NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(KeyInterop.VirtualKeyFromKey(Key.LeftAlt), 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(KeyInterop.VirtualKeyFromKey(Key.RightAlt), 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.LWin, 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.RWin, 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.ShiftKey, 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, 0, 0); NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, 0, 0); NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, KEYEVENTF_KEYUP, 0); NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up } - - private static String GetDataFromClipboard() - { - try - { - if (Clipboard.ContainsText()) //检查是否存在文本 - { - string res = Clipboard.GetText(); - if (!string.IsNullOrWhiteSpace(res)) - { - return res; - } - } - } - catch (Exception e) - { - Console.WriteLine(e); - return null; - } - return null; - } } } \ No newline at end of file diff --git a/STranslate/Utils/HotKeysUtil.cs b/STranslate/Utils/HotKeysUtil.cs index 383b28d..f7ec501 100644 --- a/STranslate/Utils/HotKeysUtil.cs +++ b/STranslate/Utils/HotKeysUtil.cs @@ -15,7 +15,7 @@ namespace STranslate.Utils public static class CrosswordTranslate { - public static byte Modifiers = (byte)KeyModifiers.MOD_CTRL; + public static byte Modifiers = (byte)KeyModifiers.MOD_ALT; public static int Key = 68; public static String Text = "D"; public static bool Conflict = false; diff --git a/STranslate/Utils/NativeMethod.cs b/STranslate/Utils/NativeMethod.cs index 184a366..b8268bf 100644 --- a/STranslate/Utils/NativeMethod.cs +++ b/STranslate/Utils/NativeMethod.cs @@ -39,6 +39,16 @@ namespace STranslate.Utils [DllImport("user32.dll")] public static extern void keybd_event(System.Windows.Forms.Keys vk, byte bScan, uint dwFlags, uint dwExtraInfo); + /// + /// 模拟触发键盘的按键 + /// + /// 按下的键 + /// + /// 触发的方式,0按下,2抬起 + /// + [DllImport("user32.dll")] + public static extern void keybd_event(int vk, byte bScan, uint dwFlags, uint dwExtraInfo); + /// /// 注册全局热键 ///