473,382 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
Nov 16 '05 #1
9 5258
JuanK wrote:
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.

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2
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.

--
William Stacey, MVP

"JuanK" <ju***@etb.net.co> wrote in message
news:60**********************************@microsof t.com...
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.

tx


Nov 16 '05 #3
See my code example.

--
William Stacey, MVP

"Juank" <ju***@etb.net.co> wrote in message
news:29**********************************@microsof t.com...
hey!!!!
off course... i already see the documntation about this function but this not work like getc... because press ENTER key is required to read the input character.

tx anyway


Nov 16 '05 #4
Yes is does. You can read single char without pressing enter. Is this not
your question??

--
William Stacey, MVP

"JuanK" <ju***@etb.net.co> wrote in message
news:49**********************************@microsof t.com...
OK, but this not solve my problem..
please helpme


Nov 16 '05 #5
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...
Nov 16 '05 #6
really i dont see the code taht you post her
please repeat your post with the right code..

tx
Nov 16 '05 #7
It is in the attachment. Do you have attachments turned off or something?

--
William Stacey, MVP

"Juank" <ju***@etb.net.co> wrote in message
news:1D**********************************@microsof t.com...
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 post
tx anywhere.. but preffer post your code in a place taht i can see..

again sorry.. but my english is some poor...


Nov 16 '05 #8
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
}
}

--
William Stacey, MVP

"Juank" <ju***@etb.net.co> wrote in message
news:1D**********************************@microsof t.com...
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 post
tx anywhere.. but preffer post your code in a place taht i can see..

again sorry.. but my english is some poor...


Nov 16 '05 #9
You see it in the other posts yet?

--
William Stacey, MVP

"JuanK" <ju***@etb.net.co> wrote in message
news:B3**********************************@microsof t.com...
really i dont see the code taht you post here
please repeat your post with the right code...

tx


Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
9
by: Carramba | last post by:
#include <stdio.h> int main( void ){ char cQuit = 'a'; char cKommando , artistNamn , skivansNamn , cChar; int cK ; FILE *pekaFile; printf( "l - for read file\n"); printf( "s - for writte...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.