C++ detecting keystrokes [duplicate] - c++

This question already has answers here:
Capturing a keystroke in C++
(4 answers)
Closed 9 years ago.
I am trying to use keystrokes to affect my program. For example, I have a program that prints numbers continuously. I want it to stop printing numbers if I enter Ctrl+E. How can I do this on C++?
I have read about a number of headers like conio.h but they are not built in the C library. I would like this to be kept as pure C/C++ as possible.

you can use GetAsyncKeyState function for this.
It is there in Winuser.h file

That depends on the operating system - hence impossible in 'pure' C/C++.
I fear you have to go with #ifdef SYSTEM_A, ...
Please notice, The languages C/C++ are not aware of any kind of hardware (keyboard in this case), they know streams.

Related

Programming and platform dependance for c++ questions [duplicate]

This question already has answers here:
How to write portable code in c++?
(12 answers)
What is "Portable C++"? [duplicate]
(2 answers)
What is meant when a piece of code is said to be portable? [closed]
(3 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Not sure if this is the right place to ask this:
How do programs written in c++ run on other computers if you don't write them specifically to do that? I saw something about not just sending the .exe, but also sending other things with it?
Is there a high level programming language that is as fast or nearly as fast (in run speed) as c++ while also being platform independent?
See above.
You compile your code for all the platforms you target and deploy a number of executables. Hence, Write once, compile anywhere.
C++ allows you to write portable source code. So assuming you write portable code to start with, you can compile it for some target platform, and run the resulting binary on that target.
Now, depending on what your program uses, you may have to package other "stuff" with the executable. What you mention ("I saw something about not just sending the .exe, but also sending other things with it?") would arise if your program used some dynamic link libraries that were not part of the OS (presumably Windows, based on the mention of .exe). But, it's kind of up to you to decide whether to use a library that's packaged as a DLL or not. If you don't want to package DLLs with your executable, don't use them (but sometimes, you may decide it's less trouble to use and package the DLL than do without).
As far as another language goes...doesn't really make a lot of difference as a rule. If you write code that depends on something else, you have to satisfy that dependency on the target computer. Some languages require you to add some DLLs to support the language itself, but most C++ compilers don't. On other OSes, those dependencies won't be called "DLLs", but most reasonably modern OSes provide something similar.

Is there a way to wait for and read the next keyboard input? [duplicate]

This question already has answers here:
Capture characters from standard input without waiting for enter to be pressed
(21 answers)
Closed 2 years ago.
First time asking here.
I've been looking around for quite a while and couldn't find an answer anywhere.
I've had to build a calculator in C++ (boring stuff so tried to make it interesting) but couldn't find a way to wait for the next keyboard input and read it/store it. Something like keyboard events.
Cin requires the user to press enter every time and I don't want that. same for getchar, getch, cin.get(), etc...
If possible I'd like to avoid the inclusion other library than the basic ones.
Anyone knows how to ?
Thanks.
The standard C++ libraries do not provide single-character input functions. You will need to use something like the curses library, which provides functions to do this.

How to take unbuffered input in c++ (i.e without hitting enter key)? [duplicate]

This question already has answers here:
synchronous keystroke reading from console application
(3 answers)
Closed 3 years ago.
I'm trying to take comma delimited input from the user so that without pressing enter key,the input is accepted.
I think that's not possible in a portable manner in pure C++, because it depends too much on the terminal used that may be connected with stdin (they are usually line buffered). You can, try to use getchar() or getline() and try to tweak your code based on your requirement.

Is it possible to decompile a C++ executable file [duplicate]

This question already has answers here:
Is it possible to "decompile" a Windows .exe? Or at least view the Assembly?
(16 answers)
Is there a C++ decompiler? [closed]
(5 answers)
Closed 4 years ago.
I lost the source code to an executable file but still have the actual file. Is there any way to retrieve the original C++ code?
Duplicate of this question here.
Yes, it is possible, however when it comes to peeking function bodies and the like, you might have a little less luck. Operating systems like Kali Linux specialize in de-compilation and reverse engineering, so maybe look into a VM of that. And of course, windows has a lot of applications you can use as well to check the application code.
Look over the other question for specific app suggestions. :)
Edit : You will most likely have lost all your logic and function bodies, but you might be able to recover the overall structure. It's your EXE so you might be more familiar with how it was all connected up.
You cannot get the original source code but you can decompile the binary into source code using tools given in this similar question: Is there a C++ decompiler?
The output source code will not look like the original as the compiler will have optimised the original source when generating the executable.
Short answer NO.
Long answer, because C++ doesn't use some intermediate code like C# or Java you cannot decompile the app in some readable format. But if you can read assembly maybe you can save some time.

C++ - Create function at runtime [duplicate]

This question already has answers here:
Is it possible to create a function dynamically, during runtime in C++?
(14 answers)
Closed 8 years ago.
In C++, I have to make some user-defined actions on each cells of a big table.
Because of the size of the table, I'd like not to use interpreted instructions but to compile during runtime a function that I will call on each cell.
The user-defined actions are pretty simple :
if ((state1 && state2) || state3) then change_a_value_in_memory
That's why I don't need to use the LLVM or other JIT libraries.
I hesitate to just use mmap and add the code in hex directly.
I'd like to know if it exists better solutions, or, if not, where I can find the basic format of a C++ function code to directly write it in memory.
Thanks, and sorry for my english :/
This is not the most elegant, but it works always: generate a file with the C++ code of your function, call the compiler using system(), load the generated .so file using dlopen, and use the function!
This will take some time (due to the compiler call), but if you keep the function and maintain a database of the functions you already have, then you can save the amount of compilations.