vs2010 C# 键盘钩子

上传者: lwz711005 | 上传时间: 2023-11-30 05:06:49 | 文件大小: 70KB | 文件类型: RAR
using System; using System.Runtime.InteropServices; using System.Reflection; using System.Windows.Forms; namespace KeyboardHook { public enum KeyboardEvents { KeyDown = 0x0100, KeyUp = 0x0101, SystemKeyDown = 0x0104, SystemKeyUp = 0x0105 } [StructLayout(LayoutKind.Sequential)] public struct KeyboardHookStruct { public int vkCode; //表示一个在1到254间的虚似键盘码 public int scanCode; //表示硬件扫描码 public int flags; public int time; public int dwExtraInfo; } public delegate void KeyboardEventHandler(KeyboardEvents keyEvent, System.Windows.Forms.Keys key); public class Hook { public event KeyboardEventHandler KeyboardEvent; public enum HookType { WH_JOURNALRECORD = 0, WH_JOURNALPLAYBACK = 1, WH_KEYBOARD = 2, WH_GETMESSAGE = 3, WH_CALLWNDPROC = 4, WH_CBT = 5, WH_SYSMSGFILTER = 6, WH_MOUSE = 7, WH_HARDWARE = 8, WH_DEBUG = 9, WH_SHELL = 10, WH_FOREGROUNDIDLE = 11, WH_CALLWNDPROCRET = 12, WH_KEYBOARD_LL = 13, WH_MOUSE_LL = 14, WH_MSGFILTER = -1, } public delegate IntPtr HookProc(int code, int wParam, IntPtr lParam); [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc hook, IntPtr instance, int threadID); [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr CallNextHookEx(IntPtr hookHandle, int code, int wParam, IntPtr lParam); [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern bool UnhookWindowsHookEx(IntPtr hookHandle); private IntPtr instance; private IntPtr hookHandle; private int threadID; private HookProc hookProcEx; public Hook() { this.instance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]); this.threadID = 0; hookHandle = IntPtr.Zero; hookProcEx = new HookProc(hookProc); } public bool SetHook() { this.hookHandle = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hookProcEx, this.instance, this.threadID); return ((int)hookHandle != 0); } public IntPtr hookProc(int code, int wParam, IntPtr lParam) { if (code >= 0) { KeyboardEvents kEvent = (KeyboardEvents)wParam; if (kEvent != KeyboardEvents.KeyDown && kEvent != KeyboardEvents.KeyUp && kEvent != KeyboardEvents.SystemKeyDown && kEvent != KeyboardEvents.SystemKeyUp) { return CallNextHookEx(this.hookHandle, (int)HookType.WH_KEYBOARD_LL, wParam, lParam); } KeyboardHookStruct MyKey = new KeyboardHookStruct(); Type t = MyKey.GetType(); MyKey = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, t); Keys keyData = (Keys)MyKey.vkCode; KeyboardEvent(kEvent, keyData); } return CallNextHookEx(this.hookHandle, (int)HookType.WH_KEYBOARD_LL, wParam, lParam); } public bool UnHook() { return Hook.UnhookWindowsHookEx(this.hookHandle); } } }

文件下载

资源详情

[{"title":"( 39 个子文件 70KB ) vs2010 C# 键盘钩子","children":[{"title":"KeyboardHook","children":[{"title":"KeyboardHook","children":[{"title":"KeyboardHook.csproj <span style='color:#111;'> 4.01KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Release","children":null,"spread":false},{"title":"Debug","children":[{"title":"KeyboardHook.exe <span style='color:#111;'> 16.50KB </span>","children":null,"spread":false},{"title":"KeyboardHook.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"KeyboardHook.pdb <span style='color:#111;'> 41.50KB </span>","children":null,"spread":false},{"title":"KeyboardHook.vshost.exe <span style='color:#111;'> 11.33KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Form1.Designer.cs <span style='color:#111;'> 2.48KB </span>","children":null,"spread":false},{"title":"RegistryReport.cs <span style='color:#111;'> 797B </span>","children":null,"spread":false},{"title":"Form3.cs <span style='color:#111;'> 1.36KB </span>","children":null,"spread":false},{"title":"Form2.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Form3.Designer.cs <span style='color:#111;'> 3.60KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 493B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"x86","children":[{"title":"Debug","children":[{"title":"KeyboardHook.Form3.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"KeyboardHook.csprojResolveAssemblyReference.cache <span style='color:#111;'> 8.87KB </span>","children":null,"spread":false},{"title":"KeyboardHook.exe <span style='color:#111;'> 16.50KB </span>","children":null,"spread":false},{"title":"KeyboardHook.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"KeyboardHook.pdb <span style='color:#111;'> 41.50KB </span>","children":null,"spread":false},{"title":"KeyboardHook.csproj.GenerateResource.Cache <span style='color:#111;'> 1.01KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.05KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 5.23KB </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"KeyboardHook.Form4.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"KeyboardHook.csproj.FileListAbsolute.txt <span style='color:#111;'> 651B </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true},{"title":"KeyboardHook.cs <span style='color:#111;'> 3.12KB </span>","children":null,"spread":false},{"title":"Form4.Designer.cs <span style='color:#111;'> 3.12KB </span>","children":null,"spread":false},{"title":"Form3.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Form2.cs <span style='color:#111;'> 1.15KB </span>","children":null,"spread":false},{"title":"Form1.cs <span style='color:#111;'> 4.48KB </span>","children":null,"spread":false},{"title":"Form4.cs <span style='color:#111;'> 5.74KB </span>","children":null,"spread":false},{"title":"Form4.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Form2.Designer.cs <span style='color:#111;'> 3.04KB </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Hook.cs <span style='color:#111;'> 3.60KB </span>","children":null,"spread":false},{"title":"Report.cs <span style='color:#111;'> 1.27KB </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.34KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.81KB </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"KeyboardHook.suo <span style='color:#111;'> 27.00KB </span>","children":null,"spread":false},{"title":"KeyboardHook.sln <span style='color:#111;'> 878B </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明