示例#1
0
    public static void Update()
    {
        // Повторяем цикл, пока есть необработанные нажатия
        while (true)
        {
            DeviceID deviceID = Interception.WaitWithTimeout(context, 0);
            if (deviceID == 0)
            {
                break;
            }

            currentDeviceID = deviceID;

            Interception.Stroke stroke = new Interception.Stroke();

            while (Interception.Receive(context, deviceID, ref stroke, 1) > 0)
            {
                Key key = ToKey(stroke.Key);

                if (showKeys)
                {
                    Console.WriteLine("Key: {0}; Scancode: 0x{1:X2}; State: {2}", key, stroke.Key.Code, stroke.Key.State);
                }

                bool processed;

                KeyList deviceDownedKeys = GetOrCreateKeyList(downedKeys, deviceID);

                if (stroke.Key.State.IsKeyDown())
                {
                    if (!deviceDownedKeys.Contains(key))
                    {
                        deviceDownedKeys.Add(key);
                        processed = OnKeyDown(key, false);
                    }
                    else
                    {
                        processed = OnKeyDown(key, true);
                    }
                }
                else
                {
                    deviceDownedKeys.Remove(key);
                    processed = OnKeyUp(key);
                }

                if (!processed)
                {
                    Interception.Send(context, deviceID, ref stroke, 1);
                }
            }
        }
    }
示例#2
0
        public static void Run()
        {
            var input = new List <Interception.KeyStroke>(6);

            var stroke  = new Interception.Stroke();
            var context = Interception.CreateContext();

            Interception.SetFilter(context, Interception.IsKeyboard, Interception.Filter.All);

            try
            {
                while (true)
                {
                    var device = Interception.Wait(context);
                    if (Interception.Receive(context, device, ref stroke, 1) <= 0)
                    {
                        break;
                    }

                    if (device == 1)
                    {
                        input.Add(stroke.key);

                        while (input.Count > 0)
                        {
                            var match = FindMatch(input);
                            if (match == null)
                            {
                                stroke.key = input[0];
                                Interception.Send(context, 1, ref stroke, 1);
                                input.RemoveAt(0);
                                continue;
                            }
                            else
                            {
                                if (match.Length > 0)
                                {
                                    for (var i = 0; i < match.Length; i++)
                                    {
                                        stroke.key = match[i];
                                        Interception.Send(context, 1, ref stroke, 1);
                                    }
                                    input.Clear();
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        var key = stroke.key;

                        for (var i = 0; i < input.Count; i++)
                        {
                            stroke.key = input[i];
                            Interception.Send(context, 1, ref stroke, 1);
                        }
                        input.Clear();

                        stroke.key = key;
                        Interception.Send(context, device, ref stroke, 1);
                    }
                }
            }
            catch (ThreadAbortException)
            {
            }
            finally
            {
                Interception.DestroyContext(context);
            }
        }