2

I'm writing a program that needs to respond towards a key press (say by printing "hello key press") in real time (the program runs in a giant loop that takes around 2 seconds to complete). I have found one potential answer Detecting keydown and keyup events on Linux C++ but the answers weren't very clear to me (I also looked into the 4 answer that are linked via duplicates). Can somebody please provide a simple code example of how to make a linux program respond towards a keypress by printing a single line (or doing whatever) without having to check for it each loop?

Community
  • 1
  • 1
Thijser
  • 2,625
  • 1
  • 36
  • 71
  • Please read all those answers. They contain what you are looking for. (The link you provided links to several more... read them *all*). – elcuco Aug 13 '14 at 12:14
  • 1
    possible duplicate of [Detecting keydown and keyup events on Linux C++](http://stackoverflow.com/questions/13018150/detecting-keydown-and-keyup-events-on-linux-c) – elcuco Aug 13 '14 at 12:15
  • I did read them I just couldn't quite figure out how they worked. – Thijser Aug 13 '14 at 12:18
  • It's not clear what you are trying to do with the 2 second loop. Do you want to detect events at one point in the loop? at several predefined points? anywhere in the loop? What do you want to do with these events? – n. m. could be an AI Aug 13 '14 at 12:18
  • The 2 second loop means that as it has to react in real time I cannot wait for the loop to reach any particular piece of code as I would otherwise do. – Thijser Aug 13 '14 at 12:19
  • So your program has to do two things: run the loop, and react to the keyboard. Are these two things connected to each other in any way? How? – n. m. could be an AI Aug 13 '14 at 12:42
  • The main loop is an opencv program that runs a series of image manupulations based on keypresses. Because I need the screen for these images an gui will not work. – Thijser Aug 13 '14 at 12:44
  • Does a single atomic image manipulation take 2 seconds? If so, how do you plan to react to keypresses that come in more often than each 2 seconds? If a single image manipulation doesn't take 2 seconds, what does? – n. m. could be an AI Aug 13 '14 at 12:47
  • The keys do things like "cancel this" and "next image" or select what manipulation has to be applied. Some of these actions can take far longer then 2 seconds (20+) so most of these keys start by stopping whatever the program was doing (I have a system for that no need to explain how to do that). – Thijser Aug 13 '14 at 13:11
  • So you want to be able to cancel a long operation. There are about two ways of doing this. (1) Have your long-running thread check for cancel events in regular small intervals. (2) Have your long-running *process* (not thread!) run normally, check for cancel events in another process, and kill the long-running process, or make it stop by some other means, when an event is detected. Now I wonder what kind of system of stopping-whatever-the-program-is-doing you have in place. If it's synchronous, why not check for events inside these synchronous cancellation points? – n. m. could be an AI Aug 13 '14 at 15:01

2 Answers2

3

Take a look at SDL Input events. Simple DirectMedia Layer (SDL) provides a cross platform API developed for things like gaming. It does provide a lot of low level keyboard, mouse etc functionality. A link can be found here.

doron
  • 27,972
  • 12
  • 65
  • 103
1

There are essentially to ways:

If the long loop is long in time, but short in code (that is: it contains another working inner loop that keeps all the time) you can place a event presence check in the most inner loop.

If you cannot rework the long loop, you have most likely to split the application in two distinct threads: one performing event detection and immediate actions, and another one to which to delegate the lengthy operations.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63