Reputation: 21
I am trying to send "{" and "}" using SendKeys.Send()
I already tried to use string.Replace{"{", "{{}"}
txt = txt.Replace("{", "{{}");
txt = txt.Replace("}", "{}}");
SendKeys.Send(txt);
I expected it to send "{" and "}" but the Program throws an error
System.FormatException
Upvotes: 1
Views: 1935
Reputation: 7350
It is quite clearly documented on the official docs
Here the relevant bit:
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.
The problem with your solution is that you make 2 separate replacements.
Take the input string "{".
txt = txt.Replace("{", "{{}"); // input becomes "{{}"
txt = txt.Replace("}", "{}}"); // input becomes "{{{}}"
SendKeys.Send(txt); // error!
I'm pretty sure there is some solution more elegant than this, but you could try something like:
var sb = new StringBuilder(txt.Length);
for(var i = 0; i < txt.Length; i++)
{
var c = txt[i];
switch (c)
{
case '+':
case '^':
case '%':
case '~':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
sb.Append('{');
sb.Append(c);
sb.Append('}');
break;
default:
sb.Append(c);
break;
}
}
SendKeys.Send(sb.ToString());
Edit extended the escaping with a switch and a for to account for the other escapable chars.
Upvotes: 4
Reputation: 5122
As others have pointed out your second Replace
, replaces the curly braces added by your first Replace
.
To escape the curly braces within a string you could use Regex
like this:
SendKeys.Send(Regex.Replace(txt, @"[/{/}]", m => string.Format("{{{0}}}", m.Value)));
Or even better escape all characters that have special meaning in this context ie. Regex.Replace(txt, @"[/{/}/+/%/~/(/)]", m => string.Format("{{{0}}}", m.Value))
Another completely different option would be to send characters one at the time and escape curly braces when necessary:
"some{}text".ToCharArray().ToList()
.ForEach(a=> SendKeys.Send(a == '}' || a == '{' ? "{"+a+"}": a.ToString()));
Upvotes: 1
Reputation: 112402
{
has to be escaped by {{}
. }
has to be escaped by {}}
. This attempt to escape ...
// NON-WORKING EXAMPLE!
txt = txt.Replace("{", "{{}");
txt = txt.Replace("}", "{}}");
... has the problem, that right braces introduced by the first Replace are escaped again by the second replace. So starting from {
you get
{ --> {{} --> {{{}}
which is wrong. To avoid escaping braces introduced precisely as escapes, use a surrogate character for the closing escape brace in the first Replace. E.g. some Chinese character 閉
, assuming that you will never want to send this character.
txt = txt.Replace("{", "{{閉");
txt = txt.Replace("}", "{}}");
// After having done the escape, replace the surrogate by the closing brace again.
txt = txt.Replace("閉", "}");
If it is not sure whether a single character will never be sent, use a improbable character sequence as surrogate, possibly from different alphabets like 閉©ξ
(Chinese, Latin punctuation, Greek).
Upvotes: -1
Reputation: 1269
I would do it using a different variable to hold the entire string:
var txt = "asdf{}Asdf";
var newString = new StringBuilder();
for (int i = 0; i < txt.Length; i++)
{
if (txt[i] == '{')
newString.Append("{{}");
else if(txt[i] == '}')
newString.Append("{}}");
else
newString.Append(txt[i]);
}
SendKeys.Send(newString.ToString());
Upvotes: 1