1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6.  
  7. namespace MouseKeyboardLibrary
  8. {
  9.  
  10. /// <summary>
  11. /// Abstract base class for Mouse and Keyboard hooks
  12. /// </summary>
  13. public abstract class GlobalHook
  14. {
  15.  
  16. #region Windows API Code
  17.  
  18. [StructLayout(LayoutKind.Sequential)]
  19. protected class POINT
  20. {
  21. public int x;
  22. public int y;
  23. }
  24.  
  25. [StructLayout(LayoutKind.Sequential)]
  26. protected class MouseHookStruct
  27. {
  28. public POINT pt;
  29. public int hwnd;
  30. public int wHitTestCode;
  31. public int dwExtraInfo;
  32. }
  33.  
  34. [StructLayout(LayoutKind.Sequential)]
  35. protected class MouseLLHookStruct
  36. {
  37. public POINT pt;
  38. public int mouseData;
  39. public int flags;
  40. public int time;
  41. public int dwExtraInfo;
  42. }
  43.  
  44. [StructLayout(LayoutKind.Sequential)]
  45. protected class KeyboardHookStruct
  46. {
  47. public int vkCode;
  48. public int scanCode;
  49. public int flags;
  50. public int time;
  51. public int dwExtraInfo;
  52. }
  53.  
  54. [DllImport("user32.dll", CharSet = CharSet.Auto,
  55. CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  56. protected static extern int SetWindowsHookEx(
  57. int idHook,
  58. HookProc lpfn,
  59. IntPtr hMod,
  60. int dwThreadId);
  61.  
  62. [DllImport("user32.dll", CharSet = CharSet.Auto,
  63. CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  64. protected static extern int UnhookWindowsHookEx(int idHook);
  65.  
  66. [DllImport("user32.dll", CharSet = CharSet.Auto,
  67. CallingConvention = CallingConvention.StdCall)]
  68. protected static extern int CallNextHookEx(
  69. int idHook,
  70. int nCode,
  71. int wParam,
  72. IntPtr lParam);
  73.  
  74. [DllImport("user32")]
  75. protected static extern int ToAscii(
  76. int uVirtKey,
  77. int uScanCode,
  78. byte[] lpbKeyState,
  79. byte[] lpwTransKey,
  80. int fuState);
  81.  
  82. [DllImport("user32")]
  83. protected static extern int GetKeyboardState(byte[] pbKeyState);
  84.  
  85. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  86. protected static extern short GetKeyState(int vKey);
  87.  
  88. protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
  89.  
  90. protected const int WH_MOUSE_LL = 14;
  91. protected const int WH_KEYBOARD_LL = 13;
  92.  
  93. protected const int WH_MOUSE = 7;
  94. protected const int WH_KEYBOARD = 2;
  95. protected const int WM_MOUSEMOVE = 0x200;
  96. protected const int WM_LBUTTONDOWN = 0x201;
  97. protected const int WM_RBUTTONDOWN = 0x204;
  98. protected const int WM_MBUTTONDOWN = 0x207;
  99. protected const int WM_LBUTTONUP = 0x202;
  100. protected const int WM_RBUTTONUP = 0x205;
  101. protected const int WM_MBUTTONUP = 0x208;
  102. protected const int WM_LBUTTONDBLCLK = 0x203;
  103. protected const int WM_RBUTTONDBLCLK = 0x206;
  104. protected const int WM_MBUTTONDBLCLK = 0x209;
  105. protected const int WM_MOUSEWHEEL = 0x020A;
  106. protected const int WM_KEYDOWN = 0x100;
  107. protected const int WM_KEYUP = 0x101;
  108. protected const int WM_SYSKEYDOWN = 0x104;
  109. protected const int WM_SYSKEYUP = 0x105;
  110.  
  111. protected const byte VK_SHIFT = 0x10;
  112. protected const byte VK_CAPITAL = 0x14;
  113. protected const byte VK_NUMLOCK = 0x90;
  114.  
  115. protected const byte VK_LSHIFT = 0xA0;
  116. protected const byte VK_RSHIFT = 0xA1;
  117. protected const byte VK_LCONTROL = 0xA2;
  118. protected const byte VK_RCONTROL = 0x3;
  119. protected const byte VK_LALT = 0xA4;
  120. protected const byte VK_RALT = 0xA5;
  121.  
  122. protected const byte LLKHF_ALTDOWN = 0x20;
  123.  
  124. #endregion
  125.  
  126. #region Private Variables
  127.  
  128. protected int _hookType;
  129. protected int _handleToHook;
  130. protected bool _isStarted;
  131. protected HookProc _hookCallback;
  132.  
  133. #endregion
  134.  
  135. #region Properties
  136.  
  137. public bool IsStarted
  138. {
  139. get
  140. {
  141. return _isStarted;
  142. }
  143. }
  144.  
  145. #endregion
  146.  
  147. #region Constructor
  148.  
  149. public GlobalHook()
  150. {
  151.  
  152. Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
  153.  
  154. }
  155.  
  156. #endregion
  157.  
  158. #region Methods
  159.  
  160. public void Start()
  161. {
  162.  
  163. if (!_isStarted &&
  164. _hookType != 0)
  165. {
  166.  
  167. // Make sure we keep a reference to this delegate!
  168. // If not, GC randomly collects it, and a NullReference exception is thrown
  169. _hookCallback = new HookProc(HookCallbackProcedure);
  170.  
  171. _handleToHook = SetWindowsHookEx(
  172. _hookType,
  173. _hookCallback,
  174. Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
  175. 0);
  176.  
  177. // Were we able to sucessfully start hook?
  178. if (_handleToHook != 0)
  179. {
  180. _isStarted = true;
  181. }
  182.  
  183. }
  184.  
  185. }
  186.  
  187. public void Stop()
  188. {
  189.  
  190. if (_isStarted)
  191. {
  192.  
  193. UnhookWindowsHookEx(_handleToHook);
  194.  
  195. _isStarted = false;
  196.  
  197. }
  198.  
  199. }
  200.  
  201. protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
  202. {
  203.  
  204. // This method must be overriden by each extending hook
  205. return 0;
  206.  
  207. }
  208.  
  209. protected void Application_ApplicationExit(object sender, EventArgs e)
  210. {
  211.  
  212. if (_isStarted)
  213. {
  214. Stop();
  215. }
  216.  
  217. }
  218.  
  219. #endregion
  220.  
  221. }
  222.  
  223. }
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace MouseKeyboardLibrary
  7. {
  8.  
  9. /// <summary>
  10. /// Captures global keyboard events
  11. /// </summary>
  12. public class KeyboardHook : GlobalHook
  13. {
  14.  
  15. #region Events
  16.  
  17. public event KeyEventHandler KeyDown;
  18. public event KeyEventHandler KeyUp;
  19. public event KeyPressEventHandler KeyPress;
  20.  
  21. #endregion
  22.  
  23. #region Constructor
  24.  
  25. public KeyboardHook()
  26. {
  27.  
  28. _hookType = WH_KEYBOARD_LL;
  29.  
  30. }
  31.  
  32. #endregion
  33.  
  34. #region Methods
  35.  
  36. protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
  37. {
  38.  
  39. bool handled = false;
  40.  
  41. if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
  42. {
  43.  
  44. KeyboardHookStruct keyboardHookStruct =
  45. (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
  46.  
  47. // Is Control being held down?
  48. bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
  49. ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
  50.  
  51. // Is Shift being held down?
  52. bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
  53. ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
  54.  
  55. // Is Alt being held down?
  56. bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
  57. ((GetKeyState(VK_RALT) & 0x80) != 0);
  58.  
  59. // Is CapsLock on?
  60. bool capslock = (GetKeyState(VK_CAPITAL) != 0);
  61.  
  62. // Create event using keycode and control/shift/alt values found above
  63. KeyEventArgs e = new KeyEventArgs(
  64. (Keys)(
  65. keyboardHookStruct.vkCode |
  66. (control ? (int)Keys.Control : 0) |
  67. (shift ? (int)Keys.Shift : 0) |
  68. (alt ? (int)Keys.Alt : 0)
  69. ));
  70.  
  71. // Handle KeyDown and KeyUp events
  72. switch (wParam)
  73. {
  74.  
  75. case WM_KEYDOWN:
  76. case WM_SYSKEYDOWN:
  77. if (KeyDown != null)
  78. {
  79. KeyDown(this, e);
  80. handled = handled || e.Handled;
  81. }
  82. break;
  83. case WM_KEYUP:
  84. case WM_SYSKEYUP:
  85. if (KeyUp != null)
  86. {
  87. KeyUp(this, e);
  88. handled = handled || e.Handled;
  89. }
  90. break;
  91.  
  92. }
  93.  
  94. // Handle KeyPress event
  95. if (wParam == WM_KEYDOWN &&
  96. !handled &&
  97. !e.SuppressKeyPress &&
  98. KeyPress != null)
  99. {
  100.  
  101. byte[] keyState = new byte[256];
  102. byte[] inBuffer = new byte[2];
  103. GetKeyboardState(keyState);
  104.  
  105. if (ToAscii(keyboardHookStruct.vkCode,
  106. keyboardHookStruct.scanCode,
  107. keyState,
  108. inBuffer,
  109. keyboardHookStruct.flags) == 1)
  110. {
  111.  
  112. char key = (char)inBuffer[0];
  113. if ((capslock ^ shift) && Char.IsLetter(key))
  114. key = Char.ToUpper(key);
  115. KeyPressEventArgs e2 = new KeyPressEventArgs(key);
  116. KeyPress(this, e2);
  117. handled = handled || e.Handled;
  118.  
  119. }
  120.  
  121. }
  122.  
  123. }
  124.  
  125. if (handled)
  126. {
  127. return 1;
  128. }
  129. else
  130. {
  131. return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
  132. }
  133.  
  134. }
  135.  
  136. #endregion
  137.  
  138. }
  139.  
  140. }
  1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6. namespace MouseKeyboardLibrary
  7. {
  8.  
  9. /// <summary>
  10. /// Standard Keyboard Shortcuts used by most applications
  11. /// </summary>
  12. public enum StandardShortcut
  13. {
  14. Copy,
  15. Cut,
  16. Paste,
  17. SelectAll,
  18. Save,
  19. Open,
  20. New,
  21. Close,
  22. Print
  23. }
  24.  
  25. /// <summary>
  26. /// Simulate keyboard key presses
  27. /// </summary>
  28. public static class KeyboardSimulator
  29. {
  30.  
  31. #region Windows API Code
  32.  
  33. const int KEYEVENTF_EXTENDEDKEY = 0x1;
  34. const int KEYEVENTF_KEYUP = 0x2;
  35.  
  36. [DllImport("user32.dll")]
  37. static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
  38.  
  39. #endregion
  40.  
  41. #region Methods
  42.  
  43. public static void KeyDown(Keys key)
  44. {
  45. keybd_event(ParseKey(key), 0, 0, 0);
  46. }
  47.  
  48. public static void KeyUp(Keys key)
  49. {
  50. keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
  51. }
  52.  
  53. public static void KeyPress(Keys key)
  54. {
  55. KeyDown(key);
  56. KeyUp(key);
  57. }
  58.  
  59. public static void SimulateStandardShortcut(StandardShortcut shortcut)
  60. {
  61. switch (shortcut)
  62. {
  63. case StandardShortcut.Copy:
  64. KeyDown(Keys.Control);
  65. KeyPress(Keys.C);
  66. KeyUp(Keys.Control);
  67. break;
  68. case StandardShortcut.Cut:
  69. KeyDown(Keys.Control);
  70. KeyPress(Keys.X);
  71. KeyUp(Keys.Control);
  72. break;
  73. case StandardShortcut.Paste:
  74. KeyDown(Keys.Control);
  75. KeyPress(Keys.V);
  76. KeyUp(Keys.Control);
  77. break;
  78. case StandardShortcut.SelectAll:
  79. KeyDown(Keys.Control);
  80. KeyPress(Keys.A);
  81. KeyUp(Keys.Control);
  82. break;
  83. case StandardShortcut.Save:
  84. KeyDown(Keys.Control);
  85. KeyPress(Keys.S);
  86. KeyUp(Keys.Control);
  87. break;
  88. case StandardShortcut.Open:
  89. KeyDown(Keys.Control);
  90. KeyPress(Keys.O);
  91. KeyUp(Keys.Control);
  92. break;
  93. case StandardShortcut.New:
  94. KeyDown(Keys.Control);
  95. KeyPress(Keys.N);
  96. KeyUp(Keys.Control);
  97. break;
  98. case StandardShortcut.Close:
  99. KeyDown(Keys.Alt);
  100. KeyPress(Keys.F4);
  101. KeyUp(Keys.Alt);
  102. break;
  103. case StandardShortcut.Print:
  104. KeyDown(Keys.Control);
  105. KeyPress(Keys.P);
  106. KeyUp(Keys.Control);
  107. break;
  108. }
  109. }
  110.  
  111. static byte ParseKey(Keys key)
  112. {
  113.  
  114. // Alt, Shift, and Control need to be changed for API function to work with them
  115. switch (key)
  116. {
  117. case Keys.Alt:
  118. return (byte)18;
  119. case Keys.Control:
  120. return (byte)17;
  121. case Keys.Shift:
  122. return (byte)16;
  123. default:
  124. return (byte)key;
  125. }
  126.  
  127. }
  128.  
  129. #endregion
  130.  
  131. }
  132.  
  133. }
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace MouseKeyboardLibrary
  7. {
  8.  
  9. /// <summary>
  10. /// Captures global mouse events
  11. /// </summary>
  12. public class MouseHook : GlobalHook
  13. {
  14.  
  15. #region MouseEventType Enum
  16.  
  17. private enum MouseEventType
  18. {
  19. None,
  20. MouseDown,
  21. MouseUp,
  22. DoubleClick,
  23. MouseWheel,
  24. MouseMove
  25. }
  26.  
  27. #endregion
  28.  
  29. #region Events
  30.  
  31. public event MouseEventHandler MouseDown;
  32. public event MouseEventHandler MouseUp;
  33. public event MouseEventHandler MouseMove;
  34. public event MouseEventHandler MouseWheel;
  35.  
  36. public event EventHandler Click;
  37. public event EventHandler DoubleClick;
  38.  
  39. #endregion
  40.  
  41. #region Constructor
  42.  
  43. public MouseHook()
  44. {
  45.  
  46. _hookType = WH_MOUSE_LL;
  47.  
  48. }
  49.  
  50. #endregion
  51.  
  52. #region Methods
  53.  
  54. protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
  55. {
  56.  
  57. if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
  58. {
  59.  
  60. MouseLLHookStruct mouseHookStruct =
  61. (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
  62.  
  63. MouseButtons button = GetButton(wParam);
  64. MouseEventType eventType = GetEventType(wParam);
  65.  
  66. MouseEventArgs e = new MouseEventArgs(
  67. button,
  68. (eventType == MouseEventType.DoubleClick ? 2 : 1),
  69. mouseHookStruct.pt.x,
  70. mouseHookStruct.pt.y,
  71. (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
  72.  
  73. // Prevent multiple Right Click events (this probably happens for popup menus)
  74. if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
  75. {
  76. eventType = MouseEventType.None;
  77. }
  78.  
  79. switch (eventType)
  80. {
  81. case MouseEventType.MouseDown:
  82. if (MouseDown != null)
  83. {
  84. MouseDown(this, e);
  85. }
  86. break;
  87. case MouseEventType.MouseUp:
  88. if (Click != null)
  89. {
  90. Click(this, new EventArgs());
  91. }
  92. if (MouseUp != null)
  93. {
  94. MouseUp(this, e);
  95. }
  96. break;
  97. case MouseEventType.DoubleClick:
  98. if (DoubleClick != null)
  99. {
  100. DoubleClick(this, new EventArgs());
  101. }
  102. break;
  103. case MouseEventType.MouseWheel:
  104. if (MouseWheel != null)
  105. {
  106. MouseWheel(this, e);
  107. }
  108. break;
  109. case MouseEventType.MouseMove:
  110. if (MouseMove != null)
  111. {
  112. MouseMove(this, e);
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118.  
  119. }
  120.  
  121. return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
  122.  
  123. }
  124.  
  125. private MouseButtons GetButton(Int32 wParam)
  126. {
  127.  
  128. switch (wParam)
  129. {
  130.  
  131. case WM_LBUTTONDOWN:
  132. case WM_LBUTTONUP:
  133. case WM_LBUTTONDBLCLK:
  134. return MouseButtons.Left;
  135. case WM_RBUTTONDOWN:
  136. case WM_RBUTTONUP:
  137. case WM_RBUTTONDBLCLK:
  138. return MouseButtons.Right;
  139. case WM_MBUTTONDOWN:
  140. case WM_MBUTTONUP:
  141. case WM_MBUTTONDBLCLK:
  142. return MouseButtons.Middle;
  143. default:
  144. return MouseButtons.None;
  145.  
  146. }
  147.  
  148. }
  149.  
  150. private MouseEventType GetEventType(Int32 wParam)
  151. {
  152.  
  153. switch (wParam)
  154. {
  155.  
  156. case WM_LBUTTONDOWN:
  157. case WM_RBUTTONDOWN:
  158. case WM_MBUTTONDOWN:
  159. return MouseEventType.MouseDown;
  160. case WM_LBUTTONUP:
  161. case WM_RBUTTONUP:
  162. case WM_MBUTTONUP:
  163. return MouseEventType.MouseUp;
  164. case WM_LBUTTONDBLCLK:
  165. case WM_RBUTTONDBLCLK:
  166. case WM_MBUTTONDBLCLK:
  167. return MouseEventType.DoubleClick;
  168. case WM_MOUSEWHEEL:
  169. return MouseEventType.MouseWheel;
  170. case WM_MOUSEMOVE:
  171. return MouseEventType.MouseMove;
  172. default:
  173. return MouseEventType.None;
  174.  
  175. }
  176. }
  177.  
  178. #endregion
  179.  
  180. }
  181.  
  182. }
  1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6.  
  7. namespace MouseKeyboardLibrary
  8. {
  9.  
  10. /// <summary>
  11. /// And X, Y point on the screen
  12. /// </summary>
  13. public struct MousePoint
  14. {
  15.  
  16. public MousePoint(Point p)
  17. {
  18. X = p.X;
  19. Y = p.Y;
  20. }
  21.  
  22. public int X;
  23. public int Y;
  24.  
  25. public static implicit operator Point(MousePoint p)
  26. {
  27. return new Point(p.X, p.Y);
  28. }
  29.  
  30. }
  31.  
  32. /// <summary>
  33. /// Mouse buttons that can be pressed
  34. /// </summary>
  35. public enum MouseButton
  36. {
  37. Left = 0x2,
  38. Right = 0x8,
  39. Middle = 0x20
  40. }
  41.  
  42. /// <summary>
  43. /// Operations that simulate mouse events
  44. /// </summary>
  45. public static class MouseSimulator
  46. {
  47.  
  48. #region Windows API Code
  49.  
  50. [DllImport("user32.dll")]
  51. static extern int ShowCursor(bool show);
  52.  
  53. [DllImport("user32.dll")]
  54. static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
  55.  
  56. const int MOUSEEVENTF_MOVE = 0x1;
  57. const int MOUSEEVENTF_LEFTDOWN = 0x2;
  58. const int MOUSEEVENTF_LEFTUP = 0x4;
  59. const int MOUSEEVENTF_RIGHTDOWN = 0x8;
  60. const int MOUSEEVENTF_RIGHTUP = 0x10;
  61. const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
  62. const int MOUSEEVENTF_MIDDLEUP = 0x40;
  63. const int MOUSEEVENTF_WHEEL = 0x800;
  64. const int MOUSEEVENTF_ABSOLUTE = 0x8000;
  65.  
  66. #endregion
  67.  
  68. #region Properties
  69.  
  70. /// <summary>
  71. /// Gets or sets a structure that represents both X and Y mouse coordinates
  72. /// </summary>
  73. public static MousePoint Position
  74. {
  75. get
  76. {
  77. return new MousePoint(Cursor.Position);
  78. }
  79. set
  80. {
  81. Cursor.Position = value;
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Gets or sets only the mouse's x coordinate
  87. /// </summary>
  88. public static int X
  89. {
  90. get
  91. {
  92. return Cursor.Position.X;
  93. }
  94. set
  95. {
  96. Cursor.Position = new Point(value, Y);
  97. }
  98. }
  99.  
  100. /// <summary>
  101. /// Gets or sets only the mouse's y coordinate
  102. /// </summary>
  103. public static int Y
  104. {
  105. get
  106. {
  107. return Cursor.Position.Y;
  108. }
  109. set
  110. {
  111. Cursor.Position = new Point(X, value);
  112. }
  113. }
  114.  
  115. #endregion
  116.  
  117. #region Methods
  118.  
  119. /// <summary>
  120. /// Press a mouse button down
  121. /// </summary>
  122. /// <param name="button"></param>
  123. public static void MouseDown(MouseButton button)
  124. {
  125. mouse_event(((int)button), 0, 0, 0, 0);
  126. }
  127.  
  128. public static void MouseDown(MouseButtons button)
  129. {
  130. switch (button)
  131. {
  132. case MouseButtons.Left:
  133. MouseDown(MouseButton.Left);
  134. break;
  135. case MouseButtons.Middle:
  136. MouseDown(MouseButton.Middle);
  137. break;
  138. case MouseButtons.Right:
  139. MouseDown(MouseButton.Right);
  140. break;
  141. }
  142. }
  143.  
  144. /// <summary>
  145. /// Let a mouse button up
  146. /// </summary>
  147. /// <param name="button"></param>
  148. public static void MouseUp(MouseButton button)
  149. {
  150. mouse_event(((int)button) * 2, 0, 0, 0, 0);
  151. }
  152.  
  153. public static void MouseUp(MouseButtons button)
  154. {
  155. switch (button)
  156. {
  157. case MouseButtons.Left:
  158. MouseUp(MouseButton.Left);
  159. break;
  160. case MouseButtons.Middle:
  161. MouseUp(MouseButton.Middle);
  162. break;
  163. case MouseButtons.Right:
  164. MouseUp(MouseButton.Right);
  165. break;
  166. }
  167. }
  168.  
  169. /// <summary>
  170. /// Click a mouse button (down then up)
  171. /// </summary>
  172. /// <param name="button"></param>
  173. public static void Click(MouseButton button)
  174. {
  175. MouseDown(button);
  176. MouseUp(button);
  177. }
  178.  
  179. public static void Click(MouseButtons button)
  180. {
  181. switch (button)
  182. {
  183. case MouseButtons.Left:
  184. Click(MouseButton.Left);
  185. break;
  186. case MouseButtons.Middle:
  187. Click(MouseButton.Middle);
  188. break;
  189. case MouseButtons.Right:
  190. Click(MouseButton.Right);
  191. break;
  192. }
  193. }
  194.  
  195. /// <summary>
  196. /// Double click a mouse button (down then up twice)
  197. /// </summary>
  198. /// <param name="button"></param>
  199. public static void DoubleClick(MouseButton button)
  200. {
  201. Click(button);
  202. Click(button);
  203. }
  204.  
  205. public static void DoubleClick(MouseButtons button)
  206. {
  207. switch (button)
  208. {
  209. case MouseButtons.Left:
  210. DoubleClick(MouseButton.Left);
  211. break;
  212. case MouseButtons.Middle:
  213. DoubleClick(MouseButton.Middle);
  214. break;
  215. case MouseButtons.Right:
  216. DoubleClick(MouseButton.Right);
  217. break;
  218. }
  219. }
  220.  
  221. /// <summary>
  222. /// Show a hidden current on currently application
  223. /// </summary>
  224. public static void Show()
  225. {
  226. ShowCursor(true);
  227. }
  228.  
  229. /// <summary>
  230. /// Hide mouse cursor only on current application's forms
  231. /// </summary>
  232. public static void Hide()
  233. {
  234. ShowCursor(false);
  235. }
  236.  
  237. #endregion
  238.  
  239. }
  240.  
  241. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. using MouseKeyboardLibrary;
  11.  
  12. namespace SampleApplication
  13. {
  14. /*
  15. 上面的5个类编译成Dll引用,使用例子
  16. */
  17. public partial class HookTestForm : Form
  18. {
  19.  
  20. MouseHook mouseHook = new MouseHook();
  21. KeyboardHook keyboardHook = new KeyboardHook();
  22.  
  23. public HookTestForm()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. private void TestForm_Load(object sender, EventArgs e)
  29. {
  30.  
  31. mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
  32. mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
  33. mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
  34. mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);
  35.  
  36. keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
  37. keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
  38. keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);
  39.  
  40. mouseHook.Start();
  41. keyboardHook.Start();
  42.  
  43. SetXYLabel(MouseSimulator.X, MouseSimulator.Y);
  44.  
  45. }
  46.  
  47. void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
  48. {
  49.  
  50. AddKeyboardEvent(
  51. "KeyPress",
  52. "",
  53. e.KeyChar.ToString(),
  54. "",
  55. "",
  56. ""
  57. );
  58.  
  59. }
  60.  
  61. void keyboardHook_KeyUp(object sender, KeyEventArgs e)
  62. {
  63.  
  64. AddKeyboardEvent(
  65. "KeyUp",
  66. e.KeyCode.ToString(),
  67. "",
  68. e.Shift.ToString(),
  69. e.Alt.ToString(),
  70. e.Control.ToString()
  71. );
  72.  
  73. }
  74.  
  75. void keyboardHook_KeyDown(object sender, KeyEventArgs e)
  76. {
  77.  
  78. AddKeyboardEvent(
  79. "KeyDown",
  80. e.KeyCode.ToString(),
  81. "",
  82. e.Shift.ToString(),
  83. e.Alt.ToString(),
  84. e.Control.ToString()
  85. );
  86.  
  87. }
  88.  
  89. void mouseHook_MouseWheel(object sender, MouseEventArgs e)
  90. {
  91.  
  92. AddMouseEvent(
  93. "MouseWheel",
  94. "",
  95. "",
  96. "",
  97. e.Delta.ToString()
  98. );
  99.  
  100. }
  101.  
  102. void mouseHook_MouseUp(object sender, MouseEventArgs e)
  103. {
  104.  
  105. AddMouseEvent(
  106. "MouseUp",
  107. e.Button.ToString(),
  108. e.X.ToString(),
  109. e.Y.ToString(),
  110. ""
  111. );
  112.  
  113. }
  114.  
  115. void mouseHook_MouseDown(object sender, MouseEventArgs e)
  116. {
  117.  
  118. AddMouseEvent(
  119. "MouseDown",
  120. e.Button.ToString(),
  121. e.X.ToString(),
  122. e.Y.ToString(),
  123. ""
  124. );
  125.  
  126. }
  127.  
  128. void mouseHook_MouseMove(object sender, MouseEventArgs e)
  129. {
  130.  
  131. SetXYLabel(e.X, e.Y);
  132.  
  133. }
  134.  
  135. void SetXYLabel(int x, int y)
  136. {
  137.  
  138. curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);
  139.  
  140. }
  141.  
  142. void AddMouseEvent(string eventType, string button, string x, string y, string delta)
  143. {
  144.  
  145. listView1.Items.Insert(0,
  146. new ListViewItem(
  147. new string[]{
  148. eventType,
  149. button,
  150. x,
  151. y,
  152. delta
  153. }));
  154.  
  155. }
  156.  
  157. void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
  158. {
  159.  
  160. listView2.Items.Insert(0,
  161. new ListViewItem(
  162. new string[]{
  163. eventType,
  164. keyCode,
  165. keyChar,
  166. shift,
  167. alt,
  168. control
  169. }));
  170.  
  171. }
  172.  
  173. private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
  174. {
  175.  
  176. // Not necessary anymore, will stop when application exits
  177.  
  178. //mouseHook.Stop();
  179. //keyboardHook.Stop();
  180.  
  181. }
  182.  
  183. }
  184. }

C#钩子类 几乎捕获键盘鼠标所有事件的更多相关文章

  1. [UE4]键盘鼠标输入事件

    然后在角色的事件视图就可以使用预先定义好的事件

  2. Directx11学习笔记【二十一】 封装键盘鼠标响应类

    原文:Directx11学习笔记[二十一] 封装键盘鼠标响应类 摘要: 本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/ ...

  3. c#键盘鼠标钩子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  4. DSAPI 键盘鼠标钩子

    通常,说到Hook键盘鼠标,总需要一大堆代码,涉及各种不明白的API.而在DSAPI中,可以说已经把勾子简化到不能再简化的地步.甚至不需要任何示例代码即会使用.那么如何实现呢? Private Wit ...

  5. WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例

    键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...

  6. 【pyHook】 监测键盘鼠标事件等

    [pyHook] pyHook是一个用来进行键盘.鼠标等层面事件监控的库.这个库的正常工作需要pythoncom等操作系统的API的支持.首先来说说如何安装. 直接pip install pyHook ...

  7. setCapture只能作用于鼠标不可作用于键盘等其它事件

    处理的优点非常类似于流媒体的优点.分析能够立即开始,而不是等待所有的数据被处理.而且,由于应用程序只是在读取数据时检查数据,因此不需要将数据存储在内存中.这对于大型文档来说是个巨大的优点.事实上,应用 ...

  8. java 计算器SWT/RAP(版本3)键盘鼠标兼容

    java 计算器SWT/RAP(版本3)键盘鼠标兼容,之前版本也对,但存在线程失效问题,当多人访问时,就容易线程失效,一直犯得一个错误就是一直用static变量和static方法, 之前加了什么js界 ...

  9. Delphi keydown与keyup、keypress的区别(KeyDown 和KeyUp 通常可以捕获键盘除了PrScrn所有按键)

    Shift 是一个集合变量.type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble ...

随机推荐

  1. javascript计算啤酒2元一瓶,4个盖换一瓶,2个瓶换一瓶,10元钱最多喝多少瓶

    var n = 0//当前剩下多少瓶加上喝赢了多少瓶 var x = 5//初始多少瓶 var y = 0//除了喝掉的,剩下多少瓶 var z = 0;//总数 var arr = []//定义一个 ...

  2. 原生的AJAX

    var XHR=null; if (window.XMLHttpRequest) { // 非IE内核 XHR = new XMLHttpRequest(); } else if (window.Ac ...

  3. java环境安装说明

    Java从安装到运行第一个程序 对于初学者来说,能否成功运行第一个Java程序,关系到这杯咖啡的口感. 作为才疏学浅的常年初学者,语言描述不清,还是上图吧! 一.安装JDK 打开网址http://ww ...

  4. 201521123083 《Java程序设计》第10周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常,多线程 1.finally题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? 一个tr ...

  5. 微信小程序view标签以及display:flex的测试

    一:testview.wxml,testview.js自动生成示例代码 //testview.wxml <view class="section"> <view ...

  6. 201521123061 《Java程序设计》第十一周学习总结

    201521123061 <Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 本周学习的是如何解决多线程访问中的互斥 ...

  7. 201521123013 《Java程序设计》第7周学习总结

    1. 本章学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { r ...

  8. 201521123010 《Java程序设计》第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 ①ArrayList代码分析 1.1 解释ArrayList的contains源代码 ...

  9. 201521123093 java 第三周学习总结

    1.本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 本周学习总结: ...

  10. Mysql数据库文件、表、记录的增删改查

    一.数据库文件夹的的操作 create database db1 charset utf8; 增加db1文件夹 show databases ; 查看所有数据库 show create databas ...