Unicode values

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

I am not sure where to start on this.

Does someone have a code snippet which will wait for a keypress or
combination keypress & give me its utf8 value?

e.g

ESC // keypress
Ctrl + C // combination keypress

Thanks.
 
How about (below, use [Ctrl]+[Break] to end):

using System;
using System.Text;
static class Program
{
static void Main()
{
Console.TreatControlCAsInput = true;
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
WriteChar(key.KeyChar);
}
}
static void WriteChar(char c)
{
char[] chars = { c };
byte[] bytes = Encoding.UTF8.GetBytes(chars);

foreach (byte b in bytes)
{
Console.Write(b.ToString("x2"));
}
Console.WriteLine();
}
}
 
Thanks Marc. I'll try your solution momentarily.

If I am using SendKeys to simulate the left arrow being pressed 10 times,
I'd send {LEFT 10}

If I am reading the keystrokes to send to a window from a file, how do
write {LEFT 10} to the source file in the first place?

Thanks.
 
If I am reading the keystrokes to send to a window from a file, how do
write {LEFT 10} to the source file in the first place?

I might have misunderstood the question, but how about:

File.WriteAllText(path, "{LEFT 10}");

Marc
 
I might have misunderstood the question,

You might have. Currently, I read the input/source one byte at a time; so if
I read

\r I send {Enter}
\t I send {TAB}

here's my problem

[what to I need to read] I send {LEFT 10}

Thanks for your help.
 
Well, reading one *byte* at a time is a little dangerous anyway (unless
you know the data is ASCII or another single-byte encoding [UTF8 is not
single-byte for most of the possible chars]).

I'm not entirely sure whether your input is meant to be in plain text,
or SendKeys format; if plain text, and you want to send the literal
"{LEFT 10}", then you need to escape the reserved chars - i.e. any of:
+, ^, %, ~, {, }, [, ], (, )

The rules for this are here:
http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx

If the file is meant to be already in SendKeys format, why do you need
to parse \r etc?

Anyway, you could use StreamReader to read it a line at a time, and send
lines rather than individual characters - although if the file is in
SendKeys format, I think I would not send anything for CR/LF, and just
use it to break up the text - i.e. I might have:


some~
text~
that
might
be~
on~
different
lines

In addition to solving the issue with sending "{", "E", "N", "T", "E",
"}", this would involve fewer IPC calls, so should be quicker.


Marc
 
like so, but I'm using a string for soruce instead of a file (same
difference...)

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
static class Program
{
static void Main()
{
Process proc = Process.Start("notepad.exe");

Console.WriteLine("Please ensure notepad is active!");
Thread.Sleep(5000);

string INPUT = @"some~
text~
that
might{TAB}hic
be~
on~
different
lines";
using (TextReader reader = new StringReader(INPUT))
{
string line;
while ((line = reader.ReadLine()) != null)
{
SendKeys.SendWait(line);
}
}
}

}
 
Thanks for your help and sample code.

I need to have a clearer grasp of what I am doing, I think.
 
Back
Top