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?

  • 3
    See stackoverflow.com/questions/6565924/… – 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
  • 1
    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

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: http://c-faq.com/osdep/cbreak.html

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(http://qt.nokia.com/products/) for example, if you want to stay system independent. Qt offers events for keyboard input.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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