سؤال

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;
        }

    }
هل كانت مفيدة؟

المحلول

Ok, quick analysis, so forgive me if I miss something.

  • You're doing a foreach using blockText as your collection, and manipulating it if a special char is found. This can be messy; I would think on another way to implement this.
  • You're looping through all special chars, and for each interaction that you can't identify a match you're sending the current letter. That means you're re-sending all non-special characters for the number of elements in the specialChars array minus one. I don't think that's what you've intended to do.

I would suggest an implementation like this:

    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());
    }

There are more optimized ways to implement, but I would choose this one out of clarity of purpose and similarity to your original code.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top