Distinguishing Printable Chars on Console

Hi all;

I am renovating an old DOS app that takes its input from the console
using getch. For some reason, getch doesn't seem to work anymore.
I am looking at the option of using the .NET Console.ReadKey(Boolean)
method as a replacement. I need to be able to distinguish between the
printable characters (" " to "~"), and the non-printable (control)
characters.

From my research so far, I see that I can get the hash code, as well
as the integer value of the enumerator returned by the call to ReadKey.
It would seem that, for non-printable (control) characters, the hash
code is zero, with the exceptions of backspace (8), tab (9), return (13),
and escape (27). The hash code for the printable characters start with 32.

Is there a better way to do this?

Is there anything in .NET that will emulate the functionality of getch?

I am including a modified version of the example code from the MS docs
for your amusement.

=====================================================================

// This example demonstrates the Console.ReadKey(Boolean) method
using namespace System;
using namespace System::Text;
int main()
{
ConsoleKeyInfo cki;

// 0 1 2 3 4 5 6
// 123456789012345678901234567890123456879012345678901234567890
String^ m1 = "This example discovers the console and modifier keys "
"that you press, \n"
"and optionally echoes the character that corresponds to "
"the console \n"
"key in column 1.\n";
String^ m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, "
"and a console key.\n"
"Press the plus key (+) to toggle echoing the key character "
"in column 1.\n"
"Press the escape (Esc) key to quit: ";
String^ m3 = "> You pressed >";
String^ m4 = "< (character '{0}') - (hash code '{1}') - (val '{2}').";
String^ m5 = "< (echo {0})";
StringBuilder^ sb = gcnew StringBuilder;
Boolean suppress = false;
int key_hash_code, key_val;

//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.
//
Console::TreatControlCAsInput = true;
Console::WriteLine( m1 );
do
{
Console::WriteLine( m2 );
sb->Length = 0;
cki = Console::ReadKey( suppress );
sb->Append( m3 );
if ( (int)cki.Modifiers )
{
if ( (int)(cki.Modifiers & ConsoleModifiers::Alt) )
sb->Append( "ALT+" );
if ( (int)(cki.Modifiers & ConsoleModifiers::Shift) )
sb->Append( "SHIFT+" );
if ( (int)(cki.Modifiers & ConsoleModifiers::Control) )
sb->Append( "CTL+" );
}
sb->Append( cki.Key.ToString() );
key_hash_code = cki.GetHashCode();
key_val = (int) cki.Key;
sb->AppendFormat( m4, cki.KeyChar, key_hash_code.ToString (),
key_val.ToString () );
sb->AppendLine()->AppendLine();
Console::WriteLine( m5, (suppress ? (String^)"OFF" : "ON") );
Console::WriteLine( sb );
if ( cki.KeyChar == '+' )
{
suppress = !suppress;
}
}
while ( cki.Key != ConsoleKey::Escape );
}

/*
This example produces results similar to the following text:

This example discovers the console and modifier keys that you press,
and optionally echoes the character that corresponds to the console
key in column 1.

Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
+ (echo ON)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
(echo OFF)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
(echo OFF)
You pressed Add (character '+').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
I (echo ON)
You pressed SHIFT+I (character 'I').


Press any combination of CTL, ALT, and SHIFT modifier keys, and a console key.
Press the plus key (+) to toggle echoing the key character in column 1.
Press the escape (Esc) key to quit:
? (echo ON)
You pressed Escape (character '?').

*/

=====================================================================


Thanks!
Last edited on
Topic archived. No new replies allowed.