escaping - How to differentiate between Escape and Up/Down/Left/Right with termios? -
escaping - How to differentiate between Escape and Up/Down/Left/Right with termios? -
github
this best can come handle ncurses-style key presses (i'm writing alternative ncurses various reasons).
an illustration app built code advises user "quit pressing escape". in truth, requires escape + escape or escape + arrow key. i'd prepare this.
#include <sys/ioctl.h> #include <termios.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char *get_key() { char c = getchar(); switch(c) { case 'a': homecoming "a"; case 'b': homecoming "b"; case 'c': homecoming "c"; ... case '\x1b': c = getchar(); switch(c) { case '[': c = getchar(); switch(c) { case 'a': homecoming "up"; case 'b': homecoming "down"; case 'c': homecoming "right"; case 'd': homecoming "left"; } case '\x1b': homecoming "escape"; } default: homecoming "unknown"; }
step 1. research.
http://en.wikipedia.org/wiki/escape_sequence
if esc key , other keys send escape sequences both supposed meaningful application, ambiguity arises, if terminal or terminal emulator in use. in particular, when application receives ascii escape character, not clear whether character result of user pressing esc key or whether initial character of escape sequence (e.g., resulting arrow key press). traditional method of resolving ambiguity observe whether or not character follows escape character. if not, assumed not part of escape sequence. heuristic can fail under circumstances, in practice works reasonably well, faster modern communication speeds.
step 2. figure out how see if there's character waiting in input buffer on os. if there's character in buffer; escape sequence. if input buffer empty, escape.
since didn't mention os, it's not clear how can done.
windows: what windows equivalent capabilities defined in sys/select.h , termios.h
linux: http://linux.die.net/man/3/termios
escaping ncurses arrow-keys termios
Comments
Post a Comment