C#ベースのHOOKキーボードフックインスタンスコード

5923 ワード

本明細書では、システムのホットキーを遮断するために使用できるC#ベースのHOOK例について説明する.プログラムは主にフックのインストール、フックの転送、フックのアンロードなどの機能を実現した.「転送フック」:独自のフック関数のハンドルです.このハンドルでは,フックチェーンを巡って伝達されたパラメータをCallNextHookExに簡単に伝達することができ,伝達されたパラメータをCallNextHookExに簡単に伝達することができ,HOOKクラスではキーボードフックハンドル,キーボードフック依頼例,下位層のフック変数など,いくつかのプライベート変数を定義している.フックがメッセージをキャプチャした後、メッセージを処理します.
具体的な実現HOOKコードは以下の通りである.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
namespace          
{
  class HOOK
  {
    #region     
 private IntPtr m_pKeyboardHook = IntPtr.Zero;///       
 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);///       
 private HookProc m_KeyboardHookProcedure;///         
  public const int idHook = 13;///        
  [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr pInstance, int threadId);///     
  [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]///     
  public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
    ///     
      ///             。           
    ///           CallNextHookEx  
    ///           CallNextHookEx  
    /// 
    /// 
    [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode, Int32 wParam, IntPtr lParam);
    [StructLayout(LayoutKind.Sequential)]
    public struct KeyMSG
    {
      public int vkCode;
      public int scanCode;
      public int flags;
      public int time;
      public int dwExtraInfo;
    }
    protected const int WM_QUERYENDSESSION = 0x0011;
    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 int VK_LWIN = 91;
    protected const int VK_RWIN = 92;
    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;
    public bool Porwer = true;//    Porwer 
    public static int pp = 0;//      
    public static bool isSet = false;//        ,false        
    public static bool isHotkey = false;
    public static bool isInstall = false;//      ,true   
    #endregion
    #region      
    public event KeyEventHandler KeyDown;//      
    public event KeyEventHandler KeyUp;//      
    public event KeyPressEventHandler KeyPress;//      
    #endregion
    #region   
    /// 
    ///        ,       
    /// 
    ///   ,       
    ///       
    ///   
    private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
    {
      if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
      {
        KeyMSG keyboardHookStruct = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));//         
        KeyEventArgs e = new KeyEventArgs((Keys)(keyboardHookStruct.vkCode));//  KeyEventArgs       
        switch (wParam)
        {
          case WM_KEYDOWN://      
          case WM_SYSKEYDOWN:
            if (KeyDown != null)//         
            {
              KeyDown(this, e);//     
            }
            break;
          case WM_KEYUP://      
          case WM_SYSKEYUP:
            if (KeyUp != null)//         
            {
              KeyUp(this, e);//     
            }
            break;
        }
      }
      return pp;//        ,1   ,2   
    }
    #endregion
    #region   、    
    /// 
    ///     
    /// 
    ///       
    public bool Start()
    {
      IntPtr pInstance = (IntPtr)4194304;//         
      if (this.m_pKeyboardHook == IntPtr.Zero)//         
      {
        this.m_KeyboardHookProcedure = new HookProc(KeyboardHookProc);//        
        this.m_pKeyboardHook = SetWindowsHookEx(idHook, m_KeyboardHookProcedure, pInstance, 0);//    
        if (this.m_pKeyboardHook == IntPtr.Zero)//      
        {
          this.Stop();//    
          return false;
        }
      }
      isInstall = true;//     
      return true;
    }
    /// 
    ///     
    /// 
    ///       
    public bool Stop()
    {
      if (isInstall == false)//        
      {
        return true;
      }
      bool result = true;
      if (this.m_pKeyboardHook != IntPtr.Zero)//       
      {
        result = (UnhookWindowsHookEx(this.m_pKeyboardHook) && result);//    
        this.m_pKeyboardHook = IntPtr.Zero;//         
      }
      return result;
    }
    #endregion     
  }
}