新增類KeybordHookProc函數
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace HookDemo { /// <summary> /// 這個類能夠讓你獲得一個在運行中程序的全部鍵盤事件 /// 而且引起一個帶KeyEventArgs和MouseEventArgs參數的.NET事件以便你很容易使用這些信息 /// </summary> public class KeyBordHook { private const byte LLKHF_ALTDOWN = 0x20; private const byte VK_CAPITAL = 0x14; private const byte VK_ESCAPE = 0x1B; private const byte VK_F4 = 0x73; private const byte VK_LCONTROL = 0xA2; private const byte VK_NUMLOCK = 0x90; private const byte VK_RCONTROL = 0xA3; private const byte VK_SHIFT = 0x10; private const byte VK_TAB = 0x09; public const int WH_KEYBOARD = 13; private const int WH_MOUSE = 7; private const int WH_MOUSE_LL = 14; private const int WM_KEYDOWN = 0x100; private const int WM_KEYUP = 0x101; private const int WM_LBUTTONDBLCLK = 0x203; private const int WM_LBUTTONDOWN = 0x201; private const int WM_LBUTTONUP = 0x202; private const int WM_MBUTTONDBLCLK = 0x209; private const int WM_MBUTTONDOWN = 0x207; private const int WM_MBUTTONUP = 0x208; private const int WM_MOUSEMOVE = 0x200; private const int WM_MOUSEWHEEL = 0x020A; private const int WM_RBUTTONDBLCLK = 0x206; private const int WM_RBUTTONDOWN = 0x204; private const int WM_RBUTTONUP = 0x205; private const int WM_SYSKEYDOWN = 0x104; private const int WM_SYSKEYUP = 0x105; //全局的事件 public event KeyEventHandler OnKeyDownEvent; public event KeyEventHandler OnKeyUpEvent; public event KeyPressEventHandler OnKeyPressEvent; static int hKeyboardHook = 0; //鍵盤鉤子句柄 //鼠標常量 public const int WH_KEYBOARD_LL = 13; //keyboard hook constant HookProc KeyboardHookProcedure; //聲明鍵盤鉤子事件類型. //聲明鍵盤鉤子的封送結構類型 [StructLayout(LayoutKind.Sequential)] public class KeyboardHookStruct { public int vkCode; //表示一個在1到254間的虛似鍵盤碼 public int scanCode; //表示硬件掃描碼 public int flags; public int time; public int dwExtraInfo; } //裝置鉤子的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); //卸下鉤子的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); //下一個鉤掛的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); //取得當前線程編號 [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern short GetKeyState(int vKey); public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); /// <summary> /// 墨認的構造函數構造當前類的實例並自動的運行起來. /// </summary> public KeyBordHook() { Start(); } //析構函數. ~KeyBordHook() { Stop(); } /// <summary> /// 啓動Hook,並用流屏蔽任務管理器 /// </summary> public void Start() { if (hKeyboardHook == 0) { // 建立HookProc實例 KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); // 若是設置鉤子失敗 if (hKeyboardHook == 0) { Stop(); AppLog.Error("SetWindowsHookEx failed."); } ////用二進制流的方法打開任務管理器。並且不關閉流.這樣任務管理器就打開不了 //MyFs = new FileStream(Environment.ExpandEnvironmentVariables("%windir%\\system32\\taskmgr.exe"), // FileMode.Open); //byte[] MyByte = new byte[(int)MyFs.Length]; //MyFs.Write(MyByte, 0, (int)MyFs.Length); } } /// <summary> /// 卸載Hook /// </summary> public void Stop() { bool retKeyboard = true; if (hKeyboardHook != 0) { retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; } //if (null != MyFs) //{ // MyFs.Close(); //} if (!(retKeyboard)) { AppLog.Error("UnhookWindowsHookEx failed."); } } #region Nested type: KeyMSG public struct KeyMSG { public int dwExtraInfo; public int flags; public int scanCode; public int time; public int vkCode; } #endregion /// <summary> /// 鍵盤鉤子 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { KeyboardHookStruct m = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); AppLog.Debug($"nCode is :{nCode} -- vkCode is :{m.vkCode}"); if (m.vkCode == (int)Keys.LWin || m.vkCode == (int)Keys.RWin || m.vkCode == (int)Keys.LWin && m.vkCode == (int)Keys.D || m.vkCode == (int)Keys.RWin && m.vkCode == (int)Keys.D || m.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control || m.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt || m.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt || m.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt || m.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift || (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete ) { AppLog.Info("hooc is OK"); return 1; } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } } }
添加鍵盤按鍵觸發事件spa
/// <summary> /// 鍵盤按下觸發事件 /// </summary> void kh_OnKeyDownEvent(object sender, KeyEventArgs e) { }
鍵盤鉤子的使用線程
/// <summary>
/// 鍵盤鉤子
/// </summary>
KeyBordHook _keyBordHook;code
//鍵盤鉤子實例化 _keyBordHook = new KeyBordHook(); _keyBordHook.OnKeyDownEvent += kh_OnKeyDownEvent;