Noel Arteche
Noel Arteche

Reputation: 201

How to detect when a key is press using Linux system calls

I am trying to write a C program that uses Linux system calls to emulate the behaviour of the more command in the Linux terminal. Since the users might enter commands such as q to finish the execution, I am trying to find a way in which to read keystrokes from the standard inout without using read(...), as I don't want the pressed key to appear on the standard output.

In other words, I want to be able to detect pressed keys without them being written.

I have read that ioctl() and the termios struct can somehow be used for this purpose, but I am not sure how they are used (I find the man pages somewhat cryptic).

I have found a few answers to the use of those functions, but all of them seem too complicated. There must be an easier way to detect simple keystrokes, isn't there?

Upvotes: 0

Views: 361

Answers (1)

domen
domen

Reputation: 1908

man 3 termios, tcsetattr, disable ECHO on stdin.

For a longer explanation see: Hide password input on terminal

Alternatively, you could go through below termios abstraction, use input layer, /dev/input/*, but I think you'll need to disable forwarding events from your input devices to upper layers then.

Upvotes: 2

Related Questions