0

I am learning how to use terminal escape codes on Windows. I know how to move cursor position and how to do a few other things. But how do I receive input?

For instance, if I want to navigate around with the arrow keys, how would I do that?

  • There is no standardisation of cursor and function key handling across platforms. I'm just looking for a duplicate question... such as this [answer](https://stackoverflow.com/a/34452407/4142924). – Weather Vane Feb 26 '23 at 11:00
  • @WeatherVane Microsoft docs: "By contrast, virtual terminal sequences (in a variety of dialects) drive the command-line environment operations for all other platforms. These sequences are rooted in an ECMA Standard" – user21290559 Feb 26 '23 at 11:01
  • 1
    For Windows if you want to use otherwise plain C, I recommend the Windows-specific [`_getch`](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/078sfkak(v=vs.110)?redirectedfrom=MSDN) or [`_getche`](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/kswce429(v=vs.110)) functions. Read the documentation carefully, it explains how to read the cursor and function keys. – Some programmer dude Feb 26 '23 at 11:01
  • @Someprogrammerdude What if I was on Linux or MacOS, how would I receive input? Or is terminal input platform specific? – user21290559 Feb 26 '23 at 11:03
  • @WeatherVane I saw that answer earlier, but that relies on the windows specific function _getch or _getche. Is it like this with all platforms? Does that mean that I have to utilize different functions for input for different platforms? – user21290559 Feb 26 '23 at 11:05
  • It is Windows specific, sorry I have never used virtual terminal input sequences, but your phrase links to [Classic Console APIs versus Virtual Terminal Sequences](https://learn.microsoft.com/en-us/windows/console/classic-vs-vt) which you might like to explore. – Weather Vane Feb 26 '23 at 11:07
  • @WeatherVane I already looked at that, although I think they have a page with all of the virtual terminal sequences, perhaps I'll find something there. Thanks for the effort. – user21290559 Feb 26 '23 at 11:10
  • Linux uses `ncurses`, and there are versions available for windows. MS does not like standards (and even tries to break them with its false 'deprecated' warnings). – Weather Vane Feb 26 '23 at 11:13
  • 1
    For POSIX systems (like Linux and macOS) I suggest [ncurses](https://en.wikipedia.org/wiki/Ncurses) instead. There are builds of ncurses available for Windows as well, but I don't know how good or compatible they are. Unfortunately low-level input, even from keyboards, are rather platform-specific. And what you should do, what libraries or functions to use, that really depends on your use-case and what your program is actually supposed to do. – Some programmer dude Feb 26 '23 at 11:14

1 Answers1

0

Keyboard input is usually platform specific. ncurses can be used on Linux and macOS while there exists an alternative library for Windows called pdcurses. Another option is <conio.h> which is offered by most C compilers that target Windows including mingw.

<conio.h> lets you use _getch() and _getwch() which are functions that get a character from the console. A link to them: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-170

Here is a SO question that uses _getch() to detect when the arrow keys are pressed: how to detect the ESC key in C?

Edit, this might help as well: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences