I'm experminenting with ncurses and I've stumbled upon a bit counter-intuitive behaviour when usinghalfdelay
with small delays.
The following expects keyboard input from the user. Rather than just pressing and releasing a key, I'm testing it with the key being pressed so that it should continue printing the corresponding character (more precisely, the corresponding int
value). Here's a stripped version of what I have:
#include <ncurses.h>
int main() {
int c = 0, d = 0, e = 0;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
while ('q' != (c = getch())) {
printw("c: %d\n", c);
// Make sure that halfdelay returned OK. It should as the input is in the expected range ([1, 255])
if ((d = halfdelay(1)) != OK) {
printw("d: %d\n", d);
return 0;
}
e = getch();
printw("e: %d\n", e);
cbreak();
}
endwin();
return 0;
}
Here's how I build it and run it:
$ gcc -Wall -Wpedantic -lncurses file.c
$ a.out
And the output when running it (after pressing and holding a
on the keyboard for ~1s):
c: 97
e: -1
c: 97
e: 97
c: 97
e: 97
c: 97
e: 97
c: 97
e: 97
c: 97
e: 97
c: 97
e: -1
Question
Where is -1/EOF
coming from in this output? I did notice that by increasing the delay (from 1 to 7 on my machine) makes the EOF
character to go away completely. I would love to understand what's the mechanism causig this.