1
votes

i'm using ncurses in C prog and i would like to use getch() to catch arrow key and escape key.

I use newterm, raw mode, noecho mode and keypad set to TRUE to use correctly getch. I can easly get the key code of arrow or either every key but when I press escape, I know getch set a timer of ~1 sec to check if none of other keys is pressed.

Do you think the delay could be delete or set at zero ?

Man or IBM doc said it's not useful to try to catch escape with getch when you use at the same time keypad but i would like to get it...

Sorry for my bad english and thanks you for yours answers :)

1

1 Answers

5
votes

You have to choose between seeing arrow and function keys, or getting an escape key immediately when it is typed. Even if you really really really want to see escapes immediately and also see arrow keys, you cannot. That's because the first thing the arrow key sends is an escape character, and so if wgetch sees an escape character, it needs to check the next character before concluding that the character was generated by the escape key.

The curses interface was designed for the possibility that data from the terminal is transmitted to the computer over a possibly slow network connection. It would be possible for the escape character which is part of a function key sequence to be sent at the end of a network packet, with the next character being part of the next packet, and there could, therefore, be a delay between the two characters. Even if there were no network, though, a keyboard is not an ultra-high-speed device, and the characters in a multi-character sequence are sent with a noticeable (to the computer) delay. So without some time, it would be impossible for wgetch to recognized function and arrow keys.

If you are prepared to run the risk of sometimes turning arrow keys into an escape sequence, you can reduce the escape delay. By default it is set to 1000 milliseconds (1 second), but you can change it by changing the value of the global variable ESCDELAY. If your ncurses library was compiled with appropriate threading features enabled, you can also use the set_escdelay function, which is specific to ncurses. In both cases, the value is an integer, representing the number of milliseconds to delay.

See man curs_variables and man curs_threads for more details. (On some distributions, you may need to use different man arguments, such as man curses_variables and man 3ncurses threads.)