473,387 Members | 3,787 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,387 software developers and data experts.

Console "Pause"

I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it executes it returns to the prompt. I have tried console.read and console.readline but they both require an <Enter> key to be hit. Any suggestions

Thanks
-KC
Nov 15 '05 #1
4 30319
"=?Utf-8?B?S2Vyd2luIENhYnJlcmE=?=" <an*******@discussions.microsoft.com>
wrote in news:CD**********************************@microsof t.com:
I am writing a console application and I would like to put a "Press
any key..." message once the program is done executing. Currently,
when it executes it returns to the prompt. I have tried console.read
and console.readline but they both require an <Enter> key to be hit.
Any suggestions?

Thanks,
-KC


Hi,

Console.WriteLine("Press any key...");
Console.Read();

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
Nov 15 '05 #2
I tried this but it still requires the <Enter> key. Do you have any other
suggestions?

KC
"Peter Koen" <koen-newsreply&snusnu.at> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
"=?Utf-8?B?S2Vyd2luIENhYnJlcmE=?=" <an*******@discussions.microsoft.com>
wrote in news:CD**********************************@microsof t.com:
I am writing a console application and I would like to put a "Press
any key..." message once the program is done executing. Currently,
when it executes it returns to the prompt. I have tried console.read
and console.readline but they both require an <Enter> key to be hit.
Any suggestions?

Thanks,
-KC


Hi,

Console.WriteLine("Press any key...");
Console.Read();

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------

Nov 15 '05 #3
In article <CD**********************************@microsoft.co m>, Kerwin Cabrera wrote:
I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it executes it returns to the prompt. I have tried console.read and console.readline but they both require an <Enter> key to be hit. Any suggestions?

Thanks,
-KC


You have to use P/Invoke for this to call SetConsoleMode to turn off
ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT. Here is a little program that
I use to grab key values for a batch file menu... It is pretty quick
and dirty, but it does the job :)

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GetKey
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class KeyGrabber
{
// constant values for use with the console api
private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
private const uint ENABLE_LINE_INPUT = 0x0002;
private const uint ENABLE_ECHO_INPUT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_VALUE = new
IntPtr(-1);

// console functions
[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int ret = 0;
string prompt = (args.Length != 0 ? args[0] : string.Empty);

IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
if (stdin != INVALID_HANDLE_VALUE)
{
uint oldMode;

if (GetConsoleMode(stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT)));
if (SetConsoleMode(stdin, newMode))
{
Console.Write(prompt);
ret = Console.Read();
Console.WriteLine();
if (!SetConsoleMode(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 15 '05 #4
Thanks! That worked great!

KC
"Tom Shelton" <to*@mtogden.com> wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
In article <CD**********************************@microsoft.co m>, Kerwin

Cabrera wrote:
I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it
executes it returns to the prompt. I have tried console.read and
console.readline but they both require an <Enter> key to be hit. Any
suggestions?
Thanks,
-KC


You have to use P/Invoke for this to call SetConsoleMode to turn off
ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT. Here is a little program that
I use to grab key values for a batch file menu... It is pretty quick
and dirty, but it does the job :)

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GetKey
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class KeyGrabber
{
// constant values for use with the console api
private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
private const uint ENABLE_LINE_INPUT = 0x0002;
private const uint ENABLE_ECHO_INPUT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_VALUE = new
IntPtr(-1);

// console functions
[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int ret = 0;
string prompt = (args.Length != 0 ? args[0] : string.Empty);

IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
if (stdin != INVALID_HANDLE_VALUE)
{
uint oldMode;

if (GetConsoleMode(stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT)));
if (SetConsoleMode(stdin, newMode))
{
Console.Write(prompt);
ret = Console.Read();
Console.WriteLine();
if (!SetConsoleMode(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
--
Tom Shelton
MVP [Visual Basic]

Nov 15 '05 #5

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

Similar topics

5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
5
by: Brian.Steele | last post by:
Greetings everyone. See http://www.spiceisle.com/cgi-bin/slideshow/slideshow.cgi?dir=brian/personal/2005/uk_trip/images&type=jpg The "Pause" button works in IE, but not in Firefox. Any ideas...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
3
by: RR | last post by:
I have a button on a form (form A) that opens another form. The form that opens (form B) has a listbox that is populated with a call to a function in the "on current" event. When form B with the...
9
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use...
5
by: CoderMarc | last post by:
Hi, I can not get the system pause to work in a simple program. Here is my program below: // i/o example #include <iostream> using namespace std; int main ()
1
by: Wazza | last post by:
G'Day, I have a re-occurring problem and wish to design a framework or pattern to address it. I have some Ideas but I would like to do some brainstorming with my peers before commencing. The...
2
by: Charles Zhang | last post by:
For Win32 application, I could use "__asm pause". However, for X64 platform, the inline assembler is not supported. Is there a compiler intrinsics for "__asm pause"? Thanks Charles Zhang
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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
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...

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.