Exemplo n.º 1
0
        private void PollThread()
        {
            ManagedWrapper.SetFilter(_deviceContext, IsMonitoredDevice, ManagedWrapper.Filter.All);
            int deviceId1;
            int deviceId2;
            var stroke1 = new ManagedWrapper.Stroke();
            var stroke2 = new ManagedWrapper.Stroke();

            while (true)
            {
                var strokes = new List <ManagedWrapper.Stroke>();
                if (ManagedWrapper.Receive(_deviceContext, deviceId1 = ManagedWrapper.WaitWithTimeout(_deviceContext, 10), ref stroke1, 1) > 0)
                {
                    strokes.Add(stroke1);
                    if (deviceId1 < 11)
                    {
                        if (ManagedWrapper.Receive(_deviceContext, deviceId2 = ManagedWrapper.WaitWithTimeout(_deviceContext, 0), ref stroke2, 1) > 0)
                        {
                            strokes.Add(stroke2);
                        }
                    }
                    if (!_block)
                    {
                        for (int i = 0; i < strokes.Count; i++)
                        {
                            var stroke = strokes[i];
                            ManagedWrapper.Send(_deviceContext, _deviceId, ref stroke, 1);
                        }
                    }
                    // Use array for callback, as the callback may be AHK code, and dealing with arrays in AHK is way simpler that Lists
                    var keyEvents = new KeyEvent[strokes.Count];
                    for (int i = 0; i < strokes.Count; i++)
                    {
                        var s = strokes[i];
                        keyEvents[i] = new KeyEvent {
                            Code = s.key.code, State = s.key.state
                        };
                    }
                    _callback(keyEvents);
                }
            }
        }
Exemplo n.º 2
0
        private static void PollThread(object obj)
        {
            var token = (CancellationToken)obj;

            //Debug.WriteLine($"AHK| Poll Thread Started");
            _pollThreadRunning = true;
            int stroke1DeviceId;
            int stroke2DeviceId;

            while (!token.IsCancellationRequested)
            {
                var stroke = new ManagedWrapper.Stroke();
                // While no input happens, this loop will exit every 10ms to allow us to check if cancellation has been requested
                // WaitWithTimeout is used with a timeout of 10ms instead of Wait, so that when we eg use SetState to turn the thread off...
                // ... any input which was filtered and is waiting to be processed can be processed (eg lots of mouse moves buffered)
                if (ManagedWrapper.Receive(DeviceContext, stroke1DeviceId = ManagedWrapper.WaitWithTimeout(DeviceContext, 10), ref stroke, 1) > 0)
                {
                    var strokes = new List <ManagedWrapper.Stroke>();
                    strokes.Add(stroke);
                    if (stroke1DeviceId < 11)
                    {
                        //Debug.WriteLine($"Stroke 1: {RenderStroke(stroke)}");
                        // If this is a keyboard stroke, then keep performing more Receives immediately with a timeout of 0...
                        // ... this is to check whether an extended stroke is waiting.
                        // Unfortunately, at this point, it's entirely possible that two single-stroke keys end up in strokes...
                        // ... or even 3 strokes or more (eg one single-stroke key followed by a two-stroke key)
                        //while ((stroke2DeviceId = ManagedWrapper.WaitWithTimeout(DeviceContext, 0)) == stroke1DeviceId)
                        while ((stroke2DeviceId = ManagedWrapper.WaitWithTimeout(DeviceContext, 0)) != 0)
                        {
                            ManagedWrapper.Receive(DeviceContext, stroke2DeviceId, ref stroke, 1);
                            strokes.Add(stroke);
                            //Debug.WriteLine($"Stroke {strokes.Count}: {RenderStroke(stroke)}");
                        }

                        // Loop through the list checking the first 2 indexes for valid "two-code" key combinations.
                        //   If no combo is found, send index 0 on its way, remove it off the top of the list, repeat
                        while (strokes.Count > 0)
                        {
                            if (strokes.Count >= 2 && ScanCodeHelper.IsDoubleScanCode(new List <ManagedWrapper.Stroke> {
                                strokes[0], strokes[1]
                            }))
                            {
                                DeviceHandlers[stroke1DeviceId].ProcessStroke(new List <ManagedWrapper.Stroke> {
                                    strokes[0], strokes[1]
                                });
                                strokes.RemoveRange(0, 2);
                            }
                            else
                            {
                                DeviceHandlers[stroke1DeviceId].ProcessStroke(new List <ManagedWrapper.Stroke> {
                                    strokes[0]
                                });
                                strokes.RemoveAt(0);
                            }
                        }
                    }
                    else
                    {
                        DeviceHandlers[stroke1DeviceId].ProcessStroke(strokes);
                    }
                }
            }
            _pollThreadRunning = false;
            //Debug.WriteLine($"AHK| Poll Thread Ended");
        }