473,624 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Console input problem

JM
Hi,

I need a function for a character based program (console) that works exactly
like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then
move on. Console.Read; Console.ReadLin e;.... all read input until the user
hits <Enter>.
For example, if you get the question "Do you wish to continue? (Y/N)", from
the moment the user presses his "Y" key, I want to continue.
I haven't found a function in the console class that will do this.

Can someone please help me to acompish this? It MUST be possible, no? Maybe
through winAPI?
Thanks in advance,

JFM
PS: Please do not reply by e-mail as the e-mailaddress I entered is fake,
I'm already getting way too much spam!
Nov 20 '05 #1
6 4705
On Fri, 19 Dec 2003 00:46:08 +0100, "JM" <a@b.com> wrote:
Hi,

I need a function for a character based program (console) that works exactly
like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then
move on. Console.Read; Console.ReadLin e;.... all read input until the user
hits <Enter>.
For example, if you get the question "Do you wish to continue? (Y/N)", from
the moment the user presses his "Y" key, I want to continue.
I haven't found a function in the console class that will do this.

Can someone please help me to acompish this? It MUST be possible, no? Maybe
through winAPI?
Thanks in advance,

JFM
PS: Please do not reply by e-mail as the e-mailaddress I entered is fake,
I'm already getting way too much spam!

You can't do it in straight VB.NET. All the console functions wait
until ENTER has been pressed before stuff get's pushed through the
streams.

There's a workaround, if you have Visual C++ .NET

Build the following VC++.NET Code and compile it:

#include "stdafx.h"
#include <conio.h>
namespace Helper
{
public __gc class InKey
{
public:
static System::Int32 ReadKeypress()
{
while ( ! _kbhit() ) {}
return _getch();
}
};
}

Then, reference the .NET DLL in your Console app and call it like
this:

Dim nChar As Integer = Helper.InKey.Re adKeypress()
Hope that helps!
// CHRIS
Nov 20 '05 #2
On Thu, 18 Dec 2003 23:43:04 -0500, Chris Morse <ch***@cosmicwo lf.com>
wrote:
static System::Int32 ReadKeypress()
{
while ( ! _kbhit() ) {}
return _getch();
}


Actually, in the while() loop I would insert a sleep() call for, say,
50 milliseconds.. just to keep the cpu usage down..

// CHRIS

Nov 20 '05 #3
Whidbey promises to make some enhancements in this area, Im not sure if this
is one of them, but there should be full console support like there is in
C++. Rubbish really, you cant even do a clear screen natively.

OHM
Chris Morse wrote:
On Fri, 19 Dec 2003 00:46:08 +0100, "JM" <a@b.com> wrote:
Hi,

I need a function for a character based program (console) that works
exactly like the INKEY$() function in (old) QBasic: Read only 1
keystroke and then move on. Console.Read; Console.ReadLin e;.... all
read input until the user hits <Enter>.
For example, if you get the question "Do you wish to continue?
(Y/N)", from the moment the user presses his "Y" key, I want to
continue.
I haven't found a function in the console class that will do this.

Can someone please help me to acompish this? It MUST be possible,
no? Maybe through winAPI?
Thanks in advance,

JFM
PS: Please do not reply by e-mail as the e-mailaddress I entered is
fake, I'm already getting way too much spam!

You can't do it in straight VB.NET. All the console functions wait
until ENTER has been pressed before stuff get's pushed through the
streams.

There's a workaround, if you have Visual C++ .NET

Build the following VC++.NET Code and compile it:

#include "stdafx.h"
#include <conio.h>
namespace Helper
{
public __gc class InKey
{
public:
static System::Int32 ReadKeypress()
{
while ( ! _kbhit() ) {}
return _getch();
}
};
}

Then, reference the .NET DLL in your Console app and call it like
this:

Dim nChar As Integer = Helper.InKey.Re adKeypress()
Hope that helps!
// CHRIS


Regards - OHM# OneHandedMan{at }BTInternet{dot }com
Nov 20 '05 #4
JM
Hi, thanks a lot for this code, I think I'll get there
with this.
But I only have a basic knowledge of C++ (have learned it
years ago but almost never used it), so I copied your
code into a new C++ .NET Class library, but when I try to
build the project, I get some errors and I don't know
what to do with it...

This is the build output:

------ Build started: Project: ConsoleHelpers,
Configuration: Debug Win32 ------

Compiling...
ConsoleHelpers. cpp
Linking...
ConsoleHelpers. obj : error LNK2001: unresolved external
symbol "int __cdecl _getch(void)" (?_getch@@$$J0Y AHXZ)
ConsoleHelpers. obj : error LNK2001: unresolved external
symbol "int __cdecl _kbhit(void)" (?_kbhit@@$$J0Y AHXZ)
D:\(...)\Visual Studio Projects\Consol eApplication4
\Debug\ConsoleH elpers.dll : fatal error LNK1120: 2
unresolved externals

ConsoleHelpers - 3 error(s), 0 warning(s)
---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


BTW: my news server seems to be down so it might take a
little longer before I can react to any replies...
-----Original Message----- (...)
You can't do it in straight VB.NET. All the console functions waituntil ENTER has been pressed before stuff get's pushed through thestreams.

There's a workaround, if you have Visual C++ .NET

Build the following VC++.NET Code and compile it:

#include "stdafx.h"
#include <conio.h>
namespace Helper
{
public __gc class InKey
{
public:
static System::Int32 ReadKeypress()
{
while ( ! _kbhit() ) {}
return _getch();
}
};
}

Then, reference the .NET DLL in your Console app and call it likethis:

Dim nChar As Integer = Helper.InKey.Re adKeypress()
Hope that helps!
// CHRIS
.

Nov 20 '05 #5
* "One Handed Man [ OHM# ]" <OneHandedMan{a t}BTInternet{do t}com> scripsit:
Whidbey promises to make some enhancements in this area, Im not sure if this
is one of them, but there should be full console support like there is in
C++. Rubbish really, you cant even do a clear screen natively.


See:

<http://longhorn.msdn.m icrosoft.com/lhsdk/ref/ns/system/c/console/console.aspx>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
The problem is that the console is set, by default, to line input mode so
there is nothing in the input buffer until a line has been entered. The
solution is not to use a busy wait but to change the console input mode. The
following is a rough example:

Module Whatever

<System.Runtime .InteropService s.DllImport("ke rnel32")> _
Function SetConsoleMode( ByVal hConsoleHandle As IntPtr, _
ByVal dwMode As Integer) As Integer
End Function

<System.Runtime .InteropService s.DllImport("ke rnel32")> _
Function GetConsoleMode( ByVal hConsoleHandle As IntPtr, _
ByRef dwMode As Integer) As Integer
End Function

Private Const ENABLE_LINE_INP UT As Integer = 2
Private Const ENABLE_ECHO_INP UT As Integer = 4

Private Const CONIN As Integer = 3

Sub Main()

Dim hStdIn As New IntPtr(CONIN)
Dim nMode As Integer
Dim cInput As Char

GetConsoleMode( hStdIn, nMode)
nMode = (nMode And Not ENABLE_LINE_INP UT) _
And Not ENABLE_ECHO_INP UT

If SetConsoleMode( hStdIn, nMode) = 0 Then
'Error do something
Exit Sub
End If

Console.Write(" Do you want to continue?(Y/N)")

Do While True
cInput = Char.ToUpper(Co nvert.ToChar(Co nsole.Read()))
If cInput = "N"c Then
Exit Do
ElseIf cInput = "Y"c Then
Console.Write(c Input & vbCrLf)
Console.Write(" Do you want to continue?(Y/N)")
End If
Loop

End Sub

End Module

"JM" <a@b.com> wrote in message
news:02******** *************** *****@phx.gbl.. .
Hi, thanks a lot for this code, I think I'll get there
with this.
But I only have a basic knowledge of C++ (have learned it
years ago but almost never used it), so I copied your
code into a new C++ .NET Class library, but when I try to
build the project, I get some errors and I don't know
what to do with it...

This is the build output:

------ Build started: Project: ConsoleHelpers,
Configuration: Debug Win32 ------

Compiling...
ConsoleHelpers. cpp
Linking...
ConsoleHelpers. obj : error LNK2001: unresolved external
symbol "int __cdecl _getch(void)" (?_getch@@$$J0Y AHXZ)
ConsoleHelpers. obj : error LNK2001: unresolved external
symbol "int __cdecl _kbhit(void)" (?_kbhit@@$$J0Y AHXZ)
D:\(...)\Visual Studio Projects\Consol eApplication4
\Debug\ConsoleH elpers.dll : fatal error LNK1120: 2
unresolved externals

ConsoleHelpers - 3 error(s), 0 warning(s)
---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


BTW: my news server seems to be down so it might take a
little longer before I can react to any replies...
-----Original Message-----

(...)

You can't do it in straight VB.NET. All the console

functions wait
until ENTER has been pressed before stuff get's pushed

through the
streams.

There's a workaround, if you have Visual C++ .NET

Build the following VC++.NET Code and compile it:

#include "stdafx.h"
#include <conio.h>
namespace Helper
{
public __gc class InKey
{
public:
static System::Int32 ReadKeypress()
{
while ( ! _kbhit() ) {}
return _getch();
}
};
}

Then, reference the .NET DLL in your Console app and

call it like
this:

Dim nChar As Integer = Helper.InKey.Re adKeypress()
Hope that helps!
// CHRIS
.

Nov 20 '05 #7

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

Similar topics

1
5376
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
4
8734
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax. The obvious way to do this is by using the static Console type properties, In and Out. When trying to debug the app in the IDE however, this doesn't appear to work. I've edited the project properties and added the necessary text to the "command...
3
8296
by: Jerry | last post by:
I am having problem with Console.Read(), the Value I entered is not the value that store in my variable. public class Num { int num; Console.Write("Enter a number: "); num=Console.Read(); Console.WriteLine("{0}",num)
5
6333
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a complete project to show what happens. If anybody has an idea of how to fix this, please let me know. Yes I am aware that this is all in .Net 2. Thanks in advance.
12
3737
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would also allow my application to display a Console, but i don't seem to be allowed to do this. I don't see any References that i need to add, so how do i do this.
7
2291
by: mabra | last post by:
Hi All ! Problem:Reading/duplicating "Console.In" fails for unknown reason. I wrote a little console helper tool, named TEE, which just duplicates the StdIn to the StdOut AND an additional stream. Looks working basically and the part that does this is like the following: private static void CopyStream(TextWriter outStream, bool dup) {
2
14361
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to proceed further with the program logic. I had a difficulty in implementing this, as console.readline() indefinitely waits for the user input.
12
6524
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
4
3778
by: jane007 | last post by:
Hello everybody: I am having a problem. On Unix platform, there is a script that need user to input data from console, then when I call Unix perl script from Windows, these is an issue occurs, when I input data and enter "enter" so fast, the Windows console is freezed, I don't know why, does anybody know?Thank you very much. My code like follows:
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.