Exemple #1
0
        private void _HostKeyboard_KeyUp(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            // Handle the key press ASAP.
            var key = (KeyboardKey)args.Key;

            _OutBuffer.KeyUp(key);
            var shiftPressed   = (_Keyboard.GetKeyState(USBH_Key.LeftShift) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightShift) == USBH_KeyState.Down);
            var controlPressed = (_Keyboard.GetKeyState(USBH_Key.LeftCtrl) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightCtrl) == USBH_KeyState.Down);
            var altPressed     = (_Keyboard.GetKeyState(USBH_Key.LeftAlt) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightAlt) == USBH_KeyState.Down);
            var logoPressed    = (_Keyboard.GetKeyState(USBH_Key.LeftGUI) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightGUI) == USBH_KeyState.Down);

            // Handle inactivity and minimum time before start state.
            _MinimumKeystrokes--;

            if (_SelectedFiddle == null && _PublishedFiddle == null && _MinimumDelay < Utility.GetMachineTime() && _MinimumKeystrokes <= 0)
            {
                // Transition to active state by selecting the next fiddle to publish.
                SelectNextFiddle();
            }

            if (_PublishedFiddle != null)
            {
                // Call fiddler to adjust our output.
                _PublishedFiddle.Implementation.ApplyOnKeyUp(_OutBuffer, key, shiftPressed, controlPressed, altPressed, logoPressed);
                if (_PublishedFiddle.Implementation.IsComplete)
                {
                    // Fiddle was applied, schedule the next one.
                    _PublishedFiddle.Implementation.AfterCompletion();
                    _PublishedFiddle = null;
                    _Ui.FiddlesMade  = _Ui.FiddlesMade + 1;
                    SelectNextFiddle();
                }
            }
        }
Exemple #2
0
        private void _HostKeyboard_KeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            // Handle the key event ASAP.
            var key = (KeyboardKey)args.Key;

            _OutBuffer.KeyDown(key);
            var shiftPressed   = (_Keyboard.GetKeyState(USBH_Key.LeftShift) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightShift) == USBH_KeyState.Down);
            var controlPressed = (_Keyboard.GetKeyState(USBH_Key.LeftCtrl) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightCtrl) == USBH_KeyState.Down);
            var altPressed     = (_Keyboard.GetKeyState(USBH_Key.LeftAlt) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightAlt) == USBH_KeyState.Down);
            var logoPressed    = (_Keyboard.GetKeyState(USBH_Key.LeftGUI) == USBH_KeyState.Down) || (_Keyboard.GetKeyState(USBH_Key.RightGUI) == USBH_KeyState.Down);

            // Handle inactivity and minimum time before start state.
            this.SetInactivityTimeout();

            if (_IsInactive)
            {
                // Transition to initial delay state by initialising the minimum variables.
                SetMinimumCounters();
                _IsInactive = false;
            }

            if (_PublishedFiddle != null)
            {
                // Call fiddler to adjust our output.
                _PublishedFiddle.Implementation.ApplyOnKeyDown(_OutBuffer, key, shiftPressed, altPressed, controlPressed, logoPressed);
                if (_PublishedFiddle.Implementation.IsComplete)
                {
                    // Fiddle was applied, schedule the next one.
                    _PublishedFiddle.Implementation.AfterCompletion();
                    _PublishedFiddle = null;
                    _Ui.FiddlesMade  = _Ui.FiddlesMade + 1;
                    SelectNextFiddle();
                }
            }
        }
Exemple #3
0
        private void FiddleHandler(object arg)
        {
            if (_SelectedFiddle == null)
            {
                return;
            }

            // It's now time to publish the chosen fiddle.
            _SelectedFiddle.Implementation.OnPublish(_Rand);
            _PublishedFiddle = _SelectedFiddle;
            _SelectedFiddle  = null;
            _NextFiddleEvents.Change(Timeout.Infinite, Timeout.Infinite);
            _Ui.CurrentState = UnitState.Applying;
        }
Exemple #4
0
        private void _Keyboard_Disconnected(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            // Unhook event handlers and null our host keyboard.
            _Keyboard.KeyUp        -= new USBH_KeyboardEventHandler(_HostKeyboard_KeyUp);
            _Keyboard.KeyDown      -= new USBH_KeyboardEventHandler(_HostKeyboard_KeyDown);
            _Keyboard.Disconnected -= new USBH_KeyboardEventHandler(_Keyboard_Disconnected);
            _Keyboard = null;

            // Null any events / state.
            _NextFiddleEvents.Change(Timeout.Infinite, Timeout.Infinite);
            _InactivityTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _PublishedFiddle   = null;
            _SelectedFiddle    = null;
            _MinimumKeystrokes = Int32.MaxValue;
            _MinimumDelay      = TimeSpan.MaxValue;
            _IsInactive        = false;
            _Ui.CurrentState   = UnitState.None;
        }
Exemple #5
0
        private void SelectNextFiddle()
        {
            // Start by choosing a fiddle to perform.
            var defs = _Config.Definitions;
            FiddleDefinition theChosenOne = null;

            if (defs.Length == 1)
            {
                theChosenOne = defs[0];
            }
            else
            {
                var rand = _Rand.Next();
                for (int i = 1; i < defs.Length; i++)
                {
                    if (rand >= defs[i - 1].Probability && rand < defs[i].Probability)
                    {
                        theChosenOne = defs[i - 1];
                        break;
                    }
                }
                if (theChosenOne == null)
                {
                    theChosenOne = defs[defs.Length - 1];
                }
            }
            _SelectedFiddle  = theChosenOne;
            _Ui.CurrentState = UnitState.Scheduled;

            // Now choose when to perform it.
            var millisecondRange  = (int)((theChosenOne.MaxDelay.Ticks - theChosenOne.MinDelay.Ticks) / TimeSpan.TicksPerMillisecond);
            var millisecondOffset = (int)(theChosenOne.MinDelay.Ticks / TimeSpan.TicksPerMillisecond);
            var delayMilliseconds = _Rand.Next(millisecondRange) + millisecondOffset;

            if (_InDebugMode)
            {
                delayMilliseconds = (int)(delayMilliseconds * _Config.DebugScaleingFactor);
            }
            _NextFiddleEvents.Change(delayMilliseconds, Timeout.Infinite);
        }
Exemple #6
0
        private void InactivityTimerHandler(object arg)
        {
            // First off, deactivate the fiddle handler timer so nothing gets published.
            _NextFiddleEvents.Change(Timeout.Infinite, Timeout.Infinite);

            // Deactivate any fiddlers which may be (or may about to be) active.
            if (_PublishedFiddle != null)
            {
                _PublishedFiddle.Implementation.AfterCompletion();
                _PublishedFiddle = null;
            }
            if (_SelectedFiddle != null)
            {
                _SelectedFiddle.Implementation.AfterCompletion();
                _PublishedFiddle = null;
            }

            // The inactivity timer is now disabled until some keys are pressed.
            _InactivityTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _IsInactive      = true;
            _Ui.CurrentState = UnitState.Inactive;
        }
Exemple #7
0
        public void BeginMonitorKeyboardFrom(USBH_Device device)
        {
            _Keyboard               = new USBH_Keyboard(device);
            _Keyboard.KeyUp        += new USBH_KeyboardEventHandler(_HostKeyboard_KeyUp);
            _Keyboard.KeyDown      += new USBH_KeyboardEventHandler(_HostKeyboard_KeyDown);
            _Keyboard.Disconnected += new USBH_KeyboardEventHandler(_Keyboard_Disconnected);

            // Create a random seed using:
            uint seed = _Config.RandomSeed;                                             // A random number from the internet!

            seed  = seed ^ (uint)(((uint)device.VENDOR_ID << 16) | device.PRODUCT_ID);  // The USB vendor and device ids.
            seed  = seed ^ (uint)(device.ID << 9);                                      // Some other USB ID.
            seed  = seed ^ (uint)(Utility.GetMachineTime().Ticks & 0x00000000ffffffff); // The current time.
            _Rand = new Random((int)seed);

            // Nothing is scheduled up front, we have to wait for the minimum time & keystrokes before anything happens.
            SetMinimumCounters();
            _NextFiddleEvents.Change(Timeout.Infinite, Timeout.Infinite);
            _InactivityTimer.Change(Timeout.Infinite, Timeout.Infinite);
            _PublishedFiddle = null;
            _SelectedFiddle  = null;
            _IsInactive      = false;
        }