MotionControl.xaml.cs 9.7 KB

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