MotionControl.xaml.cs 15 KB

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