C++ single character input

Started by
7 comments, last by jkleinecke 17 years, 7 months ago
Hello, everyone. I am looking for a way to read a single character from the keyboard(a,s,e, etc.) and then act on it, without requiring the user to hit enter each time that they input a character. Does anyone know how I would accomplish this with C++? It's preferably that it be doable using only the standard libraries. Thanks, Spidy
Advertisement
Can't be done within the standard library. As far as c++ and it's libraries are concerned your computer might not even have a keyboard.
Different platforms have different methods of doing this. For instance on Windows you can use _getch()
daerid@gmail.com
I'm on a Mac, although I'd like the code to be as cross-platform compatible as possible. The only solution I've come up with is to use OpenGL's keypress handling hooks, but that seems like a bit much.
Quote:Original post by Spidy
I'm on a Mac, although I'd like the code to be as cross-platform compatible as possible. The only solution I've come up with is to use OpenGL's keypress handling hooks, but that seems like a bit much.
OpenGL doesn't handle input - it's just an Open Graphics Library. Perhaps you mean SDL? If so, the way it works is by basically having a different version for each platform, with each version having the same functions implemented in platform-specific ways.

-Extrarius
If you're using windows, use this:

#include <conio.h>int main(){    char test;    test = _getch();}


if not I know there are some libraries for linux that support it, just google serach it

edit: sorry, just read you're on a mac. I have no experience there.
Maybe try the get function?
Quote:Original post by Anonymous Poster
Quote:Original post by Spidy
I'm on a Mac, although I'd like the code to be as cross-platform compatible as possible. The only solution I've come up with is to use OpenGL's keypress handling hooks, but that seems like a bit much.
OpenGL doesn't handle input - it's just an Open Graphics Library. Perhaps you mean SDL? If so, the way it works is by basically having a different version for each platform, with each version having the same functions implemented in platform-specific ways.

-Extrarius


I realized right after writing the post that I meant GLUT. Whoops.

Quote:Original post by ChopperDave
Maybe try the get function?


get worked, thanks!
Check out the Object Oriented Input System (OIS). It's a cross platform input library.
-----------------------------------Indium Studios, Inc.

This topic is closed to new replies.

Advertisement