Search code examples
c#streamreadersendkeys

SendKeys not sending '(' and ')'


I want to make text file repeater with StreamReader of C#. But some problem is appear...

Example) ** input word "A"(;!)'B'

** Output word "A";!'B'

I don't know why '(' and ')' is not apply.

I tried to change StreamReader parameter such as Encoding.Default, utf8, utf32.

But they are nor solve it

Please advice to me.

   private void button1_Click(object sender, EventArgs e)
    {
        int counter = 0;
        string line;

        stopflag = 0;

        // Read the file and display it line by line.  
        System.IO.StreamReader file = new System.IO.StreamReader(@"target.txt", Encoding.Default);
        //System.IO.StreamReader file = new System.IO.StreamReader(@"target.txt", Encoding.UTF8);
        //System.IO.StreamReader file = new System.IO.StreamReader(@"target.txt", Encoding.UTF32);
        //System.IO.StreamReader file = new System.IO.StreamReader(@"target.txt", Encoding.ASCII);

        SendKeys.Send("%{TAB}");
        System.Threading.Thread.Sleep(2000);

        while ((line = file.ReadLine()) != null)
        {
            //System.Console.WriteLine(line);
            SendKeys.Send(line);
            System.Threading.Thread.Sleep(200);
            SendKeys.Send("{ENTER}");
            System.Threading.Thread.Sleep(1200);
            counter++;

            if (stopflag == 1)
                break;
        }

        file.Close();

        System.Threading.Thread.Sleep(200);
    }

Solution

  • According to the documentation for SendKeys.Send:

    The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use "{+}". To specify brace characters, use "{{}" and "{}}". Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that might be significant when dynamic data exchange (DDE) occurs.

    So one option is to replace all parentheses with "{(}" and "{)}" in your string before calling the method. You might even create a generic method that wil escape all special characters.

    Or just set the value of whatever control you're trying to send the keystrokes to and avoid SendKeys alltogether...