示例#1
0
        static readonly Regex RegexCurly    = new Regex(@"^{(.*?)}");      // {...}

        /// <summary>
        /// 将字符串解析为击键动作队列
        /// </summary>
        /// <param name="text">字符串</param>
        /// <returns>击键动作队列</returns>
        public static List <KeyAction> Parse(string text)
        {
            List <KeyAction> dataList = new List <KeyAction>();

            while (!string.IsNullOrEmpty(text))
            {
                KeyAction action   = new KeyAction();
                int       modCount = 0;
                action.Modifiers = ParseModifiers(text, out modCount);
                if (modCount > 0)
                {
                    text = text.Substring(modCount);
                }

                if (text == "")
                {
                    break;
                }

                // 查找(...)
                Match match = RegexBrackets.Match(text);
                if (match.Success)
                {
                    ParseGroup(match.Groups[1].Value, action.KeyList);
                    dataList.Add(action);
                    text = text.Substring(match.Length);
                    continue;
                }

                // 查找{...}
                match = RegexCurly.Match(text);
                if (match.Success)
                {
                    action.KeyList.Add(new KeyPress(match.Groups[1].Value));
                    dataList.Add(action);
                    text = text.Substring(match.Length);
                    continue;
                }

                action.KeyList.Add(new KeyPress(text.Substring(0, 1)));
                dataList.Add(action);
                text = text.Substring(1);
            }

            return(dataList);
        }
示例#2
0
        /// <summary>
        /// Stoke a sequence of keys defined in "keys"
        /// </summary>
        /// <param name="keys">definition of keys, format of which are same with SendKeys</param>
        /// <param name="delay">delay applied between each keystroke, in milliseconds</param>
        public static void KeyStroke(string keys, int delay = 0)
        {
            List <KeyAction> actions = KeyAction.Parse(keys);

            if (actions.Count == 0)
            {
                return;
            }

            foreach (KeyAction action in actions)
            {
                if (action.KeyList.Count == 0)
                {
                    continue;
                }

                ModifiersDown(action.Modifiers);

                foreach (KeyPress press in action.KeyList)
                {
                    if (press.Key == Keys.None)
                    {
                        continue;
                    }

                    for (int i = 0; i < press.Count; i++)
                    {
                        KeyStroke(press.Key);
                        if (delay > 0)
                        {
                            Thread.Sleep(delay);
                        }
                    }
                }

                ModifiersUp(action.Modifiers);
            }
        }