Покажите полный пример хука для левой клавиши мыши с импортами и описаниями методов. Разбирался, написал: | Код | ... private static LowLevelKeyboardProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; private const int WH_MOUSE = 13; ... // installs an application-defined hook procedure into a hook chain [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, HandleRef hMod, int dwThreadId); //removes a hook procedure installed in a hook chain by the SetWindowsHookEx function. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); //passes the hook information to the next hook procedure in the current hook chain [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); //Retrieves a module handle for the specified module. The module must have been loaded by the calling process [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern HandleRef GetModuleHandle(string lpModuleName); //hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); public static IntPtr SetHook(LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { //installs an application-defined hook procedure into a hook chain return SetWindowsHookEx(WH_MOUSE, proc, GetModuleHandle(curModule.ModuleName), 0); } }
// public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
if ((nCode >= 0) && (wParam == (IntPtr)WH_MOUSE) ) { MessageBox.Show("22"); //Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. int vkCode = Marshal.ReadInt32(lParam); if (((Keys)vkCode == Keys.LWin) || ((Keys)vkCode == Keys.RWin)) { MessageBox.Show("11"); Console.WriteLine("{0} blocked!", (Keys)vkCode); return (IntPtr)1; } } //passes the hook information to the next hook procedure in the current hook chain. MessageBox.Show("33"); return CallNextHookEx(_hookID, nCode, wParam, lParam); }
public Form2() { //Call this method to start sending debug reports to the named pipe. _hookID = SetHook(_proc); // Required for Windows Form Designer support InitializeComponent(); ...
|
Неработает! В конец запутался.
--------------------
В каждой черной комнате есть выход - всегда есть выход с любой ситуации!
|