1

This is the gist the program :

while(true)
{
//bunch of codes that gets data from port ,
//if there is no data it waits here .

}

i am using linux , is there any inbuilt support for keypresses like Ctrl+C . I can catch that using signal(SIGINT, signal_callback_handler); but Ctrl+C has some problem as it gives errors .

i want to get out of this loop on keypress , is this possible ? If yes , how to do it .

rajat
  • 3,415
  • 15
  • 56
  • 90
  • `break` well get you out the loop. to get the key, you well probably need a thread. – elyashiv Oct 20 '12 at 17:53
  • Which OS? On Windows, checking with `GetAsyncKeyState` would humbly suffice for a specific key. – chris Oct 20 '12 at 17:54
  • "if there is no data it waits here" I think you mean *spins* here, unless your port-read is blocking. – WhozCraig Oct 20 '12 at 17:55
  • it;s blocking , it stops there . – rajat Oct 20 '12 at 17:57
  • i am using linux , is there any inbuilt support for keypresses like Ctrl+C . I can catch that using **signal(SIGINT, signal_callback_handler);** but Ctrl+C has some problem it gives errors . – rajat Oct 20 '12 at 17:57
  • 1
    On Linux, you can use NCurses. This has been answered already: http://stackoverflow.com/a/4028974/856199 – Nikos C. Oct 20 '12 at 18:18
  • @rajat Remember than when you use ncurses in your program, you have to link the ncurses library when you build it. Simply add `-lncurses` at the end of your g++ command line. – Nikos C. Oct 20 '12 at 18:33

3 Answers3

0
while(!_kbhit()){
    // do stuff here.
}

Works on Windows.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
0

You can signal an interupt

      #include <csignal>

      raise(SIGINT);

or

     raise(SIGABRT);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The function that can terminate your program is exit().

To input a premature termination of your cicle you can use break.

If you are willing to use some C oriented solution i suggest to take a look at this question.

For something strictly related to linux you probably should refer to the ncurses library in combination with the exit function.

Also do not assume that a particular key combination is equal to a termination, many terminals are not real terminals but emulators, and many emulators are customizable, by the OS creator and by the user.

Community
  • 1
  • 1
Ken
  • 2,105
  • 3
  • 19
  • 22