zoukankan      html  css  js  c++  java
  • C#钩子类 几乎捕获键盘鼠标所有事件

    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.   
    67.         [DllImport("user32.dll", CharSet = CharSet.Auto,  
    68.              CallingConvention = CallingConvention.StdCall)]  
    69.         protected static extern int CallNextHookEx(  
    70.             int idHook,  
    71.             int nCode,  
    72.             int wParam,  
    73.             IntPtr lParam);  
    74.   
    75.         [DllImport("user32")]  
    76.         protected static extern int ToAscii(  
    77.             int uVirtKey,  
    78.             int uScanCode,  
    79.             byte[] lpbKeyState,  
    80.             byte[] lpwTransKey,  
    81.             int fuState);  
    82.   
    83.         [DllImport("user32")]  
    84.         protected static extern int GetKeyboardState(byte[] pbKeyState);  
    85.   
    86.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]  
    87.         protected static extern short GetKeyState(int vKey);  
    88.   
    89.         protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);  
    90.   
    91.         protected const int WH_MOUSE_LL = 14;  
    92.         protected const int WH_KEYBOARD_LL = 13;  
    93.   
    94.         protected const int WH_MOUSE = 7;  
    95.         protected const int WH_KEYBOARD = 2;  
    96.         protected const int WM_MOUSEMOVE = 0x200;  
    97.         protected const int WM_LBUTTONDOWN = 0x201;  
    98.         protected const int WM_RBUTTONDOWN = 0x204;  
    99.         protected const int WM_MBUTTONDOWN = 0x207;  
    100.         protected const int WM_LBUTTONUP = 0x202;  
    101.         protected const int WM_RBUTTONUP = 0x205;  
    102.         protected const int WM_MBUTTONUP = 0x208;  
    103.         protected const int WM_LBUTTONDBLCLK = 0x203;  
    104.         protected const int WM_RBUTTONDBLCLK = 0x206;  
    105.         protected const int WM_MBUTTONDBLCLK = 0x209;  
    106.         protected const int WM_MOUSEWHEEL = 0x020A;  
    107.         protected const int WM_KEYDOWN = 0x100;  
    108.         protected const int WM_KEYUP = 0x101;  
    109.         protected const int WM_SYSKEYDOWN = 0x104;  
    110.         protected const int WM_SYSKEYUP = 0x105;  
    111.   
    112.         protected const byte VK_SHIFT = 0x10;  
    113.         protected const byte VK_CAPITAL = 0x14;  
    114.         protected const byte VK_NUMLOCK = 0x90;  
    115.   
    116.         protected const byte VK_LSHIFT = 0xA0;  
    117.         protected const byte VK_RSHIFT = 0xA1;  
    118.         protected const byte VK_LCONTROL = 0xA2;  
    119.         protected const byte VK_RCONTROL = 0x3;  
    120.         protected const byte VK_LALT = 0xA4;  
    121.         protected const byte VK_RALT = 0xA5;  
    122.   
    123.         protected const byte LLKHF_ALTDOWN = 0x20;  
    124.  
    125.         #endregion  
    126.  
    127.         #region Private Variables  
    128.   
    129.         protected int _hookType;  
    130.         protected int _handleToHook;  
    131.         protected bool _isStarted;  
    132.         protected HookProc _hookCallback;  
    133.  
    134.         #endregion  
    135.  
    136.         #region Properties  
    137.   
    138.         public bool IsStarted  
    139.         {  
    140.             get  
    141.             {  
    142.                 return _isStarted;  
    143.             }  
    144.         }  
    145.  
    146.         #endregion  
    147.  
    148.         #region Constructor  
    149.   
    150.         public GlobalHook()  
    151.         {  
    152.   
    153.             Application.ApplicationExit += new EventHandler(Application_ApplicationExit);  
    154.   
    155.         }  
    156.  
    157.         #endregion  
    158.  
    159.         #region Methods  
    160.   
    161.         public void Start()  
    162.         {  
    163.   
    164.             if (!_isStarted &&  
    165.                 _hookType != 0)  
    166.             {  
    167.   
    168.                 // Make sure we keep a reference to this delegate!  
    169.                 // If not, GC randomly collects it, and a NullReference exception is thrown  
    170.                 _hookCallback = new HookProc(HookCallbackProcedure);  
    171.   
    172.                 _handleToHook = SetWindowsHookEx(  
    173.                     _hookType,  
    174.                     _hookCallback,  
    175.                     Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),  
    176.                     0);  
    177.   
    178.                 // Were we able to sucessfully start hook?  
    179.                 if (_handleToHook != 0)  
    180.                 {  
    181.                     _isStarted = true;  
    182.                 }  
    183.   
    184.             }  
    185.   
    186.         }  
    187.   
    188.         public void Stop()  
    189.         {  
    190.   
    191.             if (_isStarted)  
    192.             {  
    193.   
    194.                 UnhookWindowsHookEx(_handleToHook);  
    195.   
    196.                 _isStarted = false;  
    197.   
    198.             }  
    199.   
    200.         }  
    201.   
    202.         protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)  
    203.         {  
    204.              
    205.             // This method must be overriden by each extending hook  
    206.             return 0;  
    207.   
    208.         }  
    209.   
    210.         protected void Application_ApplicationExit(object sender, EventArgs e)  
    211.         {  
    212.   
    213.             if (_isStarted)  
    214.             {  
    215.                 Stop();  
    216.             }  
    217.   
    218.         }  
    219.  
    220.         #endregion  
    221.   
    222.     }  
    223.   
    224. }  
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    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. }  
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    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. }  
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    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. }  
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    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. }  
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    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.   
    79.             AddKeyboardEvent(  
    80.                 "KeyDown",  
    81.                 e.KeyCode.ToString(),  
    82.                 "",  
    83.                 e.Shift.ToString(),  
    84.                 e.Alt.ToString(),  
    85.                 e.Control.ToString()  
    86.                 );  
    87.   
    88.         }  
    89.   
    90.         void mouseHook_MouseWheel(object sender, MouseEventArgs e)  
    91.         {  
    92.   
    93.             AddMouseEvent(  
    94.                 "MouseWheel",  
    95.                 "",  
    96.                 "",  
    97.                 "",  
    98.                 e.Delta.ToString()  
    99.                 );  
    100.   
    101.         }  
    102.   
    103.         void mouseHook_MouseUp(object sender, MouseEventArgs e)  
    104.         {  
    105.   
    106.   
    107.             AddMouseEvent(  
    108.                 "MouseUp",  
    109.                 e.Button.ToString(),  
    110.                 e.X.ToString(),  
    111.                 e.Y.ToString(),  
    112.                 ""  
    113.                 );  
    114.   
    115.         }  
    116.   
    117.         void mouseHook_MouseDown(object sender, MouseEventArgs e)  
    118.         {  
    119.   
    120.   
    121.             AddMouseEvent(  
    122.                 "MouseDown",  
    123.                 e.Button.ToString(),  
    124.                 e.X.ToString(),  
    125.                 e.Y.ToString(),  
    126.                 ""  
    127.                 );  
    128.   
    129.   
    130.         }  
    131.   
    132.         void mouseHook_MouseMove(object sender, MouseEventArgs e)  
    133.         {  
    134.   
    135.             SetXYLabel(e.X, e.Y);  
    136.   
    137.         }  
    138.   
    139.         void SetXYLabel(int x, int y)  
    140.         {  
    141.   
    142.             curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);  
    143.   
    144.         }  
    145.   
    146.         void AddMouseEvent(string eventType, string button, string x, string y, string delta)  
    147.         {  
    148.   
    149.             listView1.Items.Insert(0,  
    150.                 new ListViewItem(  
    151.                     new string[]{  
    152.                         eventType,   
    153.                         button,  
    154.                         x,  
    155.                         y,  
    156.                         delta  
    157.                     }));  
    158.   
    159.         }  
    160.   
    161.         void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)  
    162.         {  
    163.   
    164.             listView2.Items.Insert(0,  
    165.                  new ListViewItem(  
    166.                      new string[]{  
    167.                         eventType,   
    168.                         keyCode,  
    169.                         keyChar,  
    170.                         shift,  
    171.                         alt,  
    172.                         control  
    173.                 }));  
    174.   
    175.         }  
    176.   
    177.         private void TestForm_FormClosed(object sender, FormClosedEventArgs e)  
    178.         {  
    179.   
    180.             // Not necessary anymore, will stop when application exits  
    181.   
    182.             //mouseHook.Stop();  
    183.             //keyboardHook.Stop();  
    184.   
    185.         }  
    186.   
    187.   
    188.     }  
    189. }  
  • 相关阅读:
    HttpCookie类
    WebClient类
    最大流算法 ISAP 模板 和 Dinic模板
    拓扑序+dp Codeforces Round #374 (Div. 2) C
    二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
    线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
    无源无汇有上下界的最大流
    并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E
    dfs Codeforces Round #356 (Div. 2) D
    cookie+session
  • 原文地址:https://www.cnblogs.com/gc2013/p/4036414.html
Copyright © 2011-2022 走看看