c - How do you read the arrow keys? -


extensive searching on use of raw mode termios , xterm leads numerous references "timing trick" required distinguish between escape-sequence , lone appearance of escape character.

so how do it?

i don't want use curses because don't want clear screen. calculator-style program, it's important retain "ticker-tape" interface.

finally found nice detailed description in old usenet thread. quote relevant message in entirety.

 path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!usenet.ins.cwru.edu!ncoast!allbery from: all...@ncoast.org (brandon s. allbery kb8jrr) newsgroups: comp.unix.programmer subject: re: how read arrow keys? message-id:  date: 1 jan 91 03:56:56 gmt references:    reply-to: all...@ncoast.org (brandon s. allbery kb8jrr) followup-to: comp.unix.programmer organization: north coast computer resources (ncoast) lines: 68  quoted  brn...@kramden.acf.nyu.edu (dan bernstein): +--------------- | it's terminal's fault, not programmer's fault. codes | coming terminal should uniquely decodable untimed byte | streams. in best situation, no code prefix of another. +--------------- 

at&t has nice solution problem; unfortunately, depends on at&t termio (or posix termios), implementing under bsd variant difficult. although 1 conceivably come hack using select, not quite reliable. @ least 1 commercial product know of uses method (termio, not select), documented in @ least 1 programmer's manual i've read well.

termio(s) doesn't have "raw" mode; has "packet" mode. common use packet size of 1 , timeout of 1 (which treated "no timeout"). however, 1 can set other combinations. useful in case set packet size size of longest function key sequence , timeout longest time needed sent as function key. assumption (usually correct) being if user types it, take longer.

once done, attempt read() longest number of characters @ same time. read() returns actual number of characters read before timeout, starts after first character of packet received. thus, single keystrokes esc read such, given vt100, pf1 return 3 characters --- esc o p (esc [ p if, me, detest applications cursor , keypad modes).

struct termio tbuf; /* posix: struct termios */ int maxlen = 3, len; char buf[3];  ioctl(0, tcgeta, &tbuf); /* posix: tcgetattr(0, &tbuf); */ tbuf.c_lflags &= ~(icanon|echo); tbuf.c_cc[vmin] = maxlen; tbuf.c_cc[vtime] = 2; /* 2/10 sec, @ 9600 baud , */ ioctl(0, tcsetaw, &tbuf); /* posix: tcsetattr(0, x???wait, &tbuf); */               /* forget exact flag */  len = read(0, buf, maxlen); if (len == 1) {     /* single character */ } else {     /* function key sequence */ } 

getting vtime correct various baud rates can tricky; it's one-time task. , i've used trick in own programs; works well. believe function key support in svr3 curses can coerced doing if halfdelay() enabled , works in port.

for bsd, can check see if version (e.g. ultrix 3.x or sunos 4.x, etc.) supports termio interface, or wait bsd4.4 supposedly have posix termios. (since bsd4.4 either out or --- i've been out of touch --- no doubt chime in , tell us.) warned earlier ultrix versions claimed have termio support, didn't work.

i've trimmed author's signature block, since acknowledges not original author anyway.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -