469,904 Members | 2,535 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

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 30047
"=?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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Danny Anderson | last post: by
11 posts views Thread by Paminu | last post: by
3 posts views Thread by RR | last post: by
2 posts views Thread by Charles Zhang | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.