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

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.ReadLine;.... 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 4682
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.ReadLine;.... 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.ReadKeypress()
Hope that helps!
// CHRIS
Nov 20 '05 #2
On Thu, 18 Dec 2003 23:43:04 -0500, Chris Morse <ch***@cosmicwolf.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.ReadLine;.... 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.ReadKeypress()
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@@$$J0YAHXZ)
ConsoleHelpers.obj : error LNK2001: unresolved external
symbol "int __cdecl _kbhit(void)" (?_kbhit@@$$J0YAHXZ)
D:\(...)\Visual Studio Projects\ConsoleApplication4
\Debug\ConsoleHelpers.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.ReadKeypress()
Hope that helps!
// CHRIS
.

Nov 20 '05 #5
* "One Handed Man [ OHM# ]" <OneHandedMan{at}BTInternet{dot}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.microsoft.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.InteropServices.DllImport("kernel3 2")> _
Function SetConsoleMode(ByVal hConsoleHandle As IntPtr, _
ByVal dwMode As Integer) As Integer
End Function

<System.Runtime.InteropServices.DllImport("kernel3 2")> _
Function GetConsoleMode(ByVal hConsoleHandle As IntPtr, _
ByRef dwMode As Integer) As Integer
End Function

Private Const ENABLE_LINE_INPUT As Integer = 2
Private Const ENABLE_ECHO_INPUT 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_INPUT) _
And Not ENABLE_ECHO_INPUT

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(Convert.ToChar(Console.Read()))
If cInput = "N"c Then
Exit Do
ElseIf cInput = "Y"c Then
Console.Write(cInput & 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@@$$J0YAHXZ)
ConsoleHelpers.obj : error LNK2001: unresolved external
symbol "int __cdecl _kbhit(void)" (?_kbhit@@$$J0YAHXZ)
D:\(...)\Visual Studio Projects\ConsoleApplication4
\Debug\ConsoleHelpers.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.ReadKeypress()
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
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...
4
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....
3
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();...
5
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...
12
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...
7
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...
2
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...
12
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...
4
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,...
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: 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
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,...
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
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
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...
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,...
0
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...

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.