how to run a piece of code by pressing a key and stop the code when pressed again

how to run a piece of code by pressing a key and stop the code when pressed again

Attach your program to a debugger.

1 Like

You might want a thread that runs your code in a loop, but it's hard to give more specific advice with this little information.

Could you give a few more details on what you're trying to do? Eg:

  • is this getting this keypress in a terminal, running in the background, some gui library?
  • do you mean abort whatever long term computation you're performing, cancel some periodic check, an event driven loop?

Especially, if you have a snippet representing the idea you're trying to achieve, and the libraries you're using, if any.

2 Likes

I think it depends on the OS.

On my box, (debian) the steps are pretty simple.
1 I create and executable with the code I want to execute.
2 I point my mouse to the top right corner of my desktop and find "Settings"
3 In my settings, I find "Keyboard Shortcuts"
4 Add a keyboard shortcut, and enter path to executable.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.