开发工具:
文件大小: 70kb
下载次数: 0
上传时间: 2015-12-04
详细说明: 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 dwExtr aInfo; } 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); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.