Click here to Skip to main content
11,428,221 members (62,418 online)
Click here to Skip to main content

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

, 16 May 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

  Keyboard.FlushKeys();

To check the keycode in a loop:

  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:

  Display.SetConsoleTitleA(Copyright);

To clear the screen:

  Display.Cls();

To set fore- and background color:

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

To show or hide the cursor

  Display.HideCursor();
  Display.ShowCursor();

To set the cursor-position

  Display.SetCursorPosition(2,10);

To sleep

  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)

Share

About the Author

ManiB
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionNot thread safe ? Pin
_marsim_21-Apr-09 7:59
member_marsim_21-Apr-09 7:59 
AnswerRe: Not thread safe ? Pin
ManiB21-Apr-09 22:30
memberManiB21-Apr-09 22:30 
General.Net Framework 2.0... and up. Pin
Martin S. Stoller24-Apr-08 8:40
memberMartin S. Stoller24-Apr-08 8:40 
AnswerRe: .Net Framework 2.0... and up. Pin
ManiB24-Apr-08 8:53
memberManiB24-Apr-08 8:53 
GeneralRe: .Net Framework 2.0... and up. Pin
Martin S. Stoller24-Apr-08 9:32
memberMartin S. Stoller24-Apr-08 9:32 
QuestionLinking Error in VS2005 Pin
erictaneda28-Sep-07 4:20
membererictaneda28-Sep-07 4:20 
AnswerRe: Linking Error in VS2005 Pin
ManiB28-Sep-07 6:39
memberManiB28-Sep-07 6:39 
GeneralNice Pin
vivekthangaswamy26-Dec-05 0:37
membervivekthangaswamy26-Dec-05 0:37 
GeneralRe: Nice Pin
ManiB26-Dec-05 21:43
memberManiB26-Dec-05 21:43 
GeneralRe: Nice Pin
todd101scout1-Nov-06 16:07
membertodd101scout1-Nov-06 16:07 
GeneralC code or C file in C# Project Pin
AndRy82-1-Sep-05 0:21
memberAndRy82-1-Sep-05 0:21 
AnswerRe: C code or C file in C# Project Pin
ManiB1-Sep-05 1:41
memberManiB1-Sep-05 1:41 
GeneralRe: C code or C file in C# Project Pin
AndRy82-1-Sep-05 5:24
memberAndRy82-1-Sep-05 5:24 
GeneralRe: C code or C file in C# Project Pin
ManiB1-Sep-05 5:29
memberManiB1-Sep-05 5:29 
GeneralRe: C code or C file in C# Project Pin
AndRy82-1-Sep-05 6:09
memberAndRy82-1-Sep-05 6:09 
GeneralRe: C code or C file in C# Project Pin
ManiB1-Sep-05 21:20
memberManiB1-Sep-05 21:20 
GeneralRe: C code or C file in C# Project Pin
AndRy82-2-Sep-05 6:04
memberAndRy82-2-Sep-05 6:04 
GeneralRe: C code or C file in C# Project Pin
ManiB2-Sep-05 6:29
memberManiB2-Sep-05 6:29 
GeneralReverse video Pin
xlar5421-Aug-05 18:45
memberxlar5421-Aug-05 18:45 
GeneralRe: Reverse video Pin
ManiB21-Aug-05 22:26
memberManiB21-Aug-05 22:26 
Generali try to allocate memory in 'MBConsole'... Pin
Yossi mount sitter26-Jan-05 22:08
memberYossi mount sitter26-Jan-05 22:08 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
ManiB26-Jan-05 23:24
memberManiB26-Jan-05 23:24 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
Yossi mount sitter27-Jan-05 0:01
memberYossi mount sitter27-Jan-05 0:01 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
Yossi mount sitter30-Jan-05 2:38
memberYossi mount sitter30-Jan-05 2:38 
GeneralRe: i try to allocate memory in 'MBConsole'... Pin
ManiB31-Jan-05 5:33
memberManiB31-Jan-05 5:33 
Generalmemory allocation in C file Pin
Yossimount sitter26-Jan-05 4:03
memberYossimount sitter26-Jan-05 4:03 
Questionhow to use C files in C# project ??? Pin
Yossi mount sitter23-Jan-05 23:58
memberYossi mount sitter23-Jan-05 23:58 
AnswerRe: how to use C files in C# project ??? Pin
ManiB24-Jan-05 1:25
memberManiB24-Jan-05 1:25 
GeneralRe: how to use C files in C# project ??? Pin
Yossi mount sitter24-Jan-05 2:49
memberYossi mount sitter24-Jan-05 2:49 
GeneralRe: how to use C files in C# project ??? Pin
ManiB24-Jan-05 4:01
memberManiB24-Jan-05 4:01 
GeneralRe: how to use C files in C# project ??? Pin
Yossi Mount sitter24-Jan-05 22:53
memberYossi Mount sitter24-Jan-05 22:53 
GeneralRe: how to use C files in C# project ??? Pin
ManiB24-Jan-05 23:58
memberManiB24-Jan-05 23:58 
GeneralRe: how to use C files in C# project ??? Pin
Yossi mount sitter25-Jan-05 4:01
memberYossi mount sitter25-Jan-05 4:01 
GeneralRe: how to use C files in C# project ??? Pin
ManiB25-Jan-05 6:42
memberManiB25-Jan-05 6:42 
GeneralRe: how to use C files in C# project ??? Pin
Yossi mount sitter25-Jan-05 7:06
memberYossi mount sitter25-Jan-05 7:06 
Questioncan u use c code in c# project? Pin
yossi mount sitter16-Jan-05 4:35
memberyossi mount sitter16-Jan-05 4:35 
AnswerRe: can u use c code in c# project? Pin
ManiB17-Jan-05 0:44
memberManiB17-Jan-05 0:44 
GeneralRe: can u use c code in c# project? Pin
Anonymous18-Jan-05 2:27
sussAnonymous18-Jan-05 2:27 
GeneralRe: can u use c code in c# project? Pin
David L.D18-Jan-05 8:13
sussDavid L.D18-Jan-05 8:13 
GeneralRe: can u use c code in c# project? Pin
ManiB18-Jan-05 21:48
memberManiB18-Jan-05 21:48 
GeneralRe: can u use c code in c# project? Pin
David L.D19-Jan-05 0:41
sussDavid L.D19-Jan-05 0:41 
GeneralRe: can u use c code in c# project? Pin
ManiB19-Jan-05 5:05
memberManiB19-Jan-05 5:05 
GeneralRe: can u use c code in c# project? Pin
David L.D19-Jan-05 10:09
sussDavid L.D19-Jan-05 10:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 17 May 2004
Article Copyright 2004 by ManiB
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid