下班, 最近要忙起来了

This commit is contained in:
liufei
2021-06-02 17:34:02 +08:00
parent 2f0d6a9405
commit 7246687de0
4 changed files with 52 additions and 15 deletions

View File

@@ -1,5 +1,7 @@
using GeekDesk.Util; using GeekDesk.Util;
using GeekDesk.ViewModel; using GeekDesk.ViewModel;
using GlobalHotKey;
using HandyControl.Data;
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -11,6 +13,7 @@ using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
@@ -140,11 +143,15 @@ namespace GeekDesk.Control.UserControls
ConfigWindow cw = (ConfigWindow)Window.GetWindow(this); ConfigWindow cw = (ConfigWindow)Window.GetWindow(this);
try try
{ {
Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () => if (cw.mainWindow.hotKeyId != -1)
{
Hotkey.UnRegist(new WindowInteropHelper(cw.mainWindow).Handle, Hotkey.keymap[cw.mainWindow.hotKeyId]);
}
cw.mainWindow.hotKeyId = Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () =>
{ {
if (cw.mainWindow.Visibility == Visibility.Collapsed) if (cw.mainWindow.Visibility == Visibility.Collapsed)
{ {
ShowApp(cw.mainWindow); cw.mainWindow.ShowApp();
} }
else else
{ {
@@ -153,7 +160,7 @@ namespace GeekDesk.Control.UserControls
}); });
} catch (Exception) } catch (Exception)
{ {
HandyControl.Controls.Growl.ErrorGlobal("热键注册失败,当前热键已被其它程序占用!"); HandyControl.Controls.Growl.WarningGlobal("当前快捷键已被其它程序占用(" + appConfig.HotkeyStr + ")!");
} }
} }

View File

@@ -4,6 +4,7 @@ using GeekDesk.Control;
using GeekDesk.Util; using GeekDesk.Util;
using GeekDesk.ViewModel; using GeekDesk.ViewModel;
using GlobalHotKey; using GlobalHotKey;
using HandyControl.Data;
using SharpShell.SharpContextMenu; using SharpShell.SharpContextMenu;
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@@ -26,6 +27,8 @@ namespace GeekDesk
{ {
public static AppData appData = CommonCode.GetAppDataByFile(); public static AppData appData = CommonCode.GetAppDataByFile();
public int hotKeyId = -1;
public HotKeyManager hkm = new HotKeyManager();
public MainWindow() public MainWindow()
{ {
LoadData(); LoadData();
@@ -53,20 +56,47 @@ namespace GeekDesk
{ {
this.Visibility = Visibility.Collapsed; this.Visibility = Visibility.Collapsed;
} }
//加载完毕注册热键 try
Hotkey.Regist(this, HotkeyModifiers.MOD_CONTROL | HotkeyModifiers.MOD_ALT, Key.Y, ()=> {
HotKey hk = hkm.Register(Key.Y, ModifierKeys.Control);
hkm.KeyPressed += DisplayWindowHotKeyPress;
////加载完毕注册热键
//hotKeyId = Hotkey.Regist(this, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
//{
// if (this.Visibility == Visibility.Collapsed)
// {
// ShowApp();
// }
// else
// {
// this.Visibility = Visibility.Collapsed;
// }
//});
} catch (Exception)
{
HandyControl.Controls.Growl.WarningGlobal("启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
}
}
private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e)
{
if (e.HotKey.Key == Key.Y)
{ {
if (this.Visibility == Visibility.Collapsed) if (this.Visibility == Visibility.Collapsed)
{ {
ShowApp(); ShowApp();
} else }
else
{ {
this.Visibility = Visibility.Collapsed; this.Visibility = Visibility.Collapsed;
} }
}); }
} }
void MainWindow_Resize(object sender, System.EventArgs e) void MainWindow_Resize(object sender, System.EventArgs e)
{ {
if (this.DataContext != null) if (this.DataContext != null)
@@ -148,7 +178,7 @@ namespace GeekDesk
{ {
ShowApp(); ShowApp();
} }
private void ShowApp() public void ShowApp()
{ {
if (appData.AppConfig.FollowMouse) if (appData.AppConfig.FollowMouse)
{ {

View File

@@ -30,7 +30,7 @@ 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 bool Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) public static int Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack)
{ {
var hwnd = new WindowInteropHelper(window).Handle; var hwnd = new WindowInteropHelper(window).Handle;
var _hwndSource = HwndSource.FromHwnd(hwnd); var _hwndSource = HwndSource.FromHwnd(hwnd);
@@ -40,8 +40,8 @@ namespace GeekDesk.Util
var vk = KeyInterop.VirtualKeyFromKey(key); var vk = KeyInterop.VirtualKeyFromKey(key);
keymap[id] = callBack; keymap[id] = callBack;
if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed");
return RegisterHotKey(hwnd, id, fsModifiers, (uint)vk); return id;
} }
/// <summary> /// <summary>
@@ -77,7 +77,7 @@ namespace GeekDesk.Util
const int WM_HOTKEY = 0x312; const int WM_HOTKEY = 0x312;
static int keyid = 10; static int keyid = 10;
static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>(); public static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
public delegate void HotKeyCallBackHanlder(); public delegate void HotKeyCallBackHanlder();
} }

View File

@@ -37,9 +37,9 @@ namespace GeekDesk.ViewModel
private int pannelCornerRadius = 4; //面板圆角 默认4 private int pannelCornerRadius = 4; //面板圆角 默认4
[field: NonSerialized] [field: NonSerialized]
private ObservableCollection<IconInfo> selectedMenuIcons; private ObservableCollection<IconInfo> selectedMenuIcons;
private string hotkeyStr; private string hotkeyStr = "Ctrl + Q";
private HotkeyModifiers hotkeyModifiers; private HotkeyModifiers hotkeyModifiers = HotkeyModifiers.MOD_CONTROL;
private Key hotkey; private Key hotkey = Key.Q;
#region GetSet #region GetSet
public Key Hotkey public Key Hotkey