MotionControl.xaml.cs 9.1 KB

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