示例#1
0
        public void setKeyboardHook()
        {
            Console.WriteLine("Inizio Button2");
            if (hkeyHook == 0)
            {
                // Create an instance of HookProc.
                _kproc = new LowLevelKeyboardHookProc(KeyboardHookProc);
                Console.WriteLine("SetWindowsHookEx");

                IntPtr hInstance = LoadLibrary("User32");
                hkeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, _kproc, hInstance, 0);
                //If the SetWindowsHookEx function fails.
                if (hkeyHook == 0)
                {
                    System.Windows.MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }
            }
            else
            {
                bool ret = UnhookWindowsHookEx(hkeyHook);
                //If the UnhookWindowsHookEx function fails.
                if (ret == false)
                {
                    System.Windows.MessageBox.Show("UnhookWindowsHookEx Failed");
                    return;
                }
                hkeyHook = 0;
            }
        }
示例#2
0
 public LowLevelKeyboardHook(LowLevelKeyboardHookProc pfnClientHookProc)
     : base(HookType.WH_KEYBOARD_LL)
 {
     this.pfnClientHookProc = pfnClientHookProc;
 }
示例#3
0
 static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardHookProc lpfn,
                                    IntPtr hInstance, int threadId);
 static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardHookProc lpfn,
 IntPtr hInstance, int threadId);
        public void setKeyboardHook()
        {
            Console.WriteLine("Inizio Button2");
            if (hkeyHook == 0)
            {
                // Create an instance of HookProc.
                _kproc = new LowLevelKeyboardHookProc(KeyboardHookProc);
                Console.WriteLine("SetWindowsHookEx");

                IntPtr hInstance = LoadLibrary("User32");
                hkeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, _kproc, hInstance, 0);
                //If the SetWindowsHookEx function fails.
                if (hkeyHook == 0)
                {
                    System.Windows.MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }

            }
            else
            {
                bool ret = UnhookWindowsHookEx(hkeyHook);
                //If the UnhookWindowsHookEx function fails.
                if (ret == false)
                {
                    System.Windows.MessageBox.Show("UnhookWindowsHookEx Failed");
                    return;
                }
                hkeyHook = 0;

            }
        }
 static extern IntPtr SetWindowsHookEx(WindowsHookEnum idHook, LowLevelKeyboardHookProc lpfn, IntPtr hMod, uint dwThreadId);
示例#7
0
文件: User.cs 项目: labeuze/source
 /// <summary>The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.</summary>
 /// <param name="hookType">Specifies the type of hook procedure to be installed.</param>
 /// <param name="callback">Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.</param>
 /// <param name="hMod">Handle to the DLL containing the hook procedure, hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread that containt the hook procedure</param>
 /// <param name="dwThreadId">Identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.</param>
 /// <returns>handle to the hook procedure. If the function fails, the return value is NULL. To get extended error information, call GetLastError.</returns>
 [DllImport("user32", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(HookType hookType, LowLevelKeyboardHookProc callback, IntPtr hMod, uint dwThreadId);