Close a program if a key is being pressed when opening it in osx - c++

I'm trying to make a program that closes itself -or only runs the line of code that closes it- whenever a certain key is in the pressed state when the program is run, so, if I start the program and the 'c' key is pressed, the program must stop.
Note that cin or read doesn't help here, because it waits until you press enter to detect a keystroke. In a way, I'm looking for a way to detect the async key state(because the cursor may not be focused in the program's console window) in osx using c++.

Related

How to exit loop as soon as the user clicks ESC or 'q' key?

Hell'o.
In my program, there is a method that refreshes console screen four times per second. My problem is to stop executing main loop as soon as the user clicks ESC or 'q' key, without clicking ENTER after that. The good reference (without sharing my code) is game loop and event handling. I code on Linux.

Any Way of Waiting for Console Unfocus or Pause Until Not Focused?

I have a simple problem that I am struggling to find an answer to. In my situation, I am attempting to wait for user input into the console to activate the main loop of the program, but the program can not be allowed to enter the main loop until the window behind it is in focus. If you do not understand what I mean, here is an example. I have two windows, the one in focus(on top) is the console running the program which is at a pause state while waiting for the window behind it to become the window on top and in focus. I think that either enabling the console to read keyboard input while not in focus or having the program recognize that it is no longer in focus would work. Is it possible to pull off either of those situations in Windows?
Thank you in advance.
I think "focus" doesn't mean what you think it means. "Pause state" on the other hand has no special meaning. If your program has a "pause state", then you defined that state, and you define when the state is entered. So just enter the pause state when you feel like it.
That said, the strict answer to your question is the WM_KILLFOCUS message, but that arrives in the message loop for a window and you don't own the message loop of a console window.

How to dump game data before closing C++ Windows

I am working on a project in which I put my data (I.E. Game world, mobs) into text files, which are read in when I run the game. This works perfectly fine. The game saves the data when I hit a key that exits the game loop, basically saving and then closing the game. However, I habitually hit the X on the top right of the console, and this obviously causes the game to close without saving. My question is, is there any way to run a function when somebody hits the close button on the console, and then close the program? I am working on Windows XP, C++, Console Program.
Closing a c++ console app with the "x" in the top corner throws an
CTRL_CLOSE_EVENT which you could catch and process if you set a
control handler using the SetConsoleCtrlHandler function. In there
you could override the close functionality and perform whatever you
wished to do, and then optionally still perform the default behavior.
What happens when you close a c++ console application

Console Keyboard press detection lib or standard options?

i'm making a console game but i don't want the system to wait every time i tell it to detect a press (like cin)... Does anyone know how i can get a console window to detect keyboard presses, its ok if it's just letter key detection too, i can make do!
If it's Windows, the kbhit( ) function is the guy you want. If it's *nix, here's a kbhit( ) emulator. kbhit( ) returns immediately (no blocking) with a flag that says that there is or is not a keyboard character waiting to be read. You can test the flag to see if you should issue a keyboard read.

CPP:How to pause resume a running script in cmd line console using mouse?

I am in desperate need of help.... :). I am running a script from cmd line console. It does a sequence of operations. whats the best way to pause the script in between to check the results and resume it back using mouse????? or any key
I would appreciate for your reply back,
-Abishek
If I understand your question correctly, and if your program is writing to the standard output (the console window) and you are running on Windows:
You can turn on 'quick edit' in the console window by editing the cmd.exe window properties.
Open a command prompt (start | run | cmd.exe)
Click the upper left corner of the window and choose 'Defaults'.
Click the Options tab and then in Edit Options section make sure the QuickEdit Mode box is checked.
Click OK and you should be set.
If you click with the mouse inside the console window when quick edit is enabled, it will block the program from continuing while it is waiting to be able to write the next message to the output. If you then hit 'esc', the program will continue.
It's a useful shortcut for pausing a running console program that I use all of the time.
try cin.get()
There isn't any easy way to do that. You'd need to create a wrapper to run the program (for example, a debugger is a wrapper to a program), and this wrapper would have to have mouse functions programmed into it.
Without use of the mouse, use cin.get();. It will take an input from the keyboard before continuing.
I'm not sure about using the mouse to pause, but you can pause the execution of command line process by pressing the Pause / Break key or Ctrl + NumLock .
You can then resume the execution with Ctrl + Z