feat: 提取注册快捷键回调

dev_split_view_vm
DESKTOP-3BO4HSG\ksat 2 years ago
parent 9708ff1cdd
commit de3e6f0db5

@ -1,4 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Interop;
using static STranslate.Helper.NativeMethodHelper; using static STranslate.Helper.NativeMethodHelper;
namespace STranslate.Helper namespace STranslate.Helper
@ -58,6 +61,38 @@ namespace STranslate.Helper
public static byte OpenMainWindowModifiers; public static byte OpenMainWindowModifiers;
public static int OpenMainWindowKey; public static int OpenMainWindowKey;
public delegate void HotKeyCallBackHanlder();
private static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
public static void InitialHook(Window window)
{
var hwnd = new WindowInteropHelper(window).Handle;
RegisterHotKey(hwnd);
var _hwndSource = HwndSource.FromHwnd(hwnd);
_hwndSource.AddHook(WndProc);
}
public static void Register(int id, HotKeyCallBackHanlder callBack)
{
keymap[id] = callBack;
}
/// <summary>
/// 快捷键消息处理
/// </summary>
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
int id = wParam.ToInt32();
if (keymap.TryGetValue(id, out var callback))
{
callback();
}
break;
}
return IntPtr.Zero;
}
/// <summary> /// <summary>
/// 注册快捷键 /// 注册快捷键
/// </summary> /// </summary>

@ -138,44 +138,26 @@ namespace STranslate
protected override void OnSourceInitialized(EventArgs e) protected override void OnSourceInitialized(EventArgs e)
{ {
//base.OnSourceInitialized(e); //base.OnSourceInitialized(e);
IntPtr handle = new WindowInteropHelper(this).Handle; HotkeysHelper.InitialHook(this);
HotkeysHelper.RegisterHotKey(handle); HotkeysHelper.Register(HotkeysHelper.InputTranslateId, () =>
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(WndProc);
}
/// <summary>
/// 热键的功能
/// </summary>
/// <param name="m"></param>
protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle)
{ {
switch (msg) InputTranslateMenuItem_Click(null, null);
{ });
case 0x0312: //这个是window消息定义的 注册的热键消息
//Console.WriteLine(wParam.ToString()); HotkeysHelper.Register(HotkeysHelper.CrosswordTranslateId, () =>
if (wParam.ToString().Equals(HotkeysHelper.InputTranslateId + ""))
{
this.InputTranslateMenuItem_Click(null, null);
}
else if (wParam.ToString().Equals(HotkeysHelper.CrosswordTranslateId + ""))
{
this.CrossWordTranslateMenuItem_Click(null, null);
}
#if false
else if (wParam.ToString().Equals(HotkeysHelper.ScreenShotTranslateId + ""))
{ {
this.ScreenshotTranslateMenuItem_Click(null, null); CrossWordTranslateMenuItem_Click(null, null);
} });
#endif
else if (wParam.ToString().Equals(HotkeysHelper.OpenMainWindowId + "")) //HotkeysHelper.Register(HotkeysHelper.ScreenShotTranslateId, () =>
//{
// ScreenshotTranslateMenuItem_Click(null, null);
//});
HotkeysHelper.Register(HotkeysHelper.OpenMainWindowId, () =>
{ {
this.OpenMainWin_Click(null, null); OpenMainWin_Click(null, null);
} });
break;
}
return IntPtr.Zero;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save