c - Determining button boundries with terminal IO in non-canonical mode -
i'm trying understand how terminal i/o works.
when terminal placed in non-canonical mode (missing error handling):
struct termios term_original, term_current; tcgetattr(stdin_fileno, &term_original); term_current = term_original; term_current.c_lflag &= ~(icanon | isig | iexten | echo); term_current.c_iflag &= ~(brkint | icrnl | ignbrk | igncr | inlcr | inpck | istrip | ixon | parmrk); term_current.c_oflag &= ~(opost); term_current.c_cc[vmin] = 1; term_current.c_cc[vtime] = 0; tcsetattr(stdin_fileno, tcsadrain, &term_current);
a simple read loop can read in data generated each button press so:
char c; while (read(0, &c, 1) != -1) { print_char(c); }
now,
- pressing esc on keyboard generates: 0x1b.
- pressing f1 generates: 0x1b 0x4f 0x50.
- pressing f5 generates: 0x1b 0x5b 0x31 0x35 0x7e.
in terms of reading , processing input, how 1 determine output 1 button press ends , next 1 begins? find no discernible pattern, , fact esc generates single byte identical first byte of output multi-byte generating button presses seems suggest there none. there other mechanism determining button boundaries are?
programs rely on keys not being pressed fast. if delay less 100ms, 1 key press; otherwise there 2 separate events.
yes program pause time after esc being pressed, in order make sure it's esc , no other key. pause can discerned naked eye.
some programs recognize escdelay environment variable fine-tuning timing.
and yes not perfect, can fool system pressing keys fast.
Comments
Post a Comment