read a single character without press enter.. just like getc in c language

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello,
i'm trying to read a character from console just like getc function in c languaje
i'm trying with WINAPI but dont works at this time..
other methods like clear screen works OK with the WINAPI.. an others to

please helpe and try understandme because i dont speak in english

tx
 
JuanK said:
hello,
i'm trying to read a character from console just like getc function in c
languaje. i'm trying with WINAPI but dont works at this time...
other methods like clear screen works OK with the WINAPI.. an others too

please helpe and try understandme because i dont speak in english.

See Console.Read() in the documentation for an example.

Frans.
 
Here is a little sample that gets single key without enter required using
pinvoke help and example of getting a password at the console with "*"
replacement. HTH.
 
Yes is does. You can read single char without pressing enter. Is this not
your question??
 
yes, this is my Q.
but if you see your own post.
you see that no code appear here..

i really waana see the code.. but i can`t see anything aniwhere in your last pos

tx anywhere.. but preffer post your code in a place taht i can see.

again sorry.. but my english is some poor...
 
really i dont see the code taht you post her
please repeat your post with the right code..

tx
 
Here it is in text:
using System;
using System.IO;
using System.Threading;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;

namespace ConsoleInput
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("kernel32", SetLastError=true)]
static extern IntPtr GetStdHandle(IntPtr whichHandle);
[DllImport("kernel32", SetLastError=true)]
static extern bool GetConsoleMode(IntPtr handle, out uint mode);
[DllImport("kernel32", SetLastError=true)]
static extern bool SetConsoleMode(IntPtr handle, uint mode);

[DllImport("kernel32", SetLastError=true)]
static extern bool FlushConsoleInputBuffer(IntPtr hConsoleInput);

static readonly IntPtr STD_INPUT_HANDLE = new IntPtr(-10);
const int ENABLE_LINE_INPUT = 2;
const uint ENABLE_ECHO_INPUT = 4;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Hit any key to input password:");
int key = GetChar();
Console.WriteLine("{0} was hit.", key);
Console.WriteLine();
Console.Write("Enter password:");
string password = null;
password = GetPassword();
Console.WriteLine("Password is:"+password);
Console.WriteLine("\nHit any key to continue.");
GetChar();
}

/// <summary>
/// Returns an int of the key pressed at the console.
/// This returns when key is hit, so does not require Enter as
Console.Read().
/// </summary>
/// <returns></returns>
static int GetChar()
{
// Turn off console echo.
IntPtr hConsole = GetStdHandle(STD_INPUT_HANDLE);
uint oldMode;
if ( ! GetConsoleMode(hConsole, out oldMode) )
throw new ApplicationException("GetConsoleMode failed");
uint newMode = oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
if ( ! SetConsoleMode(hConsole, newMode) )
throw new ApplicationException("SetConsoleMode failed");
int i;
try
{
i = Console.Read();
return i;
}
finally
{
// Restore console echo and line input mode.
if (!SetConsoleMode(hConsole, oldMode))
throw new ApplicationException("SetConsoleMode failed");
}
} // End GetChar

static string GetPassword()
{
// turn off console echo
IntPtr hConsole = GetStdHandle(STD_INPUT_HANDLE);
uint oldMode;
if (!GetConsoleMode(hConsole, out oldMode))
{
throw new ApplicationException("GetConsoleMode failed");
}
uint newMode = oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
if (!SetConsoleMode(hConsole, newMode))
{
throw new ApplicationException("SetConsoleMode failed");
}
int i;
StringBuilder secret = new StringBuilder();

while (true)
{
i = Console.Read ();
if ( i == 13 ) // break when <return>
break;
if ( i == 8 )
{
if ( secret.Length > 0 )
{
Console.Write("\b \b");
secret.Remove(secret.Length - 1, 1);
}
}
else
{
secret.Append((char) i);
Console.Write ("*");
}
}

Console.WriteLine();

// Restore console echo and line input mode.
if ( !SetConsoleMode(hConsole, oldMode) )
{
throw new ApplicationException("SetConsoleMode failed");
}
return secret.ToString();
} // End GetPassword
}
}
 
Back
Top