0

I've been using Linux as my main system for a while, and trying to detect keys in the terminal using C/C++ I had a problem. The header conio.h does not exist on Linux, so looking around, I found the commands system("stty raw"); and system("stty cooked"); with getchar, but in addition to blocking commands like Control+C, what I typed in the terminal appears, and paralyzes the code. Looking deeper, I found termux, and I managed to create a getch, in which I could detect a key without typing enter, not appear in the terminal, and not block commands like control+c, but it still crashes the code. Even trying to implement kbhit, it goes back to drawing the keys in the terminal. Looking even deeper, I found a way to use X11 to detect the keys in the terminal, but for some reason, it doesn't work. I'm afraid it's because of the non-lts pop. I wanted a way in which you don't draw what you type in the terminal, don't block commands from the terminal, and don't crash the code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    You'll probably want to use [ncurses](https://invisible-island.net/ncurses/). – Eljay Feb 22 '22 at 18:16
  • If using ncurses, check [this answer](https://stackoverflow.com/a/4028974/5264491). Note that you cannot detect modifier keys such as SHIFT being pressed this way. You can only detect input characters being generated or special key presses. – Ian Abbott Feb 22 '22 at 18:36
  • Note that it is generally a bad idea to tag a question with both C and C++ unless the question is about one of the many differences between the two languages. It's more likely to earn downvotes than upvotes. – Jonathan Leffler Feb 22 '22 at 18:37
  • There are many existing questions about simulating `kbhit()` etc on Linux machines. I've listed two in the duplicate box; there are many others. A search using "`[c] kbhit`" shows over 400 entries. The suggestion [Create a function to check for a key press in Unix using `ncurses`](https://stackoverflow.com/questions/4025891/create-a-function-to-check-for-key-press-in-unix-using-ncurses) is another option. – Jonathan Leffler Feb 22 '22 at 18:53
  • thanks to those who indicated ncurses, with a quick look I already did everything I wanted, I had to adapt the code, and forget about some functions like the gotoxy that I created, but it totally works – José Antônio Feb 22 '22 at 19:12

0 Answers0