473,406 Members | 2,259 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,406 software developers and data experts.

Get One Character from keyboard

Jay
How do I get only one character from the keyboard without having to hit
return like in the C function getch();
Nov 16 '05 #1
1 2644
No support for this in .NET 1.1, however you always can use P/Invoke to
switch the console from line mode to character mode.
using System;
using System.Runtime.InteropServices;

public class Con {
const int STD_INPUT_HANDLE = -10;
const int ENABLE_LINE_INPUT = 0x0002;
const int ENABLE_ECHO_INPUT = 0x0004;
[DllImport("kernel32")]
public static extern IntPtr GetStdHandle( int nStdHandle);
[DllImport("kernel32")]
public static extern bool GetConsoleMode( IntPtr hConsoleHandle, ref int
pmode);
[DllImport("kernel32")]
public static extern bool SetConsoleMode( IntPtr hConsoleHandle, int mode);
public static void EnableCharMode() {
IntPtr hConsole;
int mode= 0;
hConsole = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hConsole, ref mode);

// must turn off echo mode when character input
mode &= ~ (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
SetConsoleMode(hConsole, mode)
}

public static void EnableLineMode() {
IntPtr hConsole;
int mode=0;
hConsole = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hConsole, ref mode);
mode |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
SetConsoleMode(hConsole, mode)
}

Create an instance of the class and call EnableCharMode when single char
input is required, dont forget to reset the console to
linemode before terminating the program.
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Jay" <j@h.com> wrote in message news:aliGc.2937$r3.680@okepread03...
How do I get only one character from the keyboard without having to hit
return like in the C function getch();

Nov 16 '05 #2

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

Similar topics

10
by: Albretch | last post by:
.. Can you define the Character Set for particular tables instead of databases? . Which DBMSs would let you do that? . How do you store in a DBMS i18n'ed users' from input, coming over the web...
3
by: Noah | last post by:
I have a text field in a form. I want the user to be able to click a DELETE button and have the character at the cursor position deleted. This would be just as if the user had pressed the Back...
4
by: siliconmike | last post by:
All I know is that there are 8 bit numbers from 0 to 255 mapped to characters like A, B, C, D and some strange looking ones (like the ones used to make boxes in old PC text modes) all these being...
50
by: The Bicycling Guitarist | last post by:
A browser conforming to HTML 4.0 is required to recognize &#number; notations. If I use XHTML 1.0 and charset UTF-8 though, does &eacute; have as much support as é ? Sometimes when I run...
9
by: jmev7 | last post by:
I'm in the US, and have to constantly take data input from other countries. Some of this data has characters which I can't understand, since it's input from other language keyboards. This prevents...
3
by: AA Arens | last post by:
When I want the first character of a field to be Uppercased, I need to make an input mask, like >L< followed by ??????? for example. But this mask creates ____ in an unfilled field, which I don't...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
5
by: Pieter | last post by:
Hi, How can I convert KeyCode's to the real character? For instance: the row of keys on a keyboard under the F-keys return as Keycode the Keys.Oem1, Keys.Oem2 etc... Depending on the keyboard...
3
by: Tommy Grav | last post by:
A very simple question. I would like to read a single character from the keyboard (y or n). I have tried to look in my Python books and a google search, but have come up empty. I am sure the info s...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.