SendKeys 不发送 '(' 和 ')'

SendKeys not sending '(' and ')'

我想用C#的StreamReader制作文本文件转发器。 但是出现了一些问题...

示例) ** 输入单词 "A"(;!)'B'

** 输出字 "A";!'B'

我不知道为什么“(”和“)”不适用。

我尝试更改 StreamReader 参数,例如 Encoding.Default、utf8、utf32。

但是他们也没有解决

请多指教

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

根据 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.

因此,一种选择是在调用该方法之前将字符串中的所有括号替换为 "{(}""{)}"。您甚至可以创建一个通用方法来转义 所有 个特殊字符。

或者只设置您要发送击键的任何控件的值并避免 SendKeys 一起...