SendKeys.SendWait("{%}") 写“5”而不是“%”

SendKeys.SendWait("{%}") writes "5" instead of "%"

我正在编写一个应用程序,它使用 SendKeys 在文本可用时在光标处发送文本。每次应用必须写入百分号时,它都会写入数字 5。我发送 10{%},我的输出是 105。我不知道如何简单地使 % 符号出现。

来自 MSDN :

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 "{}}".

这是一个非常简单的重现问题的示例(ConsoleApp with System.Windows.Forms reference)

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Threading.Thread.Sleep(1000);

            string text = "10{%}";

            // Output SHOULD be "10%"
            // Output IS "105"
            System.Windows.Forms.SendKeys.SendWait(text);

            Console.ReadLine();
        }
    }
}

输出是 105 而不是 10%

我搞不懂,如何使用 SendKeys 编写 % 符号?

在字符串文字的字符串值之前使用 @ 运算符。

例如string text = @"10{%}";

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

string text = "10+ù";

您的文化是 fr-fr,您使用的是 AZERTY 键盘。 % 位于 ù 的移位位置,因此它应该可以解决问题。

更新:我终于花时间查了一下:https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/SendKeys.cs,a40b41f572ed230f

查看 keywords 数组。事实上,{%}{^} 被特殊对待并且实际上意味着 Shift+5Shift+6 无论键盘布局如何。这解释了为什么在使用 AZERTY 布局向函数提供 10{%} 时得到 105

拉斐尔真奇怪。我将您的代码复制粘贴到示例 C# 项目中,并得到 10% 作为输出。您是否使用任何上述支持的平台,从您提供的 link -

Applies to

.NET Core

3.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1