Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is a question of about design decisions. I find that there are so many different things I work on where reading a _kbhit() is amazingly useful but I can't find (read: there isn't) any functionality like that in the C++ standard.

  • Example: Have a camera continuously running (and updating an on-screen window) until you hit the "escape" character.
  • Example: Pressing F11 in firefox to maximize the window.
  • Example: Pressing the W key in any FPS game to move forwards

All of these read the keyboard without pressing [enter] between each read. How the devil is that accomplished? Under windows I use getch() in a continuous loop running in its own thread. I'm not sure if that's a good idea performance-wise, but most of the software I write that runs in an OS isn't very time critical (It's mostly command-line UIs that interface with the embedded systems that I develop).

This is for a UI for a system, and the UI has to run under Linux. How do you read a keyboard hit without calling a conio.h function?

share|improve this question
See… – vines Jul 4 '11 at 11:08
Thank you. Do you know if this is the way e.g. OpenOffice uses to read characters, or is there some "best-practices" way that I haven't heard of? – medivh Jul 4 '11 at 11:14
Windowed applications almost always use an event based mechanism (not raw IO commands). There are OS specific such as Win32 or XLib -- but most people use an abstraction layer here such GTK, SDL, or Qt. – edA-qa mort-ora-y Jul 4 '11 at 11:25
Same reason there isn't a C++ standard call to eject the cd drawer - it's system specific. – Martin Beckett Jul 4 '11 at 14:55
up vote 4 down vote accepted

well, the examples you gave are all depending on some gui framework, like the windows api in this special case.

C++ uses a very abstract concept of input with the input stream stuff and the standard doesn't even know about keyboard or key hits in general.

This is why you will need to either do one of the following:

  1. system dependent programming
  2. using a library

Some useful tips for system dependent checking of keys can be found here:

Using libraries will be the far more protable way. If the UI should be graphical, it will deliver some way to check for keyboard input. You could use Qt( for example, if you want to stay system independent. Qt offers events for keyboard input.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.