How to output windows Alt keycodes in a C# console app
Asked Answered
G

2

3

How can I output Windows Alt key codes to the console in a C# console app using Console.WriteLine()?

I would like to output characters such as those used for creating boxes. I can do so manually in a command prompt by holding alt and typing in the appropriate number such as Alt+205, Alt+187, etc.

Thanks

Gipon answered 3/6, 2012 at 12:59 Comment(0)
Q
4

I suppose the easiest way would be to include them directly in your string literals within your source code:

Console.WriteLine("═╗");
Quinby answered 3/6, 2012 at 13:8 Comment(2)
Accepted, thank you. I can't believe this never occurred to me! I was too busy thinking in terms of ASCII codes to even consider doing the obvious!Gipon
Be careful that Alt codes do not correspond to ASCII; in fact, and are not even valid ASCII characters (whichever way you specify them), since ASCII values only range from 0 to 127.Quinby
O
2

EDIT: I'm sorry - my answer is incorrect. ASCII.GetChars will not work for extended ASCII characters. Thanks to Douglas for correcting me.


I think Douglas's answer is the most direct, but you could also get the character based on the value directly using something like this:

char[] characters = System.Text.Encoding.ASCII.GetChars(new byte[] {65});

For whatever ASCII code you wanted.

Orectic answered 3/6, 2012 at 13:10 Comment(2)
Thanks, this may be useful for the future.Gipon
This answer is technically incorrect for the characters that the OP requires; and cannot be represented in ASCII. The closest you can get is by replacing Encoding.ASCII with Encoding.GetEncoding(437), but that will not always work since the code page may be absent from the machine.Quinby

© 2022 - 2024 — McMap. All rights reserved.