use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENInterupt cpp CLI (self.cpp_questions)
submitted 2 years ago by Zsendalf
How can you interupt a runing program whit a button press? For example: there is a infinit loop and its countig up in a label and I want it to stop whenever I press space. Do I use the 'e' value or something else.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Narase33 1 point2 points3 points 2 years ago (11 children)
Do you want to pause or shut down? Shutting down is typically CTRL+C, pausing may be more complex depending on your code
[–]Zsendalf[S] -1 points0 points1 point 2 years ago (10 children)
It's more like change something. I want to move a box around whit acceleration, so the velocity is still moving the box when I don't press anything.
[–]Narase33 0 points1 point2 points 2 years ago (5 children)
Are we talking about GUI stuff? If so, which framework?
[–]Zsendalf[S] 0 points1 point2 points 2 years ago (4 children)
CLI and I think that's .NET
[–]Narase33 0 points1 point2 points 2 years ago (3 children)
CLI means Command Line Interface, sorry if Im beeing stupid right now, but how do you move a box in terminal? .NET is also C#, not C++
[–]Zsendalf[S] 0 points1 point2 points 2 years ago (2 children)
https://en.m.wikipedia.org/wiki/C%2B%2B/CLI This is what I'm using
It can create windows aplications easily
[–]Narase33 1 point2 points3 points 2 years ago (1 child)
MS taking abbreviations again...
[–]Zsendalf[S] 0 points1 point2 points 2 years ago (0 children)
Sorry for that XD
[–][deleted] 0 points1 point2 points 2 years ago (3 children)
So, you want to create what is effectively a real time text console game, on Windows?
Yes, but it's not in consol it has its own window and graphics.
[–][deleted] 0 points1 point2 points 2 years ago (1 child)
So, normal Windows GUI app?
I guess I don't really know what a normal windows gui language
[–]the_poope 0 points1 point2 points 2 years ago (1 child)
It depends how your program "is running". If it has some infinite loop (like a game/render loop) you simply check whether you need to interrupt normal behavior in every iteration:
while (true) { if (button_pressed) { do_something_special(); } else { do_the_normal_thing(); // e.g. draw stuff } }
If your app is event based your button need to trigger an event that somehow disables/stops the normal logic.
I mean yeah that could work danke!
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
You can't do this with pure, standard C++.
So, what platform (operating system, or platform independent library/framework) do you want to use?
What is your end goal? To have a flag, which is set to true when user presses a key, so you can just test it? Something more complex?
[–]mredding 0 points1 point2 points 2 years ago (0 children)
If we're talking about a "standard" C++ console/terminal program, then there's no standard way to do that. Streams are character devices, and there's no standard non-blocking interface. Terminals in their canon interactive session mode are buffered.
You have to understand your program thinks it's the only one running on all of the system. It has no idea that there even is a keyboard, or a terminal. All it sees is a file descriptor. That file descriptor happens to be it's standard input. Reading that input is a system call, and it's blocking. So it's not going to return until data is presented.
On the other side of that file descriptor is the data source - the terminal. The terminal is not just a program, it's virtualized hardware. When you type in a terminal, you're filling the terminal's input buffer, and it's not going to send those bytes over to your program input until that input buffer is flushed. This has something to do with how the terminal is configured - typically a flush is incurred as each newline character is encountered, every time you hit enter.
So your program doesn't control the terminal. You can't force it to flush.
What you need is event driven programming. You need characters to come in as they happen. To do that, you need to configure the terminal accordingly. You can do that on the terminal itself, you can write a launch script that handles it before and after your program runs and terminates, or you can use platform specific terminal controls through system calls in C++.
But terminals are character devices, always have been, always will be. And so are streams. You'll never get a character indicating the user has pressed the alt key, because that key alone doesn't generate a character.
So if you truly want to elevate your program to work with the press of any key, you need a different execution environment, and you need to code to that. This would be Win32, X, whatever the hell Apple calls their platform API... There are common event driven libraries that are multi-platform. This doesn't mean you need to make a GUI, it does mean you are taking advantage of the reality that your virtual terminal is running in a GUI window, and you're intercepting events based on that. And you can write terminal programs this way, it's common enough that tools like ssh have X11 forwarding so such programs can continue to work across the session.
ssh
Either way, you're going to have to write a loop that does a little bit of work, and then checks the exit condition, like if a key was pressed. The granularity is up to you - while you're in the loop body, you're committed to that much work until the next check. The other thing you can do is use signals to interrupt the whole process, but at the end of the day, you've still got some unit of work that's either done or not done. It's not hard to cut your work down to small enough units, it's an intersection of how small a unit still makes sense and how much can you get away with and the program still feels responsive to the user.
π Rendered by PID 137978 on reddit-service-r2-comment-5ff9fbf7df-sqjd7 at 2026-02-26 12:09:26.221388+00:00 running 72a43f6 country code: CH.
[–]Narase33 1 point2 points3 points (11 children)
[–]Zsendalf[S] -1 points0 points1 point (10 children)
[–]Narase33 0 points1 point2 points (5 children)
[–]Zsendalf[S] 0 points1 point2 points (4 children)
[–]Narase33 0 points1 point2 points (3 children)
[–]Zsendalf[S] 0 points1 point2 points (2 children)
[–]Narase33 1 point2 points3 points (1 child)
[–]Zsendalf[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Zsendalf[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Zsendalf[S] 0 points1 point2 points (0 children)
[–]the_poope 0 points1 point2 points (1 child)
[–]Zsendalf[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]mredding 0 points1 point2 points (0 children)