Exemple #1
0
        /// <summary>
        /// Types a message
        /// </summary>
        /// <param name="message">Message to type</param>
        /// <param name="speed">Typing speed in keys per second.</param>
        public static void Type(string message, double speed = SPEED_DEFAULT)
        {
            const double HOLD_STD_DEV  = 29;
            const double PAUSE_STD_DEV = 11;
            const double HOLD_RATIO    = 103.0 / (24.0 + 103.0); // 103 ms hold, 24 ms pause is standard
            const double PAUSE_RATIO   = 1.0 - HOLD_RATIO;

            double time_per_key_ms = (1.0 / speed) * 1000.0;
            double hold_mean       = HOLD_RATIO * time_per_key_ms;
            double pause_mean      = PAUSE_RATIO * time_per_key_ms;

            bool shift_down = false;
            NormalDistributor hold_key_random_time = new NormalDistributor(hold_mean, HOLD_STD_DEV, 0);
            NormalDistributor pause_random_time    = new NormalDistributor(pause_mean, PAUSE_STD_DEV, 0);

            try
            {
                foreach (char c in message)
                {
                    KeyData key_data = KeyData.FromCharacter(c);
                    if (key_data.ShiftPressed)
                    {
                        if (!shift_down)
                        {
                            KeyDown(Keys.RShiftKey);
                            shift_down = true;
                        }
                    }
                    else
                    {
                        if (shift_down)
                        {
                            KeyUp(Keys.RShiftKey);
                            shift_down = false;
                        }
                    }

                    TapKey(key_data.KeyCode, hold_key_random_time.GetInt());
                    Thread.Sleep(pause_random_time.GetInt());
                }
            }
            finally
            {
                if (shift_down)
                {
                    KeyUp(Keys.RShiftKey);
                }
            }
        }