Hey guys, Well i've been wondering something. How would you go about doing something like say if 'a' or 'A' is pressed do something for example move a file. Thanks, Clipper34.

Thanks for replying Ancient Dragon. But i have another question, how would you go about for a program reading a keystroke for exmaple the above you posted but in a keystroke so, if a is pressed then do something. Thanks, Clipper34.

I think the OP is interested in unbuffered input.

Win32: use <conio.h> stuff ([sub]me goes to wash my hands[/sub]) or the Win32 Console Functions to set the input console mode to 0 and read a single key. (Don't forget to restore the console mode to what it was before!) You can WaitForSingleObject() on the input console handle if you want to test for keypresses.

On POSIX systems, you'd also have to set the console mode to unbuffered (or "raw"), but the code to do that is more than I want to type-in right now, so see option 3:

For cross-platform stuff, use the Curses library.

  • NCurses
    POSIX systems: Linux, Unix, Minix, etc.
    (If you are on Unix, chances are it is already installed. If you are on Linux, sudo apt-get ncurses-dev or whatever your equivalent is.)
  • PDCurses
    Ported for Win32, DOS, OS/2, Plan 9, Nintendo DS, etc.

As long as you stay away from stuff like mouse-handling (via curses) and window-resize notifications your curses programs should recompile without difficulty on either platform. Don't forget to link with -lcurses (or if you are on Solaris, -lncurses ). [edit] oh, and on Windows, with -lpdcurses . [/edit]

I just posted a short example on curses here (another forum, alas. Sooner or later I'm going to post a comprehensive "How to <X> the console" tutorial).

Good luck!

This article has been dead for over six months. Start a new discussion instead.