Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I don't have an actual problem I'm working on, this is more a question of 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).

The reason I'm asking is because a friend has asked me to help him write a UI for a system - and the UI has to run under Linux. I have no idea where to start and rather than turn him down, I thought I'd instead ask you guys: How do you read a keyboard hit without calling a conio.h function?

share|improve this question
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
add comment

1 Answer

up vote 2 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: 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.

share|improve this answer
add comment

Your Answer

 
discard

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.