MotionControl.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using GeekDesk.Constant;
  2. using GeekDesk.MyThread;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using System.Collections.Generic;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using static GeekDesk.Util.GlobalHotKey;
  11. namespace GeekDesk.Control.UserControls.Config
  12. {
  13. /// <summary>
  14. /// 动作设置
  15. /// </summary>
  16. public partial class MotionControl : UserControl
  17. {
  18. public static bool hotkeyFinished = true; //热键设置结束
  19. private Key prevKeyTemp = Key.None; //上一个按键
  20. private readonly List<KeyEventArgs> keysTemp = new List<KeyEventArgs>();//存储一次快捷键集合
  21. private readonly AppConfig appConfig = MainWindow.appData.AppConfig;
  22. public MotionControl()
  23. {
  24. InitializeComponent();
  25. }
  26. /// <summary>
  27. /// 注册热键按下
  28. /// </summary>
  29. /// <param name="sender"></param>
  30. /// <param name="e"></param>
  31. private void HotKeyDown(object sender, KeyEventArgs e)
  32. {
  33. HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag;
  34. Key downKey = e.Key;
  35. if (downKey == Key.System)
  36. {
  37. downKey = e.SystemKey;
  38. }
  39. if (!CheckIsEnable(hkType)) return;
  40. if (prevKeyTemp == Key.None || prevKeyTemp != downKey)
  41. {
  42. if (hotkeyFinished)
  43. {
  44. switch (hkType)
  45. {
  46. case HotKeyType.Main:
  47. appConfig.Hotkey = 0;
  48. appConfig.HotkeyStr = "";
  49. appConfig.HotkeyModifiers = 0;
  50. break;
  51. case HotKeyType.ToDo:
  52. appConfig.ToDoHotkey = 0;
  53. appConfig.ToDoHotkeyStr = "";
  54. appConfig.ToDoHotkeyModifiers = 0;
  55. break;
  56. case HotKeyType.ColorPicker:
  57. appConfig.ColorPickerHotkey = 0;
  58. appConfig.ColorPickerHotkeyStr = "";
  59. appConfig.ColorPickerHotkeyModifiers = 0;
  60. break;
  61. }
  62. hotkeyFinished = false;
  63. }
  64. //首次按下按键
  65. if ((HotKeyType.Main == hkType && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0))
  66. || (HotKeyType.ToDo == hkType && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0))
  67. || (HotKeyType.ColorPicker == hkType && (appConfig.ColorPickerHotkeyStr == null || appConfig.ColorPickerHotkeyStr.Length == 0))
  68. )
  69. {
  70. if (CheckModifierKeys(downKey))
  71. {
  72. //辅助键
  73. switch (hkType)
  74. {
  75. case HotKeyType.Main:
  76. appConfig.HotkeyStr = GetKeyName(downKey);
  77. appConfig.HotkeyModifiers = GetModifierKeys(downKey);
  78. break;
  79. case HotKeyType.ToDo:
  80. appConfig.ToDoHotkeyStr = GetKeyName(downKey);
  81. appConfig.ToDoHotkeyModifiers = GetModifierKeys(downKey);
  82. break;
  83. case HotKeyType.ColorPicker:
  84. appConfig.ColorPickerHotkeyStr = GetKeyName(downKey);
  85. appConfig.ColorPickerHotkeyModifiers = GetModifierKeys(downKey);
  86. break;
  87. }
  88. prevKeyTemp = downKey;
  89. keysTemp.Add(e);
  90. }
  91. }
  92. else
  93. {
  94. //非首次按下 需要判断前一个键值是否为辅助键
  95. if (CheckModifierKeys(prevKeyTemp)
  96. && ((downKey >= Key.A && downKey <= Key.Z)
  97. || (downKey >= Key.F1 && downKey <= Key.F12)
  98. || (downKey >= Key.D0 && downKey <= Key.D9)))
  99. {
  100. switch (hkType)
  101. {
  102. case HotKeyType.Main:
  103. appConfig.Hotkey = downKey;
  104. appConfig.HotkeyStr += downKey.ToString();
  105. break;
  106. case HotKeyType.ToDo:
  107. appConfig.ToDoHotkey = downKey;
  108. appConfig.ToDoHotkeyStr += downKey.ToString();
  109. break;
  110. case HotKeyType.ColorPicker:
  111. appConfig.ColorPickerHotkey = downKey;
  112. appConfig.ColorPickerHotkeyStr += downKey.ToString();
  113. break;
  114. }
  115. prevKeyTemp = downKey;
  116. keysTemp.Add(e);
  117. }
  118. else if (CheckModifierKeys(downKey))
  119. {
  120. switch (hkType)
  121. {
  122. case HotKeyType.Main:
  123. appConfig.HotkeyStr += GetKeyName(downKey);
  124. appConfig.HotkeyModifiers |= GetModifierKeys(downKey);
  125. break;
  126. case HotKeyType.ToDo:
  127. appConfig.ToDoHotkeyStr += GetKeyName(downKey);
  128. appConfig.ToDoHotkeyModifiers |= GetModifierKeys(downKey);
  129. break;
  130. case HotKeyType.ColorPicker:
  131. appConfig.ColorPickerHotkeyStr += GetKeyName(downKey);
  132. appConfig.ColorPickerHotkeyModifiers |= GetModifierKeys(downKey);
  133. break;
  134. }
  135. prevKeyTemp = downKey;
  136. keysTemp.Add(e);
  137. }
  138. }
  139. }
  140. }
  141. private string GetKeyName(Key key)
  142. {
  143. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  144. {
  145. return "Ctrl + ";
  146. }
  147. else if (key == Key.LWin || key == Key.RWin)
  148. {
  149. return "Win + ";
  150. }
  151. else if (key == Key.LeftShift || key == Key.RightShift)
  152. {
  153. return "Shift + ";
  154. }
  155. else
  156. {
  157. return "Alt + ";
  158. }
  159. }
  160. private HotkeyModifiers GetModifierKeys(Key key)
  161. {
  162. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  163. {
  164. return HotkeyModifiers.MOD_CONTROL;
  165. }
  166. else if (key == Key.LWin || key == Key.RWin)
  167. {
  168. return HotkeyModifiers.MOD_WIN;
  169. }
  170. else if (key == Key.LeftShift || key == Key.RightShift)
  171. {
  172. return HotkeyModifiers.MOD_SHIFT;
  173. }
  174. else
  175. {
  176. return HotkeyModifiers.MOD_ALT;
  177. }
  178. }
  179. private bool CheckModifierKeys(Key key)
  180. {
  181. return key == Key.LeftCtrl || key == Key.RightCtrl
  182. || key == Key.LWin || key == Key.RWin
  183. || key == Key.LeftShift || key == Key.RightShift
  184. || key == Key.LeftAlt || key == Key.RightAlt;
  185. }
  186. [MethodImpl(MethodImplOptions.Synchronized)]
  187. private void HotKeyUp(object sender, KeyEventArgs e)
  188. {
  189. HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag;
  190. if (!CheckIsEnable(hkType)) return;
  191. lock (this)
  192. {
  193. bool allKeyUp = true;
  194. //判断所有键是否都松开
  195. foreach (KeyEventArgs key in keysTemp)
  196. {
  197. if (key.KeyStates == KeyStates.Down)
  198. {
  199. allKeyUp = false;
  200. break;
  201. }
  202. }
  203. if (allKeyUp && !hotkeyFinished)
  204. {
  205. keysTemp.Clear();
  206. prevKeyTemp = Key.None;
  207. hotkeyFinished = true;
  208. switch (hkType)
  209. {
  210. case HotKeyType.Main:
  211. if (MainWindow.hotKeyId != -1)
  212. {
  213. //Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]);
  214. GlobalHotKey.Dispose(MainWindow.hotKeyId);
  215. }
  216. MainWindow.RegisterHotKey(false);
  217. break;
  218. case HotKeyType.ToDo:
  219. if (MainWindow.toDoHotKeyId != -1)
  220. {
  221. //Hotkey.UnRegist(new WindowInteropHelper(MainWindow.toDoInfoWindow).Handle, Hotkey.keymap[MainWindow.toDoHotKeyId]);
  222. GlobalHotKey.Dispose(MainWindow.toDoHotKeyId);
  223. }
  224. MainWindow.RegisterCreateToDoHotKey(false);
  225. break;
  226. case HotKeyType.ColorPicker:
  227. if (MainWindow.colorPickerHotKeyId != -1)
  228. {
  229. //Hotkey.UnRegist(new WindowInteropHelper(MainWindow.toDoInfoWindow).Handle, Hotkey.keymap[MainWindow.toDoHotKeyId]);
  230. GlobalHotKey.Dispose(MainWindow.colorPickerHotKeyId);
  231. }
  232. MainWindow.RegisterColorPickerHotKey(false);
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. private bool CheckIsEnable(HotKeyType hkType)
  239. {
  240. switch (hkType)
  241. {
  242. case HotKeyType.Main:
  243. return true == appConfig.EnableAppHotKey;
  244. case HotKeyType.ToDo:
  245. return true == appConfig.EnableTodoHotKey;
  246. case HotKeyType.ColorPicker:
  247. return true == appConfig.EnableColorPickerHotKey;
  248. }
  249. return false;
  250. }
  251. /// <summary>
  252. /// 移动窗口
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  257. {
  258. if (e.LeftButton == MouseButtonState.Pressed)
  259. {
  260. Window.GetWindow(this).DragMove();
  261. }
  262. }
  263. private void MarginHide_Changed(object sender, RoutedEventArgs e)
  264. {
  265. if (appConfig.MarginHide)
  266. {
  267. MarginHide.StartHide();
  268. }
  269. else
  270. {
  271. MarginHide.StopHide();
  272. }
  273. }
  274. private void Animation_Checked(object sender, RoutedEventArgs e)
  275. {
  276. if (MainWindow.mainWindow.Visibility == Visibility.Collapsed)
  277. {
  278. MainWindow.mainWindow.Visibility = Visibility.Visible;
  279. // 执行一下动画 防止太过突兀
  280. MainWindow.FadeStoryBoard(0, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Collapsed);
  281. }
  282. }
  283. /// <summary>
  284. /// 鼠标中键呼出 change
  285. /// </summary>
  286. /// <param name="sender"></param>
  287. /// <param name="e"></param>
  288. private void MouseMiddle_Changed(object sender, RoutedEventArgs e)
  289. {
  290. if (appConfig.MouseMiddleShow)
  291. {
  292. MouseHookThread.MiddleHook();
  293. }
  294. else
  295. {
  296. MouseHookThread.Dispose();
  297. }
  298. }
  299. /// <summary>
  300. /// 启用热键
  301. /// </summary>
  302. /// <param name="sender"></param>
  303. /// <param name="e"></param>
  304. private void EnableHotKey_Click(object sender, RoutedEventArgs e)
  305. {
  306. HotKeyType hkType = (HotKeyType)(sender as CheckBox).Tag;
  307. switch (hkType)
  308. {
  309. case HotKeyType.Main:
  310. if (true == appConfig.EnableAppHotKey)
  311. MainWindow.RegisterHotKey(false);
  312. break;
  313. case HotKeyType.ToDo:
  314. if (true == appConfig.EnableTodoHotKey)
  315. MainWindow.RegisterCreateToDoHotKey(false);
  316. break;
  317. case HotKeyType.ColorPicker:
  318. if (true == appConfig.EnableColorPickerHotKey)
  319. MainWindow.RegisterColorPickerHotKey(false);
  320. break;
  321. }
  322. }
  323. }
  324. }