示例#1
0
        public bool HookKeyboard()
        {
            if (HookRegistered)
            {
                return(true);
            }

            bool result = false;

            _del = (int code, long wParam, ref KBDLLHOOKSTRUCT lParam) =>
            {
                if (code < 0)
                {
                    return(CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
                }

                OnKeyboardInputChanged(this, new KeyboardInputArgs(wParam, lParam.vkCode, lParam.flags));
                return(CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
            };

            HHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, _del, IntPtr.Zero, 0);

            if (HHook == IntPtr.Zero)
            {
                // Write to file?
                var myerror = Marshal.GetLastWin32Error();
            }
            else
            {
                result         = true;
                HookRegistered = true;
            }

            return(result);
        }
示例#2
0
        public bool HookKeyboard()
        {
            if (HookRegistered)
            {
                return(true);
            }

            bool result = false;

            _del = (int code, long wParam, ref KBDLLHOOKSTRUCT lParam) =>
            {
                // If the code is less than 0, Windows want it to be processed without touching..
                // https://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx
                if (code < 0)
                {
                    return(CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
                }

                foreach (var blacklistedKey in _blacklistedKeys)
                {
                    if (blacklistedKey.Key == (Keys)lParam.vkCode &&
                        blacklistedKey.Value.Invoke())
                    {
                        return((IntPtr)1);
                    }
                }

                OnKeyboardInputChanged(this, new KeyboardInputArgs(wParam, lParam.vkCode, lParam.flags));
                return(CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
            };

            HHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, _del, IntPtr.Zero, 0);

            if (HHook == IntPtr.Zero)
            {
                // Write to file?
                var myerror = Marshal.GetLastWin32Error();
            }
            else
            {
                result         = true;
                HookRegistered = true;
            }

            return(result);
        }
示例#3
0
 static extern IntPtr SetWindowsHookEx(
     HookType hookType
     , KeyboardLLHookHandler lpfn
     , IntPtr hMod
     , uint dwThreadId
     );