OnKey() public static method

Get input from the keyboard hook; return true if the key was handled and needs to be removed from the input chain.
public static OnKey ( WM ev, VK vk, SC sc, LLKHF flags ) : bool
ev WM
vk VK
sc SC
flags LLKHF
return bool
Beispiel #1
0
        private static int OnKey(HC nCode, WM wParam, IntPtr lParam)
        {
            bool is_key = (wParam == WM.KEYDOWN || wParam == WM.SYSKEYDOWN ||
                           wParam == WM.KEYUP || wParam == WM.SYSKEYUP);

            if (nCode == HC.ACTION && is_key)
            {
                // Retrieve key event data from native structure
                var data = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
                                                                   typeof(KBDLLHOOKSTRUCT));
                bool is_injected = (data.flags & LLKHF.INJECTED) != 0;

                Log.Debug("{0}: OnKey(HC.{1}, WM.{2}, [vk:0x{3:X02} sc:0x{4:X02} flags:{5}])",
                          is_injected ? "Ignored Injected Event" : "Event",
                          nCode, wParam, (int)data.vk, (int)data.sc, data.flags);

                if (!is_injected)
                {
                    if (Composer.OnKey(wParam, data.vk, data.sc, data.flags))
                    {
                        // Do not process further: that key was for us.
                        return(-1);
                    }
                }
            }
            else
            {
                Log.Debug("Ignored Event: OnKey({0}, {1})", nCode, wParam);
            }

            return(NativeMethods.CallNextHookEx(m_hook, nCode, wParam, lParam));
        }
Beispiel #2
0
        private static int OnKey(HC nCode, WM wParam, IntPtr lParam)
        {
            bool is_key = (wParam == WM.KEYDOWN || wParam == WM.SYSKEYDOWN ||
                           wParam == WM.KEYUP || wParam == WM.SYSKEYUP);

            if (m_duplicate != 0)
            {
                // Do nothing. We can only get here if a key is pressed during
                // the very short time where we have two hooks installed, i.e.
                // practically never, but it’s better to handle this properly.
            }
            else if (nCode == HC.ACTION && is_key)
            {
                // Retrieve key event data from native structure
                var data = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
                                                                   typeof(KBDLLHOOKSTRUCT));
                bool is_injected = (data.flags & LLKHF.INJECTED) != 0;
                bool accept      = !is_injected || (Settings.AllowInjected.Value && m_recursive == 0);

                Logger.Debug("{0}{1}: OnKey(HC.{2}, WM.{3}, [vk:0x{4:X02} ({7}) sc:0x{5:X02} flags:{6}])",
                             accept ? "" : "Ignored ", is_injected ? "Injected Event" : "Event",
                             nCode, wParam, (int)data.vk, (int)data.sc, data.flags, new Key(data.vk));

                if (accept)
                {
                    ++m_recursive;
                    bool processed = Composer.OnKey(wParam, data.vk, data.sc, data.flags);
                    --m_recursive;

                    if (processed)
                    {
                        return(-1); // Do not process further: that key was for us.
                    }
                }
            }
            else
            {
                Logger.Debug("Ignored Event: OnKey({0}, {1})", nCode, wParam);
            }

            // Call next hook but guard against re-doing our own work in case we
            // were installed twice.
            ++m_duplicate;
            int ret = NativeMethods.CallNextHookEx(m_hook, nCode, wParam, lParam);

            --m_duplicate;

            return(ret);
        }
Beispiel #3
0
        private static int OnKey(HC nCode, WM wParam, IntPtr lParam)
        {
            if (nCode == HC.ACTION)
            {
                // Retrieve event data from native structure
                var data = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
                                                                   typeof(KBDLLHOOKSTRUCT));

                bool is_key = (wParam == WM.KEYDOWN || wParam == WM.SYSKEYDOWN ||
                               wParam == WM.KEYUP || wParam == WM.SYSKEYUP);
                bool is_injected = (data.flags & LLKHF.INJECTED) != 0;

                if (is_key && !is_injected)
                {
                    if (Composer.OnKey(wParam, data.vk, data.sc, data.flags))
                    {
                        // Do not process further: that key was for us.
                        return(-1);
                    }
                }
            }

            return(NativeMethods.CallNextHookEx(m_hook, nCode, wParam, lParam));
        }