Search code examples
c#sendkeys

C# Sendkeys how to send tilde without sending enter


In my code, I am trying to send a tilde (~) to the users program. Everytime I send ~ it presses enter and the code does not get to continue sending the rest of the keys.

I have tried:

SendKeys.SendWait("([~])");
SendKeys.SendWait("~");
SendKeys.SendWait("{[~]}");

they all just press the enter button and don't send the actual tilde


Solution

  • The documentation says:

    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.

    So this should work:

    SendKeys.SendWait("{~}");
    

    The documentation also remarks:

    If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided.