I want to create command line game in Linux but I don't know to get the key state. I heard of getch() but that stops the program.
Asked
Active
Viewed 5,991 times
2 Answers
1
I spent a little while reading around. Apparently, this is a hard thing to do without the help of a library. Many people recommended the library ncurses. If you want to try to do it by yourself, you need to learn about switching terminal modes and crazy stuff like that. This thread was very informative: Non-blocking keyboard read - C/C++

Community
- 1
- 1

Robert Hickman
- 997
- 1
- 11
- 22
1
In this article, the author implements a cKeyboard
class, which directly listens for events from /dev/event0
. The class is then used as follows:
#include "keyboard.h"
cKeyboard kb;
...
if (kb.getKeyState(KEY_UP)) {
// do something
}
It works perfectly for me, but I had to change event0
to event4
.

Jan Kytka
- 303
- 2
- 7