|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
-
- namespace Common.system
- {
-
-
-
-
-
- public class KeyboardHookCommon
- {
-
-
-
- public event KeyEventHandler KeyDownEvent;
-
-
-
- public event KeyPressEventHandler KeyPressEvent;
-
-
-
- public event KeyEventHandler KeyUpEvent;
-
- public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
-
- private static int hKeyboardHook = 0;
-
-
-
-
- public const int WH_KEYBOARD_LL = 13;
-
-
-
-
- private HookProc KeyboardHookProcedure;
-
-
-
-
- private const int WM_KEYDOWN = 0x100;
-
-
-
- private const int WM_KEYUP = 0x101;
-
-
-
- private const int WM_SYSKEYDOWN = 0x104;
-
-
-
- private const int WM_SYSKEYUP = 0x105;
-
-
- [StructLayout(LayoutKind.Sequential)]
- public 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)]
- 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, int wParam, IntPtr lParam);
-
-
- [DllImport("kernel32.dll")]
- private static extern int GetCurrentThreadId();
-
-
- [DllImport("kernel32.dll")]
- public static extern IntPtr GetModuleHandle(string name);
-
-
- [DllImport("user32")]
- public static extern int ToAscii(int uVirtKey, //[in] 指定虚拟关键代码进行翻译。
- int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压)
- byte[] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。
- byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。
- int fuState);
-
-
- [DllImport("user32")]
- public static extern int GetKeyboardState(byte[] pbKeyState);
-
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- private static extern short GetKeyState(int vKey);
-
- public void Start()
- {
-
- if (hKeyboardHook == 0)
- {
- KeyboardHookProcedure = new HookProc(KeyboardHookProc);
- hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (hKeyboardHook == 0)
- {
- Stop();
- throw new Exception("安装键盘钩子失败");
- }
- }
- }
- public void Stop()
- {
- bool retKeyboard = true;
-
-
- if (hKeyboardHook != 0)
- {
- retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
- hKeyboardHook = 0;
- }
- try
- {
-
- if (!(retKeyboard))
- {
- throw new Exception("卸载钩子失败!");
- }
- }
- catch (Exception)
- {
-
- }
- }
-
-
- private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
- {
-
- if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
- {
- KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
-
- if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
- {
- Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
- KeyEventArgs e = new KeyEventArgs(keyData);
- KeyDownEvent(this, e);
- }
-
-
- if (KeyPressEvent != null && wParam == WM_KEYDOWN)
- {
- byte[] keyState = new byte[256];
- GetKeyboardState(keyState);
-
- byte[] inBuffer = new byte[2];
- if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
- {
- KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
- KeyPressEvent(this, e);
- }
- }
-
-
- if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
- {
- Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
- KeyEventArgs e = new KeyEventArgs(keyData);
- KeyUpEvent(this, e);
- }
-
- }
-
-
- return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
- }
- ~KeyboardHookCommon()
- {
- Stop();
- }
-
- }
- }
|