Click here to Skip to main content
Licence 
First Posted 16 May 2004
Views 113,518
Bookmarked 39 times

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

By | 16 May 2004 | Article
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ManiB

Web Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNot thread safe ? Pinmember_marsim_6:59 21 Apr '09  
AnswerRe: Not thread safe ? PinmemberManiB21:30 21 Apr '09  
General.Net Framework 2.0... and up. PinmemberMartin S. Stoller7:40 24 Apr '08  
AnswerRe: .Net Framework 2.0... and up. PinmemberManiB7:53 24 Apr '08  
GeneralRe: .Net Framework 2.0... and up. PinmemberMartin S. Stoller8:32 24 Apr '08  
QuestionLinking Error in VS2005 Pinmembererictaneda3:20 28 Sep '07  
AnswerRe: Linking Error in VS2005 PinmemberManiB5:39 28 Sep '07  
GeneralNice Pinmembervivekthangaswamy23:37 25 Dec '05  
GeneralRe: Nice PinmemberManiB20:43 26 Dec '05  
GeneralRe: Nice Pinmembertodd101scout15:07 1 Nov '06  
GeneralC code or C file in C# Project PinmemberAndRy82-23:21 31 Aug '05  
AnswerRe: C code or C file in C# Project PinmemberManiB0:41 1 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberAndRy82-4:24 1 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberManiB4:29 1 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberAndRy82-5:09 1 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberManiB20:20 1 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberAndRy82-5:04 2 Sep '05  
GeneralRe: C code or C file in C# Project PinmemberManiB5:29 2 Sep '05  
GeneralReverse video Pinmemberxlar5417:45 21 Aug '05  
GeneralRe: Reverse video PinmemberManiB21:26 21 Aug '05  
Generali try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter21:08 26 Jan '05  
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberManiB22:24 26 Jan '05  
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter23:01 26 Jan '05  
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberYossi mount sitter1:38 30 Jan '05  
GeneralRe: i try to allocate memory in 'MBConsole'... PinmemberManiB4:33 31 Jan '05  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 17 May 2004
Article Copyright 2004 by ManiB
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid