Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Article

ConsoleLibrary, C-Functions for use in C# console applications

Rate me:
Please Sign up or sign in to vote.
4.65/5 (20 votes)
16 May 2004CPOL2 min read 166.7K   1.2K   40   43
A C++ class library with functions like GetKey(), SetCursor(), Cls() ... for use in C# console applications

Introduction

While programming my first C# console applications I missed functions to read the console-keycode without waiting for the pressed key. As a C++ programmer I searched for functions like _kbhit() or _getch(). But I only found functions like Console.Read() or Console.ReadLine(). Both are still waiting until a key is pressed. But I do not want to wait. I searched on the CODE PROJECT for the keywords (kbhit, getch, GetKey) to get a quickly solution. But unfortunately I get no single hit!!! So I had to solve it on my own. And here is my solution.

The Solution

I build a C++ Class Library with the necessary classes and functions in it. I named it ConsoleLibrary because there are a couple of functions that are useful for an C# console application. After that I created four C# console applications demos that referenced the ConsoleLibrary, to show you how to manage the new functions.

  • ConsoleDemo1
    This is an example for a keyboard-test, using the ConsoleLibrary.
  • ConsoleDemo2
    This is an example for a display-test, using the ConsoleLibrary.
  • ConsoleDemo3
    This is an example for showing an ASCII-table, using the ConsoleLibrary.
  • ConsoleDemo4
    This is an example for programming a game (SNAKE), using the ConsoleLibrary.

Using the code

There are two kinds of using the ConsoleLibrary functions. First of them is to insert the ConsoleLibrary project in your solution file. After that you can make an direct reference in your console application project to the ConsoleLibrary. In this case your are able to debug into the ConsoleLibrary sourcecode, and you can make changes in it.

The second way to use the ConsoleLibrary functions is to make an reference to the ConsoleLibrary.dll. Use this way when you don't want to make changes anyway.

In both cases don't forget the using ConsoleLibrary;

Then you can directly use the functions in your sourcecode. Here are some examples:

To flush the keyboard buffer:

C#
Keyboard.FlushKeys();

To check the keycode in a loop:

C#
do
{
  // get the key...
  key = Keyboard.GetKey();
  // check the key...
  if (key != (int)KeyCode.KeyNone)
  {
    if (key == (int)KeyCode.KeyF1)
      Console.WriteLine("You've pressed F1.");
  }

  // do something else
  ...
} while (key!=(int)KeyCode.KeyEscape);

To set the caption:

C#
Display.SetConsoleTitleA(Copyright);

To clear the screen:

C#
Display.Cls();

To set fore- and background color:

C#
Display.SetForeColor(Color.Yellow);
Display.SetBackColor(Color.Lightred);

To show or hide the cursor

C#
Display.HideCursor();
Display.ShowCursor();

To set the cursor-position

C#
Display.SetCursorPosition(2,10);

To sleep

C#
Process.Sleep(80);

There are also other functions available. Please have a look in the sourcecode of the ConsoleLibrary.

Points of Interest

With this ConsoleLibrary I was able to transfer my first posted article Einstein's riddle solved in C++ from C++ to C#. If you're interested in this C# sourcecode please give me a hint.

I hope my ConsoleLibrary is also useful for you. If so, I'm happy and wish you kind regards...

Manfred...

History

  • 13.05.2004 Submit this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot thread safe ? Pin
_marsim_21-Apr-09 6:59
_marsim_21-Apr-09 6:59 
AnswerRe: Not thread safe ? Pin
ManiB21-Apr-09 21:30
ManiB21-Apr-09 21:30 
General.Net Framework 2.0... and up. Pin
Martin S. Stoller24-Apr-08 7:40
Martin S. Stoller24-Apr-08 7:40 
AnswerRe: .Net Framework 2.0... and up. Pin
ManiB24-Apr-08 7:53
ManiB24-Apr-08 7:53 
GeneralRe: .Net Framework 2.0... and up. Pin
Martin S. Stoller24-Apr-08 8:32
Martin S. Stoller24-Apr-08 8:32 
QuestionLinking Error in VS2005 Pin
erictaneda28-Sep-07 3:20
erictaneda28-Sep-07 3:20 
AnswerRe: Linking Error in VS2005 Pin
ManiB28-Sep-07 5:39
ManiB28-Sep-07 5:39 
GeneralNice Pin
vivekthangaswamy25-Dec-05 23:37
professionalvivekthangaswamy25-Dec-05 23:37 
GeneralRe: Nice Pin
ManiB26-Dec-05 20:43
ManiB26-Dec-05 20:43 
GeneralRe: Nice Pin
todd101scout1-Nov-06 15:07
todd101scout1-Nov-06 15:07 
GeneralC code or C file in C# Project Pin
AndRy82-31-Aug-05 23:21
AndRy82-31-Aug-05 23:21 
AnswerRe: C code or C file in C# Project Pin
ManiB1-Sep-05 0:41
ManiB1-Sep-05 0:41 
GeneralRe: C code or C file in C# Project Pin
AndRy82-1-Sep-05 4:24
AndRy82-1-Sep-05 4:24 
GeneralRe: C code or C file in C# Project Pin
ManiB1-Sep-05 4:29
ManiB1-Sep-05 4:29 
GeneralRe: C code or C file in C# Project Pin
AndRy82-1-Sep-05 5:09
AndRy82-1-Sep-05 5:09 
GeneralRe: C code or C file in C# Project Pin
ManiB1-Sep-05 20:20
ManiB1-Sep-05 20:20 
GeneralRe: C code or C file in C# Project Pin
AndRy82-2-Sep-05 5:04
AndRy82-2-Sep-05 5:04 
GeneralRe: C code or C file in C# Project Pin
ManiB2-Sep-05 5:29
ManiB2-Sep-05 5:29 
GeneralReverse video Pin
xlar5421-Aug-05 17:45
xlar5421-Aug-05 17:45 
GeneralRe: Reverse video Pin
ManiB21-Aug-05 21:26
ManiB21-Aug-05 21:26 
Generali try to allocate memory in 'MBConsole'... Pin
Yossi_pRJ26-Jan-05 21:08
Yossi_pRJ26-Jan-05 21:08 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
ManiB26-Jan-05 22:24
ManiB26-Jan-05 22:24 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
Yossi_pRJ26-Jan-05 23:01
Yossi_pRJ26-Jan-05 23:01 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
Yossi_pRJ30-Jan-05 1:38
Yossi_pRJ30-Jan-05 1:38 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
ManiB31-Jan-05 4:33
ManiB31-Jan-05 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.