MotionControl.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using GeekDesk.Control.Windows;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using HandyControl.Data;
  5. using Microsoft.Win32;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  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. public static bool hotkeyFinished = true; //热键设置结束
  30. private static KeyEventArgs prevKeyTemp; //上一个按键
  31. private static List<KeyEventArgs> keysTemp = new List<KeyEventArgs>();//存储一次快捷键集合
  32. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  33. public MotionControl()
  34. {
  35. InitializeComponent();
  36. }
  37. /// <summary>
  38. /// 注册热键按下
  39. /// </summary>
  40. /// <param name="sender"></param>
  41. /// <param name="e"></param>
  42. private void HotKeyDown(object sender, KeyEventArgs e)
  43. {
  44. string tag = (sender as TextBox).Tag.ToString();
  45. bool main = false;
  46. if ("Main".Equals(tag))
  47. {
  48. main = true;
  49. }
  50. if (!e.IsRepeat)
  51. {
  52. if (hotkeyFinished)
  53. {
  54. if (main)
  55. {
  56. appConfig.Hotkey = 0;
  57. appConfig.HotkeyStr = "";
  58. appConfig.HotkeyModifiers = 0;
  59. } else
  60. {
  61. appConfig.ToDoHotkey = 0;
  62. appConfig.ToDoHotkeyStr = "";
  63. appConfig.ToDoHotkeyModifiers = 0;
  64. }
  65. hotkeyFinished = false;
  66. }
  67. //首次按下按键
  68. if ((main && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0))
  69. || (!main && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0)))
  70. {
  71. if (CheckModifierKeys(e))
  72. {
  73. //辅助键
  74. if (main)
  75. {
  76. appConfig.HotkeyStr = GetKeyName(e);
  77. appConfig.HotkeyModifiers = GetModifierKeys(e);
  78. } else
  79. {
  80. appConfig.ToDoHotkeyStr = GetKeyName(e);
  81. appConfig.ToDoHotkeyModifiers = GetModifierKeys(e);
  82. }
  83. prevKeyTemp = e;
  84. keysTemp.Add(e);
  85. }
  86. }
  87. else
  88. {
  89. //非首次按下 需要判断前一个键值是否为辅助键
  90. if (CheckModifierKeys(prevKeyTemp)
  91. && ((e.Key >= Key.A && e.Key <= Key.Z)
  92. || (e.Key >= Key.F1 && e.Key <= Key.F12)
  93. || (e.Key >= Key.D0 && e.Key <= Key.D9)))
  94. {
  95. if (main)
  96. {
  97. appConfig.Hotkey = e.Key;
  98. appConfig.HotkeyStr += e.Key.ToString();
  99. } else
  100. {
  101. appConfig.ToDoHotkey = e.Key;
  102. appConfig.ToDoHotkeyStr += e.Key.ToString();
  103. }
  104. prevKeyTemp = e;
  105. keysTemp.Add(e);
  106. }
  107. else if (CheckModifierKeys(e))
  108. {
  109. if (main)
  110. {
  111. appConfig.HotkeyStr += GetKeyName(e);
  112. appConfig.HotkeyModifiers |= GetModifierKeys(e);
  113. } else
  114. {
  115. appConfig.ToDoHotkeyStr += GetKeyName(e);
  116. appConfig.ToDoHotkeyModifiers |= GetModifierKeys(e);
  117. }
  118. prevKeyTemp = e;
  119. keysTemp.Add(e);
  120. }
  121. }
  122. }
  123. }
  124. private string GetKeyName(KeyEventArgs e)
  125. {
  126. Key key = e.Key;
  127. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  128. {
  129. return "Ctrl + ";
  130. } else if (key == Key.LWin || key == Key.RWin)
  131. {
  132. return "Win + ";
  133. }
  134. else if (key == Key.LeftShift || key == Key.RightShift)
  135. {
  136. return "Shift + ";
  137. }
  138. else
  139. {
  140. return "Alt + ";
  141. }
  142. }
  143. private HotkeyModifiers GetModifierKeys(KeyEventArgs e)
  144. {
  145. Key key = e.Key;
  146. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  147. {
  148. return HotkeyModifiers.MOD_CONTROL;
  149. }
  150. else if (key == Key.LWin || key == Key.RWin)
  151. {
  152. return HotkeyModifiers.MOD_WIN;
  153. }
  154. else if (key == Key.LeftShift || key == Key.RightShift)
  155. {
  156. return HotkeyModifiers.MOD_SHIFT;
  157. }
  158. else
  159. {
  160. return HotkeyModifiers.MOD_ALT;
  161. }
  162. }
  163. private bool CheckModifierKeys(KeyEventArgs e)
  164. {
  165. Key key = e.Key;
  166. return key == Key.LeftCtrl || key == Key.RightCtrl
  167. || key == Key.LWin || key == Key.RWin
  168. || key == Key.LeftShift || key == Key.RightShift
  169. || key == Key.LeftAlt || key == Key.RightAlt;
  170. }
  171. [MethodImpl(MethodImplOptions.Synchronized)]
  172. private void HotKeyUp(object sender, KeyEventArgs e)
  173. {
  174. string tag = (sender as TextBox).Tag.ToString();
  175. bool main = false;
  176. if ("Main".Equals(tag))
  177. {
  178. main = true;
  179. }
  180. lock(this)
  181. {
  182. bool allKeyUp = true;
  183. //判断所有键是否都松开
  184. foreach (KeyEventArgs key in keysTemp)
  185. {
  186. if (key.KeyStates == KeyStates.Down)
  187. {
  188. allKeyUp = false;
  189. break;
  190. }
  191. }
  192. if (allKeyUp && !hotkeyFinished)
  193. {
  194. keysTemp.Clear();
  195. hotkeyFinished = true;
  196. if (main)
  197. {
  198. if (MainWindow.hotKeyId != -1)
  199. {
  200. Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]);
  201. }
  202. MainWindow.RegisterHotKey(false);
  203. } else
  204. {
  205. if (MainWindow.toDoHotKeyId != -1)
  206. {
  207. Hotkey.UnRegist(new WindowInteropHelper(MainWindow.toDoInfoWindow).Handle, Hotkey.keymap[MainWindow.toDoHotKeyId]);
  208. }
  209. MainWindow.RegisterCreateToDoHotKey(false);
  210. }
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 移动窗口
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  220. {
  221. if (e.LeftButton == MouseButtonState.Pressed)
  222. {
  223. Window.GetWindow(this).DragMove();
  224. }
  225. }
  226. //private void ShowApp(MainWindow mainWindow)
  227. //{
  228. // if (appConfig.FollowMouse)
  229. // {
  230. // ShowAppAndFollowMouse(mainWindow);
  231. // }
  232. // else
  233. // {
  234. // this.Visibility = Visibility.Visible;
  235. // }
  236. // Keyboard.Focus(this);
  237. //}
  238. ///// <summary>
  239. ///// 随鼠标位置显示面板 (鼠标始终在中间)
  240. ///// </summary>
  241. //private void ShowAppAndFollowMouse(MainWindow mainWindow)
  242. //{
  243. // //获取鼠标位置
  244. // System.Windows.Point p = MouseUtil.GetMousePosition();
  245. // double left = SystemParameters.VirtualScreenLeft;
  246. // double top = SystemParameters.VirtualScreenTop;
  247. // double width = SystemParameters.VirtualScreenWidth;
  248. // double height = SystemParameters.VirtualScreenHeight;
  249. // double right = width - Math.Abs(left);
  250. // double bottom = height - Math.Abs(top);
  251. // if (p.X - mainWindow.Width / 2 < left)
  252. // {
  253. // //判断是否在最左边缘
  254. // mainWindow.Left = left;
  255. // }
  256. // else if (p.X + mainWindow.Width / 2 > right)
  257. // {
  258. // //判断是否在最右边缘
  259. // mainWindow.Left = right - mainWindow.Width;
  260. // }
  261. // else
  262. // {
  263. // mainWindow.Left = p.X - mainWindow.Width / 2;
  264. // }
  265. // if (p.Y - mainWindow.Height / 2 < top)
  266. // {
  267. // //判断是否在最上边缘
  268. // mainWindow.Top = top;
  269. // }
  270. // else if (p.Y + mainWindow.Height / 2 > bottom)
  271. // {
  272. // //判断是否在最下边缘
  273. // mainWindow.Top = bottom - mainWindow.Height;
  274. // }
  275. // else
  276. // {
  277. // mainWindow.Top = p.Y - mainWindow.Height / 2;
  278. // }
  279. // mainWindow.Visibility = Visibility.Visible;
  280. //}
  281. }
  282. }