user3167666
user3167666

Reputation: 144

Linux c++ console get key state

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.

Upvotes: 3

Views: 6677

Answers (2)

Jan Kytka
Jan Kytka

Reputation: 323

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.

Upvotes: 1

Robert Hickman
Robert Hickman

Reputation: 1147

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++

Upvotes: 1

Related Questions