8

What is the best way to detect in a C program that arrow key is pressed under Linux or Solaris?

As I know there is no standard C function that can do it. I don't want to use int86 function. I need to do it in a portable way.

Edit: I am asking about console applications.

Bahaa Zaid
  • 1,449
  • 2
  • 16
  • 22

3 Answers3

6

You should look at the curses/ncurses library which will give you advanced screen and keyboard handling for console applications.

There is a lot of documentation available for curses, which is an extensive library.

There is a write-up on this very question available here

Steve Weet
  • 28,126
  • 11
  • 70
  • 86
  • This answer is spot-on. Especially if you want it portable, don't reinvent the wheel, just use a library and spare the headache. – ypnos Feb 20 '09 at 02:57
  • As pointed out by Francis the original Documentation link is now a 404. Repointed to a new source – Steve Weet Apr 19 '12 at 10:01
  • As of 2023, [here](https://stackoverflow.com/a/75499310/6013016) is the solution – Scott Feb 20 '23 at 00:09
2

I believe your program should switch into non-canonical mode to be able to read special characters

dmityugov
  • 4,390
  • 23
  • 18
0

If you wanted to do this without using something like SDL, you should look into the select() statement and how to use it to read from console input.

Suroot
  • 4,315
  • 1
  • 22
  • 28