1

I am writing an app which uses SendKeys to send text at the cursor whenever text is available. Each time the app has to write the percentage sign, it writes number 5 instead. I send 10{%} and my output is 105. I can't figure how to simply make the % symbol appear.

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

Here is a really simple sample to reproduce the issue (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();
        }
    }
}

Output is 105 instead of 10%.

I can't figure this out, how can I write % symbol using SendKeys ?

Raphaël
  • 160
  • 1
  • 13

3 Answers3

5
string text = "10+ù";

Your culture is fr-fr, you're using an AZERTY keyboard. % is located in shift position with regards to ù, so it should do the trick.

UPDATE: I finally took the time to look it up : https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/SendKeys.cs,a40b41f572ed230f

Have a look at the keywords array. Indeed, {%} and {^} are treated specially and really mean Shift+5 and Shift+6 regardless of keyboard layout. This explains why you're getting 105 when feeding 10{%} to the function with an AZERTY layout.

Laurent LA RIZZA
  • 2,905
  • 1
  • 23
  • 41
1

Use the @ operator before the string value for a string literal.

E.g. string text = @"10{%}";

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

dunnel123
  • 384
  • 1
  • 8
0

That is weird Raphaël. I copy-pasted your code in a Sample C# project and I get 10% as the output. Are you using any of the mentioned supported platforms, from the link you have provided -

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

Community
  • 1
  • 1
Samarth
  • 11
  • 1
  • 6
  • 1
    Laurent solved this : as I'm using an AZERTY keyboard, `{%}` is understood as QWERTY and the key combination results in `5` instead of `%`, which is weird indeed. Thank you for testing! – Raphaël Dec 09 '19 at 15:33
  • nice. Happy to help – Samarth Dec 09 '19 at 15:38
  • @Raphaël: Well, it's weird that it still correctly locates the `'`key, but not the `{%}`. I suppose, because `%` has meaning for `SendKeys`, that MS just hard-coded the `{%}` behaviour as `Shift+5`, disregarding exotic keyboard layouts such as AZERTY. – Laurent LA RIZZA Dec 09 '19 at 15:54
  • "Please. AZERTY is not THAT exotic :)". :). It's really likely as `^` behave the same and gives `6` – Raphaël Dec 09 '19 at 16:05