Click here to Skip to main content
Click here to Skip to main content

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

, 16 May 2004
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)

About the Author

ManiB
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionNot thread safe ? Pinmember_marsim_21-Apr-09 6:59 
AnswerRe: Not thread safe ? PinmemberManiB21-Apr-09 21:30 
General.Net Framework 2.0... and up. PinmemberMartin S. Stoller24-Apr-08 7:40 
AnswerRe: .Net Framework 2.0... and up. PinmemberManiB24-Apr-08 7:53 
GeneralRe: .Net Framework 2.0... and up. PinmemberMartin S. Stoller24-Apr-08 8:32 
QuestionLinking Error in VS2005 Pinmembererictaneda28-Sep-07 3:20 
AnswerRe: Linking Error in VS2005 PinmemberManiB28-Sep-07 5:39 
GeneralNice Pinmembervivekthangaswamy25-Dec-05 23:37 
GeneralRe: Nice PinmemberManiB26-Dec-05 20:43 
GeneralRe: Nice Pinmembertodd101scout1-Nov-06 15:07 
GeneralC code or C file in C# Project PinmemberAndRy82-31-Aug-05 23:21 
AnswerRe: C code or C file in C# Project PinmemberManiB1-Sep-05 0:41 
GeneralRe: C code or C file in C# Project PinmemberAndRy82-1-Sep-05 4:24 
GeneralRe: C code or C file in C# Project PinmemberManiB1-Sep-05 4:29 
GeneralRe: C code or C file in C# Project PinmemberAndRy82-1-Sep-05 5:09 
GeneralRe: C code or C file in C# Project PinmemberManiB1-Sep-05 20:20 
GeneralRe: C code or C file in C# Project PinmemberAndRy82-2-Sep-05 5:04 
GeneralRe: C code or C file in C# Project PinmemberManiB2-Sep-05 5:29 
GeneralReverse video Pinmemberxlar5421-Aug-05 17:45 
GeneralRe: Reverse video PinmemberManiB21-Aug-05 21:26 
Generali try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter26-Jan-05 21:08 
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberManiB26-Jan-05 22:24 
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter26-Jan-05 23:01 
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter30-Jan-05 1:38 
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberManiB31-Jan-05 4:33 

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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 17 May 2004
Article Copyright 2004 by ManiB
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid