Example #1
0
        static Random rand = new Random(); //reuse this if you are generating many
        private bool _keyUp(Hooker.KeyboardHookStruct arg)
        {
            int  vk      = arg.VirtualKeyCode;
            bool passKey = _processKeyUp(vk, arg.IsInjected);

            if (!arg.IsInjected && vk == lastVk)
            {
                lastVk      = -1;
                prevMacroVk = -1;
            }
            if (passKey)
            {
                RegisterKeyUp(vk);

                // Prevent impossibly fast keystrokes!
                if (lastEvt.ElapsedMilliseconds <= 3)
                {
                    //System.Diagnostics.Debug.WriteLine("delay key up for " + Macro.GetKeyString(vk, ModifierKey.None) + ": " + passKey.ToString().ToUpper());
                    // Now, this used to be an issue when events would be stacked up due to being blocked. Now it's not the case...
                    AccurateTimer.AccurateSleep(5);
                }
                lastEvt.Restart();
            }
            //System.Diagnostics.Debug.WriteLine("pass key up for " + Macro.GetKeyString(vk, ModifierKey.None) + ": " + passKey.ToString().ToUpper());
            return(passKey);
        }
        static void eventsender()
        {
            while (true)
            {
                lock (bufferlock)
                {
                    if (input_queue.Count == 0)
                    {
                        Monitor.Wait(bufferlock); // Release and wait to be pulsed so we can re-acquire.
                    }
                    output_queue.AddRange(input_queue);
                    input_queue.Clear();
                }

                // Flush the output queue
                while (output_queue.Count != 0)
                {
                    // If the KeyUp bit is not set, then this is a KeyDown event.
                    bool isDown = (output_queue[0].Data.Keyboard.Flags & (uint)KeyboardFlag.KeyUp) == 0;

                    // If the last event was > 200ms ago, we dispatch the event immediately.
                    if (lastEventTimer.ElapsedMilliseconds < 200)
                    {
                        // Perform the delay
                        AccurateTimer.AccurateSleep(isDown ? RandSleepDown() : RandSleepUp());
                        // Todo - maybe dont delay the up events huh
                    }

                    // Dispatch the event actually now
                    InputSender.DispatchInput(new INPUT[] { output_queue[0] });

                    // Remove this ( we should really use a queue because this has to shift all those elements over )
                    output_queue.RemoveAt(0);

                    // Restart the 'last event timer'
                    lastEventTimer.Restart();
                }
            }
        }
Example #3
0
        private bool _keyDown(Hooker.KeyboardHookStruct arg)
        {
            int vk = arg.VirtualKeyCode;

            bool passKey = _processKeyDown(vk, arg.IsInjected, vk == lastVk);

            if (!arg.IsInjected && vk != lastVk)
            {
                lastVk = vk;
            }

            /*
             * BeginInvoke((MethodInvoker)delegate
             *  {
             *      Text = "last repeat : " + (lastVk == -1 ? "(none)" : Macro.GetKeyString(lastVk, ModifierKey.None));
             *  });*/

            if (passKey)
            {
                RegisterKeyDown(vk);
                //In general, macro keys should clear the previously repeating key?

                // Prevent impossibly fast keystrokes!
                if (lastEvt.ElapsedMilliseconds <= 3)
                {
                    //System.Diagnostics.Debug.WriteLine("delay key down for " + Macro.GetKeyString(vk, ModifierKey.None) + ": " + passKey.ToString().ToUpper());

                    AccurateTimer.AccurateSleep(4);
                }
                // If the keystroke was passed through, we restart the timer.
                lastEvt.Restart();
            }

            //System.Diagnostics.Debug.WriteLine("pass key down for " + Macro.GetKeyString(vk, ModifierKey.None) + ": " + passKey.ToString().ToUpper());
            return(passKey);
        }