C#实现可捕获几乎所有键盘鼠标事件的钩子类完整实例

本文实例讲述了C#实现可捕获几乎所有键盘鼠标事件的钩子类。分享给大家供大家参考,具体如下:

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
namespace MouseKeyboardLibrary
{
  /// <summary>
  /// Abstract base class for Mouse and Keyboard hooks
  /// </summary>
  public abstract class GlobalHook
  {
    #region Windows API Code
    [StructLayout(LayoutKind.Sequential)]
    protected class POINT
    {
      public int x;
      public int y;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class MouseHookStruct
    {
      public POINT pt;
      public int hwnd;
      public int wHitTestCode;
      public int dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class MouseLLHookStruct
    {
      public POINT pt;
      public int mouseData;
      public int flags;
      public int time;
      public int dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class KeyboardHookStruct
    {
      public int vkCode;
      public int scanCode;
      public int flags;
      public int time;
      public int dwExtraInfo;
    }
    [DllImport("user32.dll", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    protected static extern int SetWindowsHookEx(
      int idHook,
      HookProc lpfn,
      IntPtr hMod,
      int dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    protected static extern int UnhookWindowsHookEx(int idHook);
    [DllImport("user32.dll", CharSet = CharSet.Auto,
       CallingConvention = CallingConvention.StdCall)]
    protected static extern int CallNextHookEx(
      int idHook,
      int nCode,
      int wParam,
      IntPtr lParam);
    [DllImport("user32")]
    protected static extern int ToAscii(
      int uVirtKey,
      int uScanCode,
      byte[] lpbKeyState,
      byte[] lpwTransKey,
      int fuState);
    [DllImport("user32")]
    protected static extern int GetKeyboardState(byte[] pbKeyState);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    protected static extern short GetKeyState(int vKey);
    protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
    protected const int WH_MOUSE_LL = 14;
    protected const int WH_KEYBOARD_LL = 13;
    protected const int WH_MOUSE = 7;
    protected const int WH_KEYBOARD = 2;
    protected const int WM_MOUSEMOVE = 0x200;
    protected const int WM_LBUTTONDOWN = 0x201;
    protected const int WM_RBUTTONDOWN = 0x204;
    protected const int WM_MBUTTONDOWN = 0x207;
    protected const int WM_LBUTTONUP = 0x202;
    protected const int WM_RBUTTONUP = 0x205;
    protected const int WM_MBUTTONUP = 0x208;
    protected const int WM_LBUTTONDBLCLK = 0x203;
    protected const int WM_RBUTTONDBLCLK = 0x206;
    protected const int WM_MBUTTONDBLCLK = 0x209;
    protected const int WM_MOUSEWHEEL = 0x020A;
    protected const int WM_KEYDOWN = 0x100;
    protected const int WM_KEYUP = 0x101;
    protected const int WM_SYSKEYDOWN = 0x104;
    protected const int WM_SYSKEYUP = 0x105;
    protected const byte VK_SHIFT = 0x10;
    protected const byte VK_CAPITAL = 0x14;
    protected const byte VK_NUMLOCK = 0x90;
    protected const byte VK_LSHIFT = 0xA0;
    protected const byte VK_RSHIFT = 0xA1;
    protected const byte VK_LCONTROL = 0xA2;
    protected const byte VK_RCONTROL = 0x3;
    protected const byte VK_LALT = 0xA4;
    protected const byte VK_RALT = 0xA5;
    protected const byte LLKHF_ALTDOWN = 0x20;
    #endregion
    #region Private Variables
    protected int _hookType;
    protected int _handleToHook;
    protected bool _isStarted;
    protected HookProc _hookCallback;
    #endregion
    #region Properties
    public bool IsStarted
    {
      get
      {
        return _isStarted;
      }
    }
    #endregion
    #region Constructor
    public GlobalHook()
    {
      Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    }
    #endregion
    #region Methods
    public void Start()
    {
      if (!_isStarted &&
        _hookType != 0)
      {
        // Make sure we keep a reference to this delegate!
        // If not, GC randomly collects it, and a NullReference exception is thrown
        _hookCallback = new HookProc(HookCallbackProcedure);
        _handleToHook = SetWindowsHookEx(
          _hookType,
          _hookCallback,
          //Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
          0);
          IntPtr.Zero;
        // Were we able to sucessfully start hook?
        if (_handleToHook != 0)
        {
          _isStarted = true;
        }
      }
    }
    public void Stop()
    {
      if (_isStarted)
      {
        UnhookWindowsHookEx(_handleToHook);
        _isStarted = false;
      }
    }
    protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
    {
      // This method must be overriden by each extending hook
      return 0;
    }
    protected void Application_ApplicationExit(object sender, EventArgs e)
    {
      if (_isStarted)
      {
        Stop();
      }
    }
    #endregion
  }
}
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MouseKeyboardLibrary
{
  /// <summary>
  /// Captures global keyboard events
  /// </summary>
  public class KeyboardHook : GlobalHook
  {
    #region Events
    public event KeyEventHandler KeyDown;
    public event KeyEventHandler KeyUp;
    public event KeyPressEventHandler KeyPress;
    #endregion
    #region Constructor
    public KeyboardHook()
    {
      _hookType = WH_KEYBOARD_LL;
    }
    #endregion
    #region Methods
    protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
    {
      bool handled = false;
      if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
      {
        KeyboardHookStruct keyboardHookStruct =
          (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
        // Is Control being held down?
        bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
                ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
        // Is Shift being held down?
        bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
               ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
        // Is Alt being held down?
        bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
              ((GetKeyState(VK_RALT) & 0x80) != 0);
        // Is CapsLock on?
        bool capslock = (GetKeyState(VK_CAPITAL) != 0);
        // Create event using keycode and control/shift/alt values found above
        KeyEventArgs e = new KeyEventArgs(
          (Keys)(
            keyboardHookStruct.vkCode |
            (control ? (int)Keys.Control : 0) |
            (shift ? (int)Keys.Shift : 0) |
            (alt ? (int)Keys.Alt : 0)
            ));
        // Handle KeyDown and KeyUp events
        switch (wParam)
        {
          case WM_KEYDOWN:
          case WM_SYSKEYDOWN:
            if (KeyDown != null)
            {
              KeyDown(this, e);
              handled = handled || e.Handled;
            }
            break;
          case WM_KEYUP:
          case WM_SYSKEYUP:
            if (KeyUp != null)
            {
              KeyUp(this, e);
              handled = handled || e.Handled;
            }
            break;
        }
        // Handle KeyPress event
        if (wParam == WM_KEYDOWN &&
          !handled &&
          !e.SuppressKeyPress &&
          KeyPress != null)
        {
          byte[] keyState = new byte[256];
          byte[] inBuffer = new byte[2];
          GetKeyboardState(keyState);
          if (ToAscii(keyboardHookStruct.vkCode,
               keyboardHookStruct.scanCode,
               keyState,
               inBuffer,
               keyboardHookStruct.flags) == 1)
          {
            char key = (char)inBuffer[0];
            if ((capslock ^ shift) && Char.IsLetter(key))
              key = Char.ToUpper(key);
            KeyPressEventArgs e2 = new KeyPressEventArgs(key);
            KeyPress(this, e2);
            handled = handled || e.Handled;
          }
        }
      }
      if (handled)
      {
        return 1;
      }
      else
      {
        return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
      }
    }
    #endregion
  }
}
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MouseKeyboardLibrary
{
  /// <summary>
  /// Standard Keyboard Shortcuts used by most applications
  /// </summary>
  public enum StandardShortcut
  {
    Copy,
    Cut,
    Paste,
    SelectAll,
    Save,
    Open,
    New,
    Close,
    Print
  }
  /// <summary>
  /// Simulate keyboard key presses
  /// </summary>
  public static class KeyboardSimulator
  {
    #region Windows API Code
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;
    [DllImport("user32.dll")]
    static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
    #endregion
    #region Methods
    public static void KeyDown(Keys key)
    {
      keybd_event(ParseKey(key), 0, 0, 0);
    }
    public static void KeyUp(Keys key)
    {
      keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
    }
    public static void KeyPress(Keys key)
    {
      KeyDown(key);
      KeyUp(key);
    }
    public static void SimulateStandardShortcut(StandardShortcut shortcut)
    {
      switch (shortcut)
      {
        case StandardShortcut.Copy:
          KeyDown(Keys.Control);
          KeyPress(Keys.C);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.Cut:
          KeyDown(Keys.Control);
          KeyPress(Keys.X);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.Paste:
          KeyDown(Keys.Control);
          KeyPress(Keys.V);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.SelectAll:
          KeyDown(Keys.Control);
          KeyPress(Keys.A);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.Save:
          KeyDown(Keys.Control);
          KeyPress(Keys.S);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.Open:
          KeyDown(Keys.Control);
          KeyPress(Keys.O);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.New:
          KeyDown(Keys.Control);
          KeyPress(Keys.N);
          KeyUp(Keys.Control);
          break;
        case StandardShortcut.Close:
          KeyDown(Keys.Alt);
          KeyPress(Keys.F4);
          KeyUp(Keys.Alt);
          break;
        case StandardShortcut.Print:
          KeyDown(Keys.Control);
          KeyPress(Keys.P);
          KeyUp(Keys.Control);
          break;
      }
    }
    static byte ParseKey(Keys key)
    {
      // Alt, Shift, and Control need to be changed for API function to work with them
      switch (key)
      {
        case Keys.Alt:
          return (byte)18;
        case Keys.Control:
          return (byte)17;
        case Keys.Shift:
          return (byte)16;
        default:
          return (byte)key;
      }
    }
    #endregion
  }
}
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MouseKeyboardLibrary
{
  /// <summary>
  /// Captures global mouse events
  /// </summary>
  public class MouseHook : GlobalHook
  {
    #region MouseEventType Enum
    private enum MouseEventType
    {
      None,
      MouseDown,
      MouseUp,
      DoubleClick,
      MouseWheel,
      MouseMove
    }
    #endregion
    #region Events
    public event MouseEventHandler MouseDown;
    public event MouseEventHandler MouseUp;
    public event MouseEventHandler MouseMove;
    public event MouseEventHandler MouseWheel;
    public event EventHandler Click;
    public event EventHandler DoubleClick;
    #endregion
    #region Constructor
    public MouseHook()
    {
      _hookType = WH_MOUSE_LL;
    }
    #endregion
    #region Methods
    protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
    {
      if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
      {
        MouseLLHookStruct mouseHookStruct =
          (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
        MouseButtons button = GetButton(wParam);
        MouseEventType eventType = GetEventType(wParam);
        MouseEventArgs e = new MouseEventArgs(
          button,
          (eventType == MouseEventType.DoubleClick ? 2 : 1),
          mouseHookStruct.pt.x,
          mouseHookStruct.pt.y,
          (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
        // Prevent multiple Right Click events (this probably happens for popup menus)
        if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
        {
          eventType = MouseEventType.None;
        }
        switch (eventType)
        {
          case MouseEventType.MouseDown:
            if (MouseDown != null)
            {
              MouseDown(this, e);
            }
            break;
          case MouseEventType.MouseUp:
            if (Click != null)
            {
              Click(this, new EventArgs());
            }
            if (MouseUp != null)
            {
              MouseUp(this, e);
            }
            break;
          case MouseEventType.DoubleClick:
            if (DoubleClick != null)
            {
              DoubleClick(this, new EventArgs());
            }
            break;
          case MouseEventType.MouseWheel:
            if (MouseWheel != null)
            {
              MouseWheel(this, e);
            }
            break;
          case MouseEventType.MouseMove:
            if (MouseMove != null)
            {
              MouseMove(this, e);
            }
            break;
          default:
            break;
        }
      }
      return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
    }
    private MouseButtons GetButton(Int32 wParam)
    {
      switch (wParam)
      {
        case WM_LBUTTONDOWN:
        case WM_LBUTTONUP:
        case WM_LBUTTONDBLCLK:
          return MouseButtons.Left;
        case WM_RBUTTONDOWN:
        case WM_RBUTTONUP:
        case WM_RBUTTONDBLCLK:
          return MouseButtons.Right;
        case WM_MBUTTONDOWN:
        case WM_MBUTTONUP:
        case WM_MBUTTONDBLCLK:
          return MouseButtons.Middle;
        default:
          return MouseButtons.None;
      }
    }
    private MouseEventType GetEventType(Int32 wParam)
    {
      switch (wParam)
      {
        case WM_LBUTTONDOWN:
        case WM_RBUTTONDOWN:
        case WM_MBUTTONDOWN:
          return MouseEventType.MouseDown;
        case WM_LBUTTONUP:
        case WM_RBUTTONUP:
        case WM_MBUTTONUP:
          return MouseEventType.MouseUp;
        case WM_LBUTTONDBLCLK:
        case WM_RBUTTONDBLCLK:
        case WM_MBUTTONDBLCLK:
          return MouseEventType.DoubleClick;
        case WM_MOUSEWHEEL:
          return MouseEventType.MouseWheel;
        case WM_MOUSEMOVE:
          return MouseEventType.MouseMove;
        default:
          return MouseEventType.None;
      }
    }
    #endregion
  }
}
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace MouseKeyboardLibrary
{
  /// <summary>
  /// And X, Y point on the screen
  /// </summary>
  public struct MousePoint
  {
    public MousePoint(Point p)
    {
      X = p.X;
      Y = p.Y;
    }
    public int X;
    public int Y;
    public static implicit operator Point(MousePoint p)
    {
      return new Point(p.X, p.Y);
    }
  }
  /// <summary>
  /// Mouse buttons that can be pressed
  /// </summary>
  public enum MouseButton
  {
    Left = 0x2,
    Right = 0x8,
    Middle = 0x20
  }
  /// <summary>
  /// Operations that simulate mouse events
  /// </summary>
  public static class MouseSimulator
  {
    #region Windows API Code
    [DllImport("user32.dll")]
    static extern int ShowCursor(bool show);
    [DllImport("user32.dll")]
    static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
    const int MOUSEEVENTF_MOVE = 0x1;
    const int MOUSEEVENTF_LEFTDOWN = 0x2;
    const int MOUSEEVENTF_LEFTUP = 0x4;
    const int MOUSEEVENTF_RIGHTDOWN = 0x8;
    const int MOUSEEVENTF_RIGHTUP = 0x10;
    const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
    const int MOUSEEVENTF_MIDDLEUP = 0x40;
    const int MOUSEEVENTF_WHEEL = 0x800;
    const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    #endregion
    #region Properties
    /// <summary>
    /// Gets or sets a structure that represents both X and Y mouse coordinates
    /// </summary>
    public static MousePoint Position
    {
      get
      {
        return new MousePoint(Cursor.Position);
      }
      set
      {
        Cursor.Position = value;
      }
    }
    /// <summary>
    /// Gets or sets only the mouse's x coordinate
    /// </summary>
    public static int X
    {
      get
      {
        return Cursor.Position.X;
      }
      set
      {
        Cursor.Position = new Point(value, Y);
      }
    }
    /// <summary>
    /// Gets or sets only the mouse's y coordinate
    /// </summary>
    public static int Y
    {
      get
      {
        return Cursor.Position.Y;
      }
      set
      {
        Cursor.Position = new Point(X, value);
      }
    }
    #endregion
    #region Methods
    /// <summary>
    /// Press a mouse button down
    /// </summary>
    /// <param name="button"></param>
    public static void MouseDown(MouseButton button)
    {
      mouse_event(((int)button), 0, 0, 0, 0);
    }
    public static void MouseDown(MouseButtons button)
    {
      switch (button)
      {
        case MouseButtons.Left:
          MouseDown(MouseButton.Left);
          break;
        case MouseButtons.Middle:
          MouseDown(MouseButton.Middle);
          break;
        case MouseButtons.Right:
          MouseDown(MouseButton.Right);
          break;
      }
    }
    /// <summary>
    /// Let a mouse button up
    /// </summary>
    /// <param name="button"></param>
    public static void MouseUp(MouseButton button)
    {
      mouse_event(((int)button) * 2, 0, 0, 0, 0);
    }
    public static void MouseUp(MouseButtons button)
    {
      switch (button)
      {
        case MouseButtons.Left:
          MouseUp(MouseButton.Left);
          break;
        case MouseButtons.Middle:
          MouseUp(MouseButton.Middle);
          break;
        case MouseButtons.Right:
          MouseUp(MouseButton.Right);
          break;
      }
    }
    /// <summary>
    /// Click a mouse button (down then up)
    /// </summary>
    /// <param name="button"></param>
    public static void Click(MouseButton button)
    {
      MouseDown(button);
      MouseUp(button);
    }
    public static void Click(MouseButtons button)
    {
      switch (button)
      {
        case MouseButtons.Left:
          Click(MouseButton.Left);
          break;
        case MouseButtons.Middle:
          Click(MouseButton.Middle);
          break;
        case MouseButtons.Right:
          Click(MouseButton.Right);
          break;
      }
    }
    /// <summary>
    /// Double click a mouse button (down then up twice)
    /// </summary>
    /// <param name="button"></param>
    public static void DoubleClick(MouseButton button)
    {
      Click(button);
      Click(button);
    }
    public static void DoubleClick(MouseButtons button)
    {
      switch (button)
      {
        case MouseButtons.Left:
          DoubleClick(MouseButton.Left);
          break;
        case MouseButtons.Middle:
          DoubleClick(MouseButton.Middle);
          break;
        case MouseButtons.Right:
          DoubleClick(MouseButton.Right);
          break;
      }
    }
    /// <summary>
    /// Show a hidden current on currently application
    /// </summary>
    public static void Show()
    {
      ShowCursor(true);
    }
    /// <summary>
    /// Hide mouse cursor only on current application's forms
    /// </summary>
    public static void Hide()
    {
      ShowCursor(false);
    }
    #endregion
  }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MouseKeyboardLibrary;
namespace SampleApplication
{
  /*
   上面的5个类编译成Dll引用,使用例子
   */
  public partial class HookTestForm : Form
  {
    MouseHook mouseHook = new MouseHook();
    KeyboardHook keyboardHook = new KeyboardHook();
    public HookTestForm()
    {
      InitializeComponent();
    }
    private void TestForm_Load(object sender, EventArgs e)
    {
      mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
      mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
      mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
      mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);
      keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
      keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
      keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);
      mouseHook.Start();
      keyboardHook.Start();
      SetXYLabel(MouseSimulator.X, MouseSimulator.Y);
    }
    void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
    {
      AddKeyboardEvent(
        "KeyPress",
        "",
        e.KeyChar.ToString(),
        "",
        "",
        ""
        );
    }
    void keyboardHook_KeyUp(object sender, KeyEventArgs e)
    {
      AddKeyboardEvent(
        "KeyUp",
        e.KeyCode.ToString(),
        "",
        e.Shift.ToString(),
        e.Alt.ToString(),
        e.Control.ToString()
        );
    }
    void keyboardHook_KeyDown(object sender, KeyEventArgs e)
    {
      AddKeyboardEvent(
        "KeyDown",
        e.KeyCode.ToString(),
        "",
        e.Shift.ToString(),
        e.Alt.ToString(),
        e.Control.ToString()
        );
    }
    void mouseHook_MouseWheel(object sender, MouseEventArgs e)
    {
      AddMouseEvent(
        "MouseWheel",
        "",
        "",
        "",
        e.Delta.ToString()
        );
    }
    void mouseHook_MouseUp(object sender, MouseEventArgs e)
    {
      AddMouseEvent(
        "MouseUp",
        e.Button.ToString(),
        e.X.ToString(),
        e.Y.ToString(),
        ""
        );
    }
    void mouseHook_MouseDown(object sender, MouseEventArgs e)
    {
      AddMouseEvent(
        "MouseDown",
        e.Button.ToString(),
        e.X.ToString(),
        e.Y.ToString(),
        ""
        );
    }
    void mouseHook_MouseMove(object sender, MouseEventArgs e)
    {
      SetXYLabel(e.X, e.Y);
    }
    void SetXYLabel(int x, int y)
    {
      curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);
    }
    void AddMouseEvent(string eventType, string button, string x, string y, string delta)
    {
      listView1.Items.Insert(0,
        new ListViewItem(
          new string[]{
            eventType,
            button,
            x,
            y,
            delta
          }));
    }
    void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
    {
      listView2.Items.Insert(0,
         new ListViewItem(
           new string[]{
            eventType,
            keyCode,
            keyChar,
            shift,
            alt,
            control
        }));
    }
    private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
    {
      // Not necessary anymore, will stop when application exits
      //mouseHook.Stop();
      //keyboardHook.Stop();
    }
  }
}

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

(0)

相关推荐

  • C#基于委托实现多线程之间操作的方法

    本文实例讲述了C#基于委托实现多线程之间操作的方法.分享给大家供大家参考,具体如下: 有的时候我们要起多个线程,更多的时候可能会有某个线程会去操作其他线程里的属性. 但是线程是并发的,一般的调用是无法实现我们的要求的. 于是,我们在这里就可以用委托,代码如下 private delegate void DelegateInfo(); private delegate void DelegateIsEnd(); //这个是线程调用其他线程的方法 private void Dowork() { //

  • 详解C#中通过委托来实现回调函数功能的方法

    委托(delegate)是一种可以把引用存储为函数的类型,这类似于c++中的函数指针. 回调函数 c++中的回调函数,就是用函数指针来实现的.类似的,c#中用委托,来实现回调函数的功能. 回调函数为什么被称为回调函数?比如你调用了一个函数,那么就叫调用,但是如果你在调用一个函数的时候,还需要把一个函数提供给该函数,让这个函数来调用你的函数,那么你提供的这个函数就被称为回调函数(callback). 对于python这样的动态语言而言,就没有c#,c++提供特殊的语法实现回调函数,因为在pytho

  • C#基于UDP进行异步通信的方法

    本文实例讲述了C#基于UDP进行异步通信的方法.分享给大家供大家参考.具体如下: 服务器端: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace AsyncServer { public class UdpState { public UdpClient udp

  • C#自定义事件监听实现方法

    本文实例讲述了C#自定义事件监听实现方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp { /// <summary> /// 定义事件 /// </summary> class CustomEvent { /// <summary> /// 定义委托 /// &

  • C#实现给DataGrid单元行添加双击事件的方法

    本文实例讲述了C#实现给DataGrid单元行添加双击事件的方法.分享给大家供大家参考.具体如下: 现在我需要做到的功能是当我单击DataGrid某行时显示相对应选中的数据信息,在双击此相同行时弹出删除对话框,应该怎么做呢.由于单击问题很简单就不再阐述了,下面我说一下双击事件是怎么实现的. 这里用到了DataGrid的ItemDataBound事件,我们可以把下面的代码加入到所需的程序中就可实现双击的功能. private void DataGrid1_ItemDataBound( object

  • 使用C#来编写一个异步的Socket服务器

    介绍 我最近需要为一个.net项目准备一个内部线程通信机制. 项目有多个使用ASP.NET,Windows 表单和控制台应用程序的服务器和客户端构成. 考虑到实现的可能性,我下定决心要使用原生的socket,而不是许多.NET中已经提前为我们构建好的组件, 像是所谓的管道, NetTcpClient 还有 Azure 服务总线. 这篇文章中的服务器基于System.Net.Sockets类异步方法. 这些允许你支持大量的socket客户端, 而一个客户端的连接是唯一的阻塞机制. 阻塞的时间是可以

  • C# 委托的三种调用示例(同步调用 异步调用 异步回调)

    首先,通过代码定义一个委托和下面三个示例将要调用的方法: 复制代码 代码如下: public delegate int AddHandler(int a,int b);    public class 加法类    {        public static int Add(int a, int b)        {            Console.WriteLine("开始计算:" + a + "+" + b);            Thread.Sl

  • C#用匿名方法定义委托的实现方法

    本文实例讲述了C#用匿名方法定义委托的实现方法.分享给大家供大家参考.具体实现方法如下: //用匿名方法定义委托 class Program { delegate string MyDelagate(string val); static void Main(string[] args) { string str1 = " 匿名方法外部 "; //中括号部分定义来了一个方法,没有名称,编译器会定指定一个名称 MyDelagate my = delegate(string param)

  • C#中事件的定义和使用

    事件的声明和使用与代理有很密切的关系,事件其实是一个或多个方法的代理,当对象的某个状态发生了变化,代理会被自动调用,从而代理的方法就被自动执行. 声明和使用一个事件需要如下步骤: 1.创建一个代理. 2.在类的内部利用event关键字声明事件,并且在类中定义调用事件的方法,也可以定义一个处理事件消息的方法. 声明一个事件的基本形式有两种: 修饰符  event   类型   标识符 修饰符  event   类型   标识符{get{};set{};} 其中: 修饰符是指C#语言的访问修饰符:类

  • 浅谈C#中的委托、事件与异步

    从刚接触c#编程到现在,差不多快有一年的时间了.在学习过程中,有很多地方始终似是而非,直到最近才弄明白. 本文将先介绍用法,后评断功能. 一.委托 基本用法: 1.声明一个委托类型.委托就像是'类'一样,声明了一种委托之后就可以创建多个具有此种特征的委托.(特征,指的是返回值.参数类型) public delegate void SomeKindOfDelegate(string result); 2.创建一个在1中创建的委托类型的委托. public SomeKindOfDelegate aD

随机推荐