Version 31 of Consio

Updated 2017-06-13 15:32:11 by MJK

Consio is a small console library for Windows. I created it, so I can make small textbased menus and catch single keystrokes and not always use Tk for creating user interfaces. It's also handy for asking passwords from user and not echo them back to the screen.

The library is written in C and has been compiled with Cygwin tools under Cygwin environment using MinGW libraries and headers (so there are no dependencies to the Cygwin).

The source code is available at GitHub .

Binary files can be downloaded from here:

The whole package is released under the BSD license.

Here's a screenshot:

http://www.omanurkka.fi/files/Consio.jpg

The library is pretty skimpy (well, it's version 0.3 at the moment, so don't expect too much), but it already implements the following commands:

Consio::clrscr
Clears screen (console buffer).
Consio::gotoxy x y
Sets a new cursor location to (x;y).
Consio::wherex
Returns the X position of the cursor in the console buffer.
Consio::wherey
Returns the Y position of the cursor in the console buffer.
Consio::bufferwidth
Returns the width of the console buffer.
Consio::bufferheight
Returns the height of the console buffer.
Consio::getch
Waits for single key press.
Consio::getche
Waits for single key press and echoes it back to the console.
Consio::putch chr
Inserts a character to the cursor location.
Consio::kbhit
checks if there are any keystokes waiting in the input buffer. This function doesn't block or remove anything from the buffer. The function will return 1 if there are keystrokes waiting in the input buffer. Otherwise returns 0.
Consio::textattr foreground background
Sets foreground and background color. Available color attributes for both foreground and background color are:
black blue green cyan red magenta brown lightgray darkgray lightblue lightgreen lightcyan lightred lightmagenta yellow white
Consio::cputs ?-nonewline? string
Prints string to standard output. If -nonewline is specified, then then the newline will not be printed at the end of the line.
Consio::cgets
Reads keystrokes from the console until Enter key is pressed. Linefeed or newline characters are not included in the result.
Consio::cgetse
Reads keystrokes from the console and echoes them back until the Enter key is pressed. Linefeed or newline characters are not included in the results.
Consio::getchex
Extended getch. Getch is unable to return a meaningful code for all keys. For example, there is no easy way to return arrow keys as a single character. Getchex works like getch except it will return key presses as an integer value. This way it's possible to receive a code from all keys, including shifts, function keys, arrows etc. The code is device-independent and defined by Windows.
Consio::getkeystate
See GetAsyncKeyState in MSDN
Consio::getch2
Waits for a keypress. Doesn't echo it to the console. This is a replacement for Consio::getch function. This is based on MSVCRT implementation. See more information here: getch in MSDN

mjk 2005-10-17: At the moment, this library has been tested only under Windows XP. All feedback is welcome and I would appreciate it greatly if people would tell me, how the library works under different flavors of Windows. The feedback can be appended to this page or sent directly to me (e-mail address is included in the package).

jmn 2005-10-18 Nice work - thanks for sharing.

It works fine on my old Windows NT & 2000 server boxes.

A consequence of course of using ReadConsole instead of lower level Console API functions such as ReadConsoleInput; is that Consio as it stands doesn't catch certain keys such as arrows, delete, end etc.. Perhaps 'press any key' in the demo should be qualified a little - otherwise the old joke about lusers hunting for the key marked 'any' just isn't as funny ;)

LES: this is very nice, and seems to work in my Windows 98. I couldn't make getch and getche work, but I suspect I just did something wrong.

mjk 2005-10-20: jmn, the "any key" issue will be fixed in next version, so no stupid user will be lost in the quest for finding the "any key" :) LES, I'm afraid that there is a small possibility that (at least at this point) this library will not work flawlessly under Windows 9x. Microsoft documentation claims that all functions I'm using are supported under older operating systems, but I'm not sure, if I can trust that. But I'm going to study this issue further at some point.

mjk 2005-10-20: Well, there is now the version 0.2 available and it among other things has an extended version of the getch command called "getchex", which will capture any keypress from the console and return a key code.

mjk 2017-06-10: Almost 12 years has passed and here we are, with a new version :) For source code and compiled binaries, please see the description above. The main reason for this release is the new command: Consio::getch2, which allows capturing key combinations, for example Alt+F1 and also recognizes arrow keys etc. with modifiers (Alt+arrow up, etc.)


Here's a conversation about this page, from the TkChat channel, on 2006-03-14. willdye

 <mAcZ>   is there possible to write "raw" to console?
 <mAcZ>   windows console supports command "prompt" which can allows ASCII
 <mAcZ>   and i wanna try to use this

 tclguy   ??

 kbk      mAcZ - What are you trying to do, in more general terms.

 <mAcZ>   i wanna use ascii chars in windows console, to use color for text

 aku      I would say that you want ANSI sequences, not ASCII

 kbk      Windows console - the black thing from a command line application, or the
          Wish console? The answers are different for the two.

 <mAcZ>   the black thing

 * kbk tries to remember what's to be done for the Windows console - suspects
       that the answer is "it's not easy."

 tclguy   that requires C API stuff IIRC, not just terminal control codes like a
          unix tty

 kbk      tclguy - ISTR there's a way to jigger a Command Prompt to take ANSI escape
          sequences, but don't remember what it is.  (And I don't remember whether it
          takes C code that we don't have)

 kbk      console I/O extension - http://wiki.tcl.tk/14850

 <mAcZ>   AH!
 <mAcZ>   perfect, big thanks

 * kbk bows. :)

What a pitty that this package didn't get further... And unfortunally, there's no modern CTk available for win32.

APN TWAPI's console module provides (I believe) a superset of these commands. MHo Yes. But what's really neaded is a cross platform CTk, otherwise one had to reinvent the wheel... And TWAPI, consio and ::term are not crossplattform. It's simply not possible to develop cross-platform tcl apps with a tui interface.