Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-08-2009, 08:07 PM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default C#, kbhit & getch

In C#, what methods take the place of the old kbhit and getch functions in C? (I'm writing a console application in Visual Studio.)
BobLewiston is offline  
Old 01-09-2009, 12:49 AM   #2 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,418

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C#, kbhit & getch

Code:
Console.WriteLine("Press the X key to exit.");
Console.ReadLine();
Then after that you would validate what key was pressed.
__________________

Baez is online now  
Old 01-09-2009, 11:54 AM   #3 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default attention, Baez:

No, I don't want:

1. the character entered to be echoed to screen until my application checks that it was one deemed to be a valid response,

2. to require the user to press the "Enter" key after entering the character constituting their response, and

3. to echo to screen the carriage return and line feed that "enter" entails.

Instead, I want to:

1. detect when any key has been pressed (kbhit), and then

2. retrieve the character entered from the keyboard buffer (getch).

Does anyone know the names of the classes and methods with these functionalities, and what Framework library ("using") files they're in?

Also, does anyone know how to toggle echo-to-screen, as was done in C (if I remember correctly) via system ("echo off") and system ("echo on")?
BobLewiston is offline  
Old 01-09-2009, 12:53 PM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: C#, kbhit & getch

Take a look through all the static methods on the Console class. It is defined in the "System" namespace. Since you're using Visual Studio, use intellisense to look at the methods defined on the Console class. There are several, including Console.ReadKey(), which is what you seem to want.
jaeusm is offline  
Old 01-09-2009, 03:05 PM   #5 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,418

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C#, kbhit & getch

You can use Interop to call _kbhit. The version of kbhit without the underscore is actually depricated. Make sure you use STAThread to ensure everything is synchronized; both COM and Windows objects.

Code:
using System;
using System.Runtime.InteropServices;

public class Win32Interop
{
   [DllImport("crtdll.dll")]
   public static extern int _kbhit();
}

class KBHIT
{
   [STAThread]
   static void Main(string[] args)
   {
      System.Console.WriteLine( "Press any key: " );
      while(!Win32Interop._kbhit());
   }
}

__________________


Last edited by Baez; 01-09-2009 at 03:08 PM.
Baez is online now  
Old 01-09-2009, 04:24 PM   #6 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: C#, kbhit & getch

That code won't run. Actually, it won't compile because the negation operator cannot be applied to any type other than an object of type Boolean. In your case, you're applying it to an integer.

Anyhow, it's a bit overkill to use p-invoke when the Console class provides all the methods (ReadKey() in this case) that do the exact same thing.

Last edited by jaeusm; 01-09-2009 at 04:27 PM.
jaeusm is offline  
Old 01-10-2009, 02:24 AM   #7 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,418

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C#, kbhit & getch

Yeah was in a C++ mindset. Use while(Win32Interop._kbhit() == 0); instead. Just tested it in XP and it worked.
__________________

Baez is online now  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On