C++ single character input

This topic is 4263 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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.

Share on other sites
Different platforms have different methods of doing this. For instance on Windows you can use _getch()

Share on other sites
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.

Share on other sites
Quote:
 Original post by SpidyI'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

Share on other sites
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.

Share on other sites
Maybe try the get function?

Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by SpidyI'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 ChopperDaveMaybe try the get function?

get worked, thanks!

Share on other sites
Check out the Object Oriented Input System (OIS). It's a cross platform input library.

• 20
• 10
• 19
• 14
• 20