Keyboard.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using CommonLang;
  8. namespace CommonFroms
  9. {
  10. public static class Keyboard
  11. {
  12. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  13. public static extern short GetKeyState(int virtualKeyCode);
  14. public const int VK_SHIFT = 0x10;
  15. public const int VK_CTRL = 0x11;
  16. public const int VK_ALT = 0x12;
  17. public static bool IsShiftDown
  18. {
  19. get
  20. {
  21. return (GetKeyState(VK_SHIFT) & 0x80) != 0;
  22. }
  23. }
  24. public static bool IsCtrlDown
  25. {
  26. get
  27. {
  28. return (GetKeyState(VK_CTRL) & 0x80) != 0;
  29. }
  30. }
  31. public static bool IsAltDown
  32. {
  33. get
  34. {
  35. return (GetKeyState(VK_ALT) & 0x80) != 0;
  36. }
  37. }
  38. public static bool GetKeyState(System.Windows.Forms.Keys keys)
  39. {
  40. return ((GetKeyState((int)keys) & 0x8000) != 0) ? true : false;
  41. }
  42. }
  43. public static class WindowsMessage
  44. {
  45. public const int WM_MOUSEMOVE = 0x0200;
  46. public const int WM_LBUTTONDOWN = 0x0201;
  47. public const int WM_LBUTTONUP = 0x0202;
  48. public const int WM_LBUTTONDBLCLK = 0x0203;
  49. public const int WM_RBUTTONDOWN = 0x204;
  50. public const int WM_RBUTTONUP = 0x205;
  51. public const int WM_RBUTTONDBLCLK = 0x206;
  52. }
  53. public class QueryKey : IDisposable, IMessageFilter
  54. {
  55. private Control component;
  56. private HashMap<MouseButtons, System.Windows.Forms.Message> mousestate = new HashMap<MouseButtons, System.Windows.Forms.Message>();
  57. private HashMap<MouseButtons, System.Windows.Forms.Message> mousestate_down = new HashMap<MouseButtons, System.Windows.Forms.Message>();
  58. private HashMap<MouseButtons, System.Windows.Forms.Message> mousestate_up = new HashMap<MouseButtons, System.Windows.Forms.Message>();
  59. private HashMap<MouseButtons, System.Windows.Forms.Message> mousestate_query_down = new HashMap<MouseButtons, System.Windows.Forms.Message>();
  60. private HashMap<MouseButtons, System.Windows.Forms.Message> mousestate_query_up = new HashMap<MouseButtons, System.Windows.Forms.Message>();
  61. private Point mouse_pos;
  62. public QueryKey(Control component)
  63. {
  64. this.component = component;
  65. Application.AddMessageFilter(this);
  66. }
  67. public void Dispose()
  68. {
  69. Application.RemoveMessageFilter(this);
  70. }
  71. public bool PreFilterMessage(ref System.Windows.Forms.Message m)
  72. {
  73. try
  74. {
  75. switch (m.Msg)
  76. {
  77. case WindowsMessage.WM_MOUSEMOVE:
  78. mouse_pos = component.PointToClient(Control.MousePosition);
  79. break;
  80. case WindowsMessage.WM_LBUTTONDOWN:
  81. mousestate_down.Put(MouseButtons.Left, m);
  82. mousestate.Put(MouseButtons.Left, m);
  83. break;
  84. case WindowsMessage.WM_RBUTTONDOWN:
  85. mousestate_down.Put(MouseButtons.Right, m);
  86. mousestate.Put(MouseButtons.Right, m);
  87. break;
  88. case WindowsMessage.WM_LBUTTONUP:
  89. mousestate_up.Put(MouseButtons.Left, m);
  90. mousestate.Remove(MouseButtons.Left);
  91. break;
  92. case WindowsMessage.WM_RBUTTONUP:
  93. mousestate_up.Put(MouseButtons.Right, m);
  94. mousestate.Remove(MouseButtons.Right);
  95. break;
  96. case WindowsMessage.WM_LBUTTONDBLCLK:
  97. case WindowsMessage.WM_RBUTTONDBLCLK:
  98. mouse_pos = component.PointToClient(Control.MousePosition);
  99. break;
  100. }
  101. }
  102. catch (Exception err)
  103. {
  104. Console.WriteLine(err.Message + "\n" + err.StackTrace);
  105. }
  106. return false;
  107. }
  108. /// <summary>
  109. /// 用收集到的按键替换查询的按键,并清理收集到的按键,重新监测下一帧的情况
  110. /// </summary>
  111. public void Update()
  112. {
  113. mousestate_query_down.Clear();
  114. mousestate_query_down.PutAll(mousestate_down);
  115. mousestate_query_up.Clear();
  116. mousestate_query_up.PutAll(mousestate_up);
  117. mousestate_down.Clear();
  118. mousestate_up.Clear();
  119. }
  120. public bool IsMouseHold(params MouseButtons[] button)
  121. {
  122. foreach (MouseButtons b in button)
  123. {
  124. if (mousestate.ContainsKey(b))
  125. {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. public bool IsMouseDown(params MouseButtons[] button)
  132. {
  133. foreach (MouseButtons b in button)
  134. {
  135. if (mousestate_query_down.ContainsKey(b))
  136. {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. public bool IsMouseUp(params MouseButtons[] button)
  143. {
  144. foreach (MouseButtons b in button)
  145. {
  146. if (mousestate_query_up.ContainsKey(b))
  147. {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. public int MouseX
  154. {
  155. get
  156. {
  157. return mouse_pos.X;
  158. }
  159. }
  160. public int MouseY
  161. {
  162. get
  163. {
  164. return mouse_pos.Y;
  165. }
  166. }
  167. }
  168. }