MotionControl.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using GeekDesk.Control.Windows;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using GlobalHotKey;
  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. namespace GeekDesk.Control.UserControls.Config
  24. {
  25. /// <summary>
  26. /// 动作设置
  27. /// </summary>
  28. public partial class MotionControl : UserControl
  29. {
  30. public static bool hotkeyFinished = true; //热键设置结束
  31. private static KeyEventArgs prevKeyTemp; //上一个按键
  32. private static List<KeyEventArgs> keysTemp = new List<KeyEventArgs>();//存储一次快捷键集合
  33. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  34. public MotionControl()
  35. {
  36. InitializeComponent();
  37. }
  38. /// <summary>
  39. /// 注册热键按下
  40. /// </summary>
  41. /// <param name="sender"></param>
  42. /// <param name="e"></param>
  43. private void HotKeyDown(object sender, KeyEventArgs e)
  44. {
  45. if (!e.IsRepeat)
  46. {
  47. if (hotkeyFinished)
  48. {
  49. appConfig.Hotkey = 0;
  50. appConfig.HotkeyStr = "";
  51. appConfig.HotkeyModifiers = 0;
  52. hotkeyFinished = false;
  53. }
  54. //首次按下按键
  55. if (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0)
  56. {
  57. if (CheckModifierKeys(e))
  58. {
  59. //辅助键
  60. appConfig.HotkeyStr = GetKeyName(e);
  61. appConfig.HotkeyModifiers = GetModifierKeys(e);
  62. prevKeyTemp = e;
  63. keysTemp.Add(e);
  64. }
  65. }
  66. else
  67. {
  68. //非首次按下 需要判断前一个键值是否为辅助键
  69. if (CheckModifierKeys(prevKeyTemp)
  70. && ((e.Key >= Key.A && e.Key <= Key.Z)
  71. || (e.Key >= Key.F1 && e.Key <= Key.F12)
  72. || (e.Key >= Key.D0 && e.Key <= Key.D9)))
  73. {
  74. appConfig.Hotkey = e.Key;
  75. appConfig.HotkeyStr += e.Key.ToString();
  76. prevKeyTemp = e;
  77. keysTemp.Add(e);
  78. }
  79. else if (CheckModifierKeys(e))
  80. {
  81. appConfig.HotkeyStr += GetKeyName(e);
  82. appConfig.HotkeyModifiers |= GetModifierKeys(e);
  83. prevKeyTemp = e;
  84. keysTemp.Add(e);
  85. }
  86. }
  87. }
  88. }
  89. private string GetKeyName(KeyEventArgs e)
  90. {
  91. Key key = e.Key;
  92. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  93. {
  94. return "Ctrl + ";
  95. } else if (key == Key.LWin || key == Key.RWin)
  96. {
  97. return "Win + ";
  98. }
  99. else if (key == Key.LeftShift || key == Key.RightShift)
  100. {
  101. return "Shift + ";
  102. }
  103. else
  104. {
  105. return "Alt + ";
  106. }
  107. }
  108. private HotkeyModifiers GetModifierKeys(KeyEventArgs e)
  109. {
  110. Key key = e.Key;
  111. if (key == Key.LeftCtrl || key == Key.RightCtrl)
  112. {
  113. return HotkeyModifiers.MOD_CONTROL;
  114. }
  115. else if (key == Key.LWin || key == Key.RWin)
  116. {
  117. return HotkeyModifiers.MOD_WIN;
  118. }
  119. else if (key == Key.LeftShift || key == Key.RightShift)
  120. {
  121. return HotkeyModifiers.MOD_SHIFT;
  122. }
  123. else
  124. {
  125. return HotkeyModifiers.MOD_ALT;
  126. }
  127. }
  128. private bool CheckModifierKeys(KeyEventArgs e)
  129. {
  130. Key key = e.Key;
  131. return key == Key.LeftCtrl || key == Key.RightCtrl
  132. || key == Key.LWin || key == Key.RWin
  133. || key == Key.LeftShift || key == Key.RightShift
  134. || key == Key.LeftAlt || key == Key.RightAlt;
  135. }
  136. [MethodImpl(MethodImplOptions.Synchronized)]
  137. private void HotKeyUp(object sender, KeyEventArgs e)
  138. {
  139. lock(this)
  140. {
  141. bool allKeyUp = true;
  142. //判断所有键是否都松开
  143. foreach (KeyEventArgs key in keysTemp)
  144. {
  145. HandyControl.Controls.Growl.SuccessGlobal(key.Key.ToString() + "=" + key.KeyStates);
  146. if (key.KeyStates == KeyStates.Down)
  147. {
  148. allKeyUp = false;
  149. break;
  150. }
  151. }
  152. if (allKeyUp && !hotkeyFinished)
  153. {
  154. hotkeyFinished = true;
  155. if (MainWindow.hotKeyId != -1)
  156. {
  157. Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]);
  158. }
  159. MainWindow.RegisterHotKey();
  160. }
  161. }
  162. }
  163. //private void ShowApp(MainWindow mainWindow)
  164. //{
  165. // if (appConfig.FollowMouse)
  166. // {
  167. // ShowAppAndFollowMouse(mainWindow);
  168. // }
  169. // else
  170. // {
  171. // this.Visibility = Visibility.Visible;
  172. // }
  173. // Keyboard.Focus(this);
  174. //}
  175. ///// <summary>
  176. ///// 随鼠标位置显示面板 (鼠标始终在中间)
  177. ///// </summary>
  178. //private void ShowAppAndFollowMouse(MainWindow mainWindow)
  179. //{
  180. // //获取鼠标位置
  181. // System.Windows.Point p = MouseUtil.GetMousePosition();
  182. // double left = SystemParameters.VirtualScreenLeft;
  183. // double top = SystemParameters.VirtualScreenTop;
  184. // double width = SystemParameters.VirtualScreenWidth;
  185. // double height = SystemParameters.VirtualScreenHeight;
  186. // double right = width - Math.Abs(left);
  187. // double bottom = height - Math.Abs(top);
  188. // if (p.X - mainWindow.Width / 2 < left)
  189. // {
  190. // //判断是否在最左边缘
  191. // mainWindow.Left = left;
  192. // }
  193. // else if (p.X + mainWindow.Width / 2 > right)
  194. // {
  195. // //判断是否在最右边缘
  196. // mainWindow.Left = right - mainWindow.Width;
  197. // }
  198. // else
  199. // {
  200. // mainWindow.Left = p.X - mainWindow.Width / 2;
  201. // }
  202. // if (p.Y - mainWindow.Height / 2 < top)
  203. // {
  204. // //判断是否在最上边缘
  205. // mainWindow.Top = top;
  206. // }
  207. // else if (p.Y + mainWindow.Height / 2 > bottom)
  208. // {
  209. // //判断是否在最下边缘
  210. // mainWindow.Top = bottom - mainWindow.Height;
  211. // }
  212. // else
  213. // {
  214. // mainWindow.Top = p.Y - mainWindow.Height / 2;
  215. // }
  216. // mainWindow.Visibility = Visibility.Visible;
  217. //}
  218. }
  219. }