尝试解决快捷键设置的bug, 下班
This commit is contained in:
@@ -7,6 +7,7 @@ using Microsoft.Win32;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
@@ -27,8 +28,9 @@ namespace GeekDesk.Control.UserControls.Config
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class MotionControl : UserControl
|
public partial class MotionControl : UserControl
|
||||||
{
|
{
|
||||||
private static bool hotkeyFinished = true; //热键设置结束
|
public static bool hotkeyFinished = true; //热键设置结束
|
||||||
private static KeyEventArgs prevKeyTemp; //上一个按键
|
private static KeyEventArgs prevKeyTemp; //上一个按键
|
||||||
|
private static List<KeyEventArgs> keysTemp = new List<KeyEventArgs>();//存储一次快捷键集合
|
||||||
private static AppConfig appConfig = MainWindow.appData.AppConfig;
|
private static AppConfig appConfig = MainWindow.appData.AppConfig;
|
||||||
|
|
||||||
public MotionControl()
|
public MotionControl()
|
||||||
@@ -62,6 +64,7 @@ namespace GeekDesk.Control.UserControls.Config
|
|||||||
appConfig.HotkeyStr = GetKeyName(e);
|
appConfig.HotkeyStr = GetKeyName(e);
|
||||||
appConfig.HotkeyModifiers = GetModifierKeys(e);
|
appConfig.HotkeyModifiers = GetModifierKeys(e);
|
||||||
prevKeyTemp = e;
|
prevKeyTemp = e;
|
||||||
|
keysTemp.Add(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -75,12 +78,14 @@ namespace GeekDesk.Control.UserControls.Config
|
|||||||
appConfig.Hotkey = e.Key;
|
appConfig.Hotkey = e.Key;
|
||||||
appConfig.HotkeyStr += e.Key.ToString();
|
appConfig.HotkeyStr += e.Key.ToString();
|
||||||
prevKeyTemp = e;
|
prevKeyTemp = e;
|
||||||
|
keysTemp.Add(e);
|
||||||
}
|
}
|
||||||
else if (CheckModifierKeys(e))
|
else if (CheckModifierKeys(e))
|
||||||
{
|
{
|
||||||
appConfig.HotkeyStr += GetKeyName(e);
|
appConfig.HotkeyStr += GetKeyName(e);
|
||||||
appConfig.HotkeyModifiers |= GetModifierKeys(e);
|
appConfig.HotkeyModifiers |= GetModifierKeys(e);
|
||||||
prevKeyTemp = e;
|
prevKeyTemp = e;
|
||||||
|
keysTemp.Add(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,94 +143,94 @@ namespace GeekDesk.Control.UserControls.Config
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void HotKeyUp(object sender, KeyEventArgs e)
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||||
|
private void HotKeyUp(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
hotkeyFinished = true;
|
lock(this)
|
||||||
ConfigWindow cw = (ConfigWindow)Window.GetWindow(this);
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (cw.mainWindow.hotKeyId != -1)
|
bool allKeyUp = true;
|
||||||
|
//判断所有键是否都松开
|
||||||
|
foreach (KeyEventArgs key in keysTemp)
|
||||||
{
|
{
|
||||||
Hotkey.UnRegist(new WindowInteropHelper(cw.mainWindow).Handle, Hotkey.keymap[cw.mainWindow.hotKeyId]);
|
HandyControl.Controls.Growl.SuccessGlobal(key.Key.ToString() + "=" + key.KeyStates);
|
||||||
|
if (key.KeyStates == KeyStates.Down)
|
||||||
|
{
|
||||||
|
allKeyUp = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cw.mainWindow.hotKeyId = Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () =>
|
if (allKeyUp && !hotkeyFinished)
|
||||||
{
|
{
|
||||||
if (cw.mainWindow.Visibility == Visibility.Collapsed)
|
hotkeyFinished = true;
|
||||||
|
if (MainWindow.hotKeyId != -1)
|
||||||
{
|
{
|
||||||
cw.mainWindow.ShowApp();
|
Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]);
|
||||||
}
|
}
|
||||||
else
|
MainWindow.RegisterHotKey();
|
||||||
{
|
}
|
||||||
cw.mainWindow.Visibility = Visibility.Collapsed;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception)
|
|
||||||
{
|
|
||||||
HandyControl.Controls.Growl.WarningGlobal("当前快捷键已被其它程序占用(" + appConfig.HotkeyStr + ")!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ShowApp(MainWindow mainWindow)
|
//private void ShowApp(MainWindow mainWindow)
|
||||||
{
|
//{
|
||||||
if (appConfig.FollowMouse)
|
// if (appConfig.FollowMouse)
|
||||||
{
|
// {
|
||||||
ShowAppAndFollowMouse(mainWindow);
|
// ShowAppAndFollowMouse(mainWindow);
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
this.Visibility = Visibility.Visible;
|
// this.Visibility = Visibility.Visible;
|
||||||
}
|
// }
|
||||||
Keyboard.Focus(this);
|
// Keyboard.Focus(this);
|
||||||
}
|
//}
|
||||||
|
|
||||||
/// <summary>
|
///// <summary>
|
||||||
/// 随鼠标位置显示面板 (鼠标始终在中间)
|
///// 随鼠标位置显示面板 (鼠标始终在中间)
|
||||||
/// </summary>
|
///// </summary>
|
||||||
private void ShowAppAndFollowMouse(MainWindow mainWindow)
|
//private void ShowAppAndFollowMouse(MainWindow mainWindow)
|
||||||
{
|
//{
|
||||||
//获取鼠标位置
|
// //获取鼠标位置
|
||||||
System.Windows.Point p = MouseUtil.GetMousePosition();
|
// System.Windows.Point p = MouseUtil.GetMousePosition();
|
||||||
double left = SystemParameters.VirtualScreenLeft;
|
// double left = SystemParameters.VirtualScreenLeft;
|
||||||
double top = SystemParameters.VirtualScreenTop;
|
// double top = SystemParameters.VirtualScreenTop;
|
||||||
double width = SystemParameters.VirtualScreenWidth;
|
// double width = SystemParameters.VirtualScreenWidth;
|
||||||
double height = SystemParameters.VirtualScreenHeight;
|
// double height = SystemParameters.VirtualScreenHeight;
|
||||||
double right = width - Math.Abs(left);
|
// double right = width - Math.Abs(left);
|
||||||
double bottom = height - Math.Abs(top);
|
// double bottom = height - Math.Abs(top);
|
||||||
|
|
||||||
|
|
||||||
if (p.X - mainWindow.Width / 2 < left)
|
// if (p.X - mainWindow.Width / 2 < left)
|
||||||
{
|
// {
|
||||||
//判断是否在最左边缘
|
// //判断是否在最左边缘
|
||||||
mainWindow.Left = left;
|
// mainWindow.Left = left;
|
||||||
}
|
// }
|
||||||
else if (p.X + mainWindow.Width / 2 > right)
|
// else if (p.X + mainWindow.Width / 2 > right)
|
||||||
{
|
// {
|
||||||
//判断是否在最右边缘
|
// //判断是否在最右边缘
|
||||||
mainWindow.Left = right - mainWindow.Width;
|
// mainWindow.Left = right - mainWindow.Width;
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
mainWindow.Left = p.X - mainWindow.Width / 2;
|
// mainWindow.Left = p.X - mainWindow.Width / 2;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
if (p.Y - mainWindow.Height / 2 < top)
|
// if (p.Y - mainWindow.Height / 2 < top)
|
||||||
{
|
// {
|
||||||
//判断是否在最上边缘
|
// //判断是否在最上边缘
|
||||||
mainWindow.Top = top;
|
// mainWindow.Top = top;
|
||||||
}
|
// }
|
||||||
else if (p.Y + mainWindow.Height / 2 > bottom)
|
// else if (p.Y + mainWindow.Height / 2 > bottom)
|
||||||
{
|
// {
|
||||||
//判断是否在最下边缘
|
// //判断是否在最下边缘
|
||||||
mainWindow.Top = bottom - mainWindow.Height;
|
// mainWindow.Top = bottom - mainWindow.Height;
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
mainWindow.Top = p.Y - mainWindow.Height / 2;
|
// mainWindow.Top = p.Y - mainWindow.Height / 2;
|
||||||
}
|
// }
|
||||||
|
|
||||||
mainWindow.Visibility = Visibility.Visible;
|
// mainWindow.Visibility = Visibility.Visible;
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using DraggAnimatedPanelExample;
|
using DraggAnimatedPanelExample;
|
||||||
using GeekDesk.Constant;
|
using GeekDesk.Constant;
|
||||||
using GeekDesk.Control;
|
using GeekDesk.Control;
|
||||||
|
using GeekDesk.Control.UserControls.Config;
|
||||||
using GeekDesk.Control.Windows;
|
using GeekDesk.Control.Windows;
|
||||||
using GeekDesk.Task;
|
using GeekDesk.Task;
|
||||||
using GeekDesk.Util;
|
using GeekDesk.Util;
|
||||||
@@ -29,12 +30,14 @@ namespace GeekDesk
|
|||||||
{
|
{
|
||||||
|
|
||||||
public static AppData appData = CommonCode.GetAppDataByFile();
|
public static AppData appData = CommonCode.GetAppDataByFile();
|
||||||
public int hotKeyId = -1;
|
public static int hotKeyId = -1;
|
||||||
|
public static MainWindow mainWindow;
|
||||||
public HotKeyManager hkm = new HotKeyManager();
|
public HotKeyManager hkm = new HotKeyManager();
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
LoadData();
|
LoadData();
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
mainWindow = this;
|
||||||
this.Topmost = true;
|
this.Topmost = true;
|
||||||
this.Loaded += Window_Loaded;
|
this.Loaded += Window_Loaded;
|
||||||
this.SizeChanged += MainWindow_Resize;
|
this.SizeChanged += MainWindow_Resize;
|
||||||
@@ -58,28 +61,43 @@ namespace GeekDesk
|
|||||||
if (!appData.AppConfig.StartedShowPanel)
|
if (!appData.AppConfig.StartedShowPanel)
|
||||||
{
|
{
|
||||||
this.Visibility = Visibility.Collapsed;
|
this.Visibility = Visibility.Collapsed;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
ShowApp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册当前窗口的热键
|
||||||
|
/// </summary>
|
||||||
|
public static void RegisterHotKey()
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HotKey hk = hkm.Register(Key.Y, ModifierKeys.Control);
|
//加载完毕注册热键
|
||||||
hkm.KeyPressed += DisplayWindowHotKeyPress;
|
hotKeyId = Hotkey.Regist(new WindowInteropHelper(MainWindow.mainWindow).Handle, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
|
||||||
////加载完毕注册热键
|
{
|
||||||
//hotKeyId = Hotkey.Regist(this, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
|
if (MotionControl.hotkeyFinished)
|
||||||
//{
|
{
|
||||||
// if (this.Visibility == Visibility.Collapsed)
|
if (mainWindow.Visibility == Visibility.Collapsed)
|
||||||
// {
|
{
|
||||||
// ShowApp();
|
ShowApp();
|
||||||
// }
|
}
|
||||||
// else
|
else
|
||||||
// {
|
{
|
||||||
// this.Visibility = Visibility.Collapsed;
|
mainWindow.Visibility = Visibility.Collapsed;
|
||||||
// }
|
}
|
||||||
//});
|
}
|
||||||
} catch (Exception)
|
});
|
||||||
|
//HandyControl.Controls.Growl.SuccessGlobal("快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
{
|
{
|
||||||
HandyControl.Controls.Growl.WarningGlobal("启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
|
HandyControl.Controls.Growl.WarningGlobal("启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e)
|
private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e)
|
||||||
@@ -180,22 +198,22 @@ namespace GeekDesk
|
|||||||
{
|
{
|
||||||
ShowApp();
|
ShowApp();
|
||||||
}
|
}
|
||||||
public void ShowApp()
|
public static void ShowApp()
|
||||||
{
|
{
|
||||||
if (appData.AppConfig.FollowMouse)
|
if (appData.AppConfig.FollowMouse)
|
||||||
{
|
{
|
||||||
ShowAppAndFollowMouse();
|
ShowAppAndFollowMouse();
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
this.Visibility = Visibility.Visible;
|
mainWindow.Visibility = Visibility.Visible;
|
||||||
}
|
}
|
||||||
Keyboard.Focus(this);
|
Keyboard.Focus(mainWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 随鼠标位置显示面板 (鼠标始终在中间)
|
/// 随鼠标位置显示面板 (鼠标始终在中间)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void ShowAppAndFollowMouse()
|
private static void ShowAppAndFollowMouse()
|
||||||
{
|
{
|
||||||
//获取鼠标位置
|
//获取鼠标位置
|
||||||
System.Windows.Point p = MouseUtil.GetMousePosition();
|
System.Windows.Point p = MouseUtil.GetMousePosition();
|
||||||
@@ -207,38 +225,38 @@ namespace GeekDesk
|
|||||||
double bottom = height - Math.Abs(top);
|
double bottom = height - Math.Abs(top);
|
||||||
|
|
||||||
|
|
||||||
if (p.X - this.Width / 2 < left)
|
if (p.X - mainWindow.Width / 2 < left)
|
||||||
{
|
{
|
||||||
//判断是否在最左边缘
|
//判断是否在最左边缘
|
||||||
this.Left = left;
|
mainWindow.Left = left;
|
||||||
}
|
}
|
||||||
else if (p.X + this.Width / 2 > right)
|
else if (p.X + mainWindow.Width / 2 > right)
|
||||||
{
|
{
|
||||||
//判断是否在最右边缘
|
//判断是否在最右边缘
|
||||||
this.Left = right - this.Width;
|
mainWindow.Left = right - mainWindow.Width;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.Left = p.X - this.Width / 2;
|
mainWindow.Left = p.X - mainWindow.Width / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (p.Y - this.Height / 2 < top)
|
if (p.Y - mainWindow.Height / 2 < top)
|
||||||
{
|
{
|
||||||
//判断是否在最上边缘
|
//判断是否在最上边缘
|
||||||
this.Top = top;
|
mainWindow.Top = top;
|
||||||
}
|
}
|
||||||
else if (p.Y + this.Height / 2 > bottom)
|
else if (p.Y + mainWindow.Height / 2 > bottom)
|
||||||
{
|
{
|
||||||
//判断是否在最下边缘
|
//判断是否在最下边缘
|
||||||
this.Top = bottom - this.Height;
|
mainWindow.Top = bottom - mainWindow.Height;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.Top = p.Y - this.Height / 2;
|
mainWindow.Top = p.Y - mainWindow.Height / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.Visibility = Visibility.Visible;
|
mainWindow.Visibility = Visibility.Visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,17 +30,16 @@ namespace GeekDesk.Util
|
|||||||
/// <param name="fsModifiers">组合键</param>
|
/// <param name="fsModifiers">组合键</param>
|
||||||
/// <param name="key">快捷键</param>
|
/// <param name="key">快捷键</param>
|
||||||
/// <param name="callBack">回调函数</param>
|
/// <param name="callBack">回调函数</param>
|
||||||
public static int Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack)
|
public static int Regist(IntPtr windowHandle, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack)
|
||||||
{
|
{
|
||||||
var hwnd = new WindowInteropHelper(window).Handle;
|
var _hwndSource = HwndSource.FromHwnd(windowHandle);
|
||||||
var _hwndSource = HwndSource.FromHwnd(hwnd);
|
|
||||||
_hwndSource.AddHook(WndProc);
|
_hwndSource.AddHook(WndProc);
|
||||||
|
|
||||||
int id = keyid++;
|
int id = keyid++;
|
||||||
|
|
||||||
var vk = KeyInterop.VirtualKeyFromKey(key);
|
var vk = KeyInterop.VirtualKeyFromKey(key);
|
||||||
keymap[id] = callBack;
|
keymap.Add(id, callBack);
|
||||||
if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed");
|
if (!RegisterHotKey(windowHandle, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed");
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +66,15 @@ namespace GeekDesk.Util
|
|||||||
/// <param name="callBack">回调函数</param>
|
/// <param name="callBack">回调函数</param>
|
||||||
public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
|
public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
|
||||||
{
|
{
|
||||||
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
|
|
||||||
|
List<int> list = new List<int>(keymap.Keys);
|
||||||
|
for (int i=0; i < list.Count; i++)
|
||||||
{
|
{
|
||||||
if (var.Value == callBack)
|
if (keymap[list[i]] == callBack)
|
||||||
UnregisterHotKey(hWnd, var.Key);
|
{
|
||||||
|
UnregisterHotKey(hWnd, list[i]);
|
||||||
|
keymap.Remove(list[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user