This project implements the Windows-specific functions _getch()
and _kbhit()
from conio.h
. These functions are used to read characters from stdin, without the user having to press enter, with the option to do it in a non-blocking way, which can be used to detect keypresses without having to "pause" the application while waiting for input.
Despite being an implementation of a Windows API for Linux, this project also works for Windows. The only function which is "implemented" for Windows (ie. adds onto the already existing functionality) is:
int getch_noblock();
which is a non-blocking version of _getch()
, which returns EOF if no character can be read from stdin.
- Add the
linux_conio.h
andlinux_conio.cpp
files to your project - Include the
linux_conio.h
header file in your project - Call
enable_conio_mode()
every time before the library is used (if conio mode is currently disabled) - Use
_getch()
,_kbhit()
and/orgetch_noblock()
as you would normally - Call
disable_conio_mode()
when your program is closing to restore the console settings.
This project compiles on any modern C++ compiler for either Linux or Windows. In this example I will use gcc
To compile the project and run the test cases, run:
g++ test.cpp linux_conio.cpp -o conio.out
./conio.out
This is not a full implementation of conio.h
, it only implements _getch()
and _kbhit()
, which are the most important functions from conio.h
, and also the hardest to implement on Linux.
To achieve this functionality on Linux, the application changes the terminal settings, which has some caveats:
enable_conio_mode()
needs to be called before any functionality is useddisable_conio_mode()
needs to be called if you want to take normal input with std::cindisable_conio_mode
needs to be called when the program exits, else the console will glitch.- When a terminating signal which can not be handled (eg. SIGKILL or SIGSTOP) is sent, conio mode does not get automatically disabled and will glitch the console. (SIGINT, SIGTERM, SIGQUIT and SIGTSTP is properly handled and cleaned up though)
- Implement more functions from
conio.h
(Not very important)