2

Okay so I'm making an auto typer and I want the user to be able to enter the keys {}()^+ and have the application out put. I know that you need to format the symbols like SendKeys.Send({^}); but I cant get this to work. Heres what I have so far for my Timer Tick. Also, I have global int blockCount, which tells the program to move on to the next character in the blockText string.
It returns "Group delimiters are not balanced."

  private void timer3_Tick(object sender, EventArgs e)
    {
        string blockText = richTextBox1.Text;

        int blockLength = richTextBox1.TextLength;
        btrand = RandomNumber(75, 200); //I have a method to make a rand num
        timer3.Interval = btrand;
        char[] specialChars = { '{', '}', '(', ')', '+','^' };
        foreach (char letter in blockText)
        {
            for (int i = 0; i < specialChars.Length; i++)
            {
                if (letter == specialChars[i])
                {
                    SendKeys.Send("{" + specialChars[i] + "}");
                    blockText.Remove(blockText.IndexOf(specialChars[i].ToString()));
                }
                else
                {
                    SendKeys.Send(letter.ToString());
                }
            }

        }
        blockCount++;
        if (blockCount >= blockLength)
        {
            blockCount = 0;
        }

    }
4

1 回答 1

0

好的,快速分析,如果我遗漏了什么,请原谅我。

  • 您正在使用blockText作为您的集合进行foreach ,并在找到特殊字符时对其进行操作。这可能很混乱;我会考虑另一种方式来实现这一点。
  • 您正在遍历所有特殊字符,并且对于您无法识别匹配的每个交互,您正在发送当前字母。这意味着您要重新发送specialChars数组中元素数减一的所有非特殊字符。我不认为那是你打算做的。

我会建议这样的实现:

    foreach (char letter in blockText)
    {
        bool _specialCharFound = false;

        for (int i = 0; i < specialChars.Length; i++)
        {
            if (letter == specialChars[i])
            {
                _specialCharFound = true;
                break;
            }
        }

        if (_specialCharFound)
            SendKeys.Send("{" + letter.ToString() + "}");
        else
            SendKeys.Send(letter.ToString());
    }

有更优化的实现方式,但出于目的清晰和与原始代码的相似性,我会选择这种方式。

于 2013-08-17T18:25:27.750 回答