emacsterminaltermios

How do programs like Emacs and Nano get user input while running in terminal without the user having to press enter?


Programs like Emacs and Nano can run in a terminal window and accept real time user input, how might I accomplish this myself? I am looking to use C++ but I can see this could require interfacing with the user's shell, in this case Bash. How can I take input (be it from the mouse or keyboard) from the terminal window without the user having to press enter?

My target platform is Mac OSX Version 10.13.5, but answers on how to do this on other platforms like Windows or Linux are also welcome.


Solution

  • These programs set the terminal to raw mode as opposed to cooked mode. Cooked mode is read a line at a time and is handled by the terminal driver, and raw mode is read a character at a time and is handled by the running program instead.

    This Answer has more information.

    This Repo is a simple project that implements this mode that you can learn from. man termios will also give you information about manipulating terminal modes.