Esempio n. 1
0
        private void ProcessKeyPress(RawInputKeyPressEventArgs args)
        {
            Console.WriteLine($"{args.Device.Name} {args.Device.ID} {args.CorrectedKey} {args.KeyPressState}");

            if (args.Device.Handle == IntPtr.Zero)
            {
                EnqueueDecision(args.VKey, args.KeyPressState, false, IntPtr.Zero);

                return;
            }

            Delegate[] handlers;

            if (!Enabled)
            {
                handlers = HookDisabledOnKeyPress?.GetInvocationList();
                if (handlers != null)
                {
                    foreach (RawInputKeyPressEvent handler in handlers)
                    {
                        handler.Invoke(this, args);

                        if (args.Handled)
                        {
                            break;
                        }
                    }
                }

                EnqueueDecision(args.VKey, args.KeyPressState, args.Handled, args.Device.Handle);

                return;
            }

            handlers = OnKeyPress?.GetInvocationList();
            if (handlers != null)
            {
                foreach (RawInputKeyPressEvent handler in handlers)
                {
                    handler.Invoke(this, args);

                    if (args.Handled)
                    {
                        break;
                    }
                }
            }

            EnqueueDecision(args.VKey, args.KeyPressState, args.Handled, args.Device.Handle);

            if (args.Handled && (handlers = HandledKeyPress?.GetInvocationList()) != null)
            {
                args.Handled = false;

                new Thread(() =>
                {
                    foreach (RawInputKeyPressEvent handler in handlers)
                    {
                        handler.Invoke(this, args);

                        if (args.Handled)
                        {
                            break;
                        }
                    }
                })
                {
                    IsBackground = true,
                    Name         = "HandledKeyPress thread"
                }.Start();
            }
        }