Detect Console Keypress


I'm aware that this type of function is OS-dependant, but I'm hoping to find the best way to implement this functionality (particularly on Windows, but if there's a universal solution I don't know, I'm open to suggestions).

I'm trying to detect a keypress without the user pressing enter at the end. For angband/rogue style controls.

Is http://msdn.microsoft.com/en-us/library/kswce429%28v=VS.80%29.aspx the best solution? (_getche)

Thanks
The best solution is curses library -- it's cross-platform (works both on windows and Linux)
Download it here (for windows) http://sourceforge.net/projects/pdcurses/files/
Example :
1
2
3
4
5
6
7
8
9
10
11
#include <curses.h>
int main()
{
initscr(); // init curses

printw("Hello world!\nPress any key to continue...");
getch(); // that's what you need


endwin(); // end curses mode
}

Hope this helps.
curses just looks a little... extreme from glancing at the win32 code. It includes all of the windows libraries, creates various handles and instances... it just seems a little crazy when that _getche command is a single line and a single library.

Am I just being silly?
I don't know anything about windows programming--I'm just a beginner myself, but you may be able to use getchar() from cstdio.h , assuming that a c standard library is cross-platform.
http://www.cplusplus.com/reference/clibrary/cstdio/getchar/
You need to #include windows stuff to use curses on windows, since the windows stuff is the underlying platform API.

To get unbuffered input, you must first set your console input to "raw" or "unbuffered" mode.
For examples, see here:
http://www.cplusplus.com/forum/articles/7312/#msg33734

A Windows function: http://www.cplusplus.com/forum/beginner/3523/#msg15435

Hope this helps. If you need more, post back.
Topic archived. No new replies allowed.