1
0

MotionControl.xaml.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Windows;
  3. using GeekDesk.Thread;
  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. } else
  68. {
  69. appConfig.ToDoHotkey = 0;
  70. appConfig.ToDoHotkeyStr = "";
  71. appConfig.ToDoHotkeyModifiers = 0;
  72. }
  73. hotkeyFinished = false;
  74. }
  75. //首次按下按键
  76. if ((main && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0))
  77. || (!main && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0)))
  78. {
  79. if (CheckModifierKeys(downKey))
  80. {
  81. //辅助键
  82. if (main)
  83. {
  84. appConfig.HotkeyStr = GetKeyName(downKey);
  85. appConfig.HotkeyModifiers = GetModifierKeys(downKey);
  86. } else
  87. {
  88. appConfig.ToDoHotkeyStr = GetKeyName(downKey);
  89. appConfig.ToDoHotkeyModifiers = GetModifierKeys(downKey);
  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. {
  103. if (main)
  104. {
  105. appConfig.Hotkey = downKey;
  106. appConfig.HotkeyStr += downKey.ToString();
  107. } else
  108. {
  109. appConfig.ToDoHotkey = downKey;
  110. appConfig.ToDoHotkeyStr += downKey.ToString();
  111. }
  112. prevKeyTemp = downKey;
  113. keysTemp.Add(e);
  114. }
  115. else if (CheckModifierKeys(downKey))
  116. {
  117. if (main)
  118. {
  119. appConfig.HotkeyStr += GetKeyName(downKey);
  120. appConfig.HotkeyModifiers |= GetModifierKeys(downKey);
  121. } else
  122. {
  123. appConfig.ToDoHotkeyStr += GetKeyName(downKey);
  124. appConfig.ToDoHotkeyModifiers |= GetModifierKeys(downKey);
  125. }
  126. prevKeyTemp = downKey;
  127. keysTemp.Add(e);
  128. }
  129. }
  130. }
  131. }
  132. private string GetKeyName(Key key)
  133. {
  134. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  135. {
  136. return "Ctrl + ";
  137. } else if (key == Key.LWin || key == Key.RWin)
  138. {
  139. return "Win + ";
  140. }
  141. else if (key == Key.LeftShift || key == Key.RightShift)
  142. {
  143. return "Shift + ";
  144. }
  145. else
  146. {
  147. return "Alt + ";
  148. }
  149. }
  150. private HotkeyModifiers GetModifierKeys(Key key)
  151. {
  152. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  153. {
  154. return HotkeyModifiers.MOD_CONTROL;
  155. }
  156. else if (key == Key.LWin || key == Key.RWin)
  157. {
  158. return HotkeyModifiers.MOD_WIN;
  159. }
  160. else if (key == Key.LeftShift || key == Key.RightShift)
  161. {
  162. return HotkeyModifiers.MOD_SHIFT;
  163. }
  164. else
  165. {
  166. return HotkeyModifiers.MOD_ALT;
  167. }
  168. }
  169. private bool CheckModifierKeys(Key key)
  170. {
  171. return key == Key.LeftCtrl || key == Key.RightCtrl
  172. || key == Key.LWin || key == Key.RWin
  173. || key == Key.LeftShift || key == Key.RightShift
  174. || key == Key.LeftAlt || key == Key.RightAlt;
  175. }
  176. [MethodImpl(MethodImplOptions.Synchronized)]
  177. private void HotKeyUp(object sender, KeyEventArgs e)
  178. {
  179. string tag = (sender as TextBox).Tag.ToString();
  180. bool main = false;
  181. if ("Main".Equals(tag))
  182. {
  183. main = true;
  184. }
  185. lock(this)
  186. {
  187. bool allKeyUp = true;
  188. //判断所有键是否都松开
  189. foreach (KeyEventArgs key in keysTemp)
  190. {
  191. if (key.KeyStates == KeyStates.Down)
  192. {
  193. allKeyUp = false;
  194. break;
  195. }
  196. }
  197. if (allKeyUp && !hotkeyFinished)
  198. {
  199. keysTemp.Clear();
  200. prevKeyTemp = Key.None;
  201. hotkeyFinished = true;
  202. if (main)
  203. {
  204. if (MainWindow.hotKeyId != -1)
  205. {
  206. //Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]);
  207. GlobalHotKey.Dispose(MainWindow.hotKeyId);
  208. }
  209. MainWindow.RegisterHotKey(false);
  210. } else
  211. {
  212. if (MainWindow.toDoHotKeyId != -1)
  213. {
  214. //Hotkey.UnRegist(new WindowInteropHelper(MainWindow.toDoInfoWindow).Handle, Hotkey.keymap[MainWindow.toDoHotKeyId]);
  215. GlobalHotKey.Dispose(MainWindow.toDoHotKeyId);
  216. }
  217. MainWindow.RegisterCreateToDoHotKey(false);
  218. }
  219. }
  220. }
  221. }
  222. /// <summary>
  223. /// 移动窗口
  224. /// </summary>
  225. /// <param name="sender"></param>
  226. /// <param name="e"></param>
  227. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  228. {
  229. if (e.LeftButton == MouseButtonState.Pressed)
  230. {
  231. Window.GetWindow(this).DragMove();
  232. }
  233. }
  234. private void MarginHide_Changed(object sender, RoutedEventArgs e)
  235. {
  236. if (appConfig.MarginHide)
  237. {
  238. MainWindow.hide.TimerSet();
  239. } else
  240. {
  241. if (MainWindow.hide.timer != null)
  242. {
  243. MainWindow.hide.TimerStop();
  244. }
  245. }
  246. }
  247. private void Animation_Checked(object sender, RoutedEventArgs e)
  248. {
  249. if (MainWindow.mainWindow.Visibility == Visibility.Collapsed)
  250. {
  251. MainWindow.mainWindow.Visibility = Visibility.Visible;
  252. // 执行一下动画 防止太过突兀
  253. MainWindow.FadeStoryBoard(0, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Collapsed);
  254. }
  255. }
  256. /// <summary>
  257. /// 鼠标中键呼出 change
  258. /// </summary>
  259. /// <param name="sender"></param>
  260. /// <param name="e"></param>
  261. private void MouseMiddle_Changed(object sender, RoutedEventArgs e)
  262. {
  263. if (appConfig.MouseMiddleShow)
  264. {
  265. MouseHookThread.MiddleHook();
  266. } else
  267. {
  268. MouseHookThread.Dispose();
  269. }
  270. }
  271. }
  272. }