MotionControl.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using GeekDesk.Control.Windows;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using GlobalHotKey;
  5. using HandyControl.Data;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Interop;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. namespace GeekDesk.Control.UserControls.Config
  23. {
  24. /// <summary>
  25. /// 动作设置
  26. /// </summary>
  27. public partial class MotionControl : UserControl
  28. {
  29. private static bool hotkeyFinished = true; //热键设置结束
  30. private static KeyEventArgs prevKeyTemp; //上一个按键
  31. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  32. public MotionControl()
  33. {
  34. InitializeComponent();
  35. }
  36. /// <summary>
  37. /// 注册热键按下
  38. /// </summary>
  39. /// <param name="sender"></param>
  40. /// <param name="e"></param>
  41. private void HotKeyDown(object sender, KeyEventArgs e)
  42. {
  43. if (!e.IsRepeat)
  44. {
  45. if (hotkeyFinished)
  46. {
  47. appConfig.Hotkey = 0;
  48. appConfig.HotkeyStr = "";
  49. appConfig.HotkeyModifiers = 0;
  50. hotkeyFinished = false;
  51. }
  52. //首次按下按键
  53. if (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0)
  54. {
  55. if (CheckModifierKeys(e))
  56. {
  57. //辅助键
  58. appConfig.HotkeyStr = GetKeyName(e);
  59. appConfig.HotkeyModifiers = GetModifierKeys(e);
  60. prevKeyTemp = e;
  61. }
  62. }
  63. else
  64. {
  65. //非首次按下 需要判断前一个键值是否为辅助键
  66. if (CheckModifierKeys(prevKeyTemp)
  67. && ((e.Key >= Key.A && e.Key <= Key.Z)
  68. || (e.Key >= Key.F1 && e.Key <= Key.F12)
  69. || (e.Key >= Key.D0 && e.Key <= Key.D9)))
  70. {
  71. appConfig.Hotkey = e.Key;
  72. appConfig.HotkeyStr += e.Key.ToString();
  73. prevKeyTemp = e;
  74. }
  75. else if (CheckModifierKeys(e))
  76. {
  77. appConfig.HotkeyStr += GetKeyName(e);
  78. appConfig.HotkeyModifiers |= GetModifierKeys(e);
  79. prevKeyTemp = e;
  80. }
  81. }
  82. }
  83. }
  84. private string GetKeyName(KeyEventArgs e)
  85. {
  86. Key key = e.Key;
  87. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  88. {
  89. return "Ctrl + ";
  90. } else if (key == Key.LWin || key == Key.RWin)
  91. {
  92. return "Win + ";
  93. }
  94. else if (key == Key.LeftShift || key == Key.RightShift)
  95. {
  96. return "Shift + ";
  97. }
  98. else
  99. {
  100. return "Alt + ";
  101. }
  102. }
  103. private HotkeyModifiers GetModifierKeys(KeyEventArgs e)
  104. {
  105. Key key = e.Key;
  106. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  107. {
  108. return HotkeyModifiers.MOD_CONTROL;
  109. }
  110. else if (key == Key.LWin || key == Key.RWin)
  111. {
  112. return HotkeyModifiers.MOD_WIN;
  113. }
  114. else if (key == Key.LeftShift || key == Key.RightShift)
  115. {
  116. return HotkeyModifiers.MOD_SHIFT;
  117. }
  118. else
  119. {
  120. return HotkeyModifiers.MOD_ALT;
  121. }
  122. }
  123. private bool CheckModifierKeys(KeyEventArgs e)
  124. {
  125. Key key = e.Key;
  126. return key == Key.LeftCtrl || key == Key.RightCtrl
  127. || key == Key.LWin || key == Key.RWin
  128. || key == Key.LeftShift || key == Key.RightShift
  129. || key == Key.LeftAlt || key == Key.RightAlt;
  130. }
  131. private void HotKeyUp(object sender, KeyEventArgs e)
  132. {
  133. hotkeyFinished = true;
  134. ConfigWindow cw = (ConfigWindow)Window.GetWindow(this);
  135. try
  136. {
  137. if (cw.mainWindow.hotKeyId != -1)
  138. {
  139. Hotkey.UnRegist(new WindowInteropHelper(cw.mainWindow).Handle, Hotkey.keymap[cw.mainWindow.hotKeyId]);
  140. }
  141. cw.mainWindow.hotKeyId = Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () =>
  142. {
  143. if (cw.mainWindow.Visibility == Visibility.Collapsed)
  144. {
  145. cw.mainWindow.ShowApp();
  146. }
  147. else
  148. {
  149. cw.mainWindow.Visibility = Visibility.Collapsed;
  150. }
  151. });
  152. } catch (Exception)
  153. {
  154. HandyControl.Controls.Growl.WarningGlobal("当前快捷键已被其它程序占用(" + appConfig.HotkeyStr + ")!");
  155. }
  156. }
  157. private void ShowApp(MainWindow mainWindow)
  158. {
  159. if (appConfig.FollowMouse)
  160. {
  161. ShowAppAndFollowMouse(mainWindow);
  162. }
  163. else
  164. {
  165. this.Visibility = Visibility.Visible;
  166. }
  167. Keyboard.Focus(this);
  168. }
  169. /// <summary>
  170. /// 随鼠标位置显示面板 (鼠标始终在中间)
  171. /// </summary>
  172. private void ShowAppAndFollowMouse(MainWindow mainWindow)
  173. {
  174. //获取鼠标位置
  175. System.Windows.Point p = MouseUtil.GetMousePosition();
  176. double left = SystemParameters.VirtualScreenLeft;
  177. double top = SystemParameters.VirtualScreenTop;
  178. double width = SystemParameters.VirtualScreenWidth;
  179. double height = SystemParameters.VirtualScreenHeight;
  180. double right = width - Math.Abs(left);
  181. double bottom = height - Math.Abs(top);
  182. if (p.X - mainWindow.Width / 2 < left)
  183. {
  184. //判断是否在最左边缘
  185. mainWindow.Left = left;
  186. }
  187. else if (p.X + mainWindow.Width / 2 > right)
  188. {
  189. //判断是否在最右边缘
  190. mainWindow.Left = right - mainWindow.Width;
  191. }
  192. else
  193. {
  194. mainWindow.Left = p.X - mainWindow.Width / 2;
  195. }
  196. if (p.Y - mainWindow.Height / 2 < top)
  197. {
  198. //判断是否在最上边缘
  199. mainWindow.Top = top;
  200. }
  201. else if (p.Y + mainWindow.Height / 2 > bottom)
  202. {
  203. //判断是否在最下边缘
  204. mainWindow.Top = bottom - mainWindow.Height;
  205. }
  206. else
  207. {
  208. mainWindow.Top = p.Y - mainWindow.Height / 2;
  209. }
  210. mainWindow.Visibility = Visibility.Visible;
  211. }
  212. }
  213. }