In Ubuntu
, I would like to write a C++
program which quits whenever the key q
is pressed. Until this, the program continues running. But rather than just a loop which checks for input on every iteration, I want to call a function which then calls other functions etc. So, I cannot just keep checking for input in a loop because that loop is never revisited. Do I need to write a separate thread for this, which detects keyboard inputs, or is there something build into Ubuntu which I can use?
Asked
Active
Viewed 5,875 times
-1

Karnivaurus
- 22,823
- 57
- 147
- 247
-
2possible duplicate of [detecting key press in C without prompting the user](http://stackoverflow.com/questions/4293355/detecting-key-press-in-c-without-prompting-the-user) – Borgleader Jan 05 '15 at 18:06
-
This solution just runs a loop though, rather than running a separate thread and allowing the program to run normally in the meantime. – Karnivaurus Jan 05 '15 at 18:30
-
You never said it had to be in a thread. From your question: "Do I need to write a separate thread for this". If you have a requirement, state it clearly. – Borgleader Jan 05 '15 at 18:34
2 Answers
3
It depends on which environment is your program running.
If it runs on the desktop, it is a GUI application, and you need some graphical toolkit like Qt or Gtk (to avoid direct X11 programming). You could consider also libsdl
If it runs in a terminal, you'll better use some terminal library like ncurses (or, if you want a line edition, readline). Read also the tty demystified web page. Notice that the console tty may be raw or cooked (in which case, line buffering happens partly in the kernel!).
You may also want some event loop (but Qt gives you one), so read about poll(2).
If you are not familiar with it, also read Advanced Linux Programming.

Basile Starynkevitch
- 223,805
- 18
- 296
- 547
-1
Just read stdin and check if it's 'q'
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void workerFun(const bool& stopFlag)
{
//Do stuff
if stopFlag
return; // probably want to have some logic to clean up, save progress etc
}
int main()
{
bool flag = false;
//Start the worker in another thread
boost::thread workerThread(workerFun, std::ref<bool>(flag));
//Keep checking if the user wants to quit
do
{
cout<<"Enter q to quit"<<endl;
char input;
cin>>input;
flag = input == 'q' || input == 'Q'
} while (!flag);
//user want's to quit, wait for the worker to stop
workerThread.join();
cout << "Goodbye!"<<endl;
return 0;
}

ventsyv
- 3,316
- 3
- 27
- 49
-
1What if instead of a loop, I want to call a function which does something. During the processing of that function, I also want the program to quit if 'p' is pressed. So, in this case, I cannot just wait until the function returns to check for input again. – Karnivaurus Jan 05 '15 at 18:37
-
That's tricky. How long does it take for the function to finish processing? You can have it run in a separate thread but then you have to signal the function to stop once the user presses 'q'. Is the function you want interrupted running in a loop? – ventsyv Jan 05 '15 at 19:08