kbhit() with nowait option misses characters.

Jaroslav Hajek highegg at gmail.com
Wed Jul 1 00:17:30 CDT 2009


On Tue, Jun 30, 2009 at 10:31 PM, Joe
Rothweiler<octaveuser at sensicomm.com> wrote:
> Bug report for Octave 3.2.0 configured for i686-pc-linux-gnu
>
> Description:
> -----------
>  I'm trying to use the kbhit builtin function under Linux in
>  nowait mode: c1=kbhit(1); It is
>  supposed to return a character if a key has been hit,
>  else return an empty string. In fact, it often
>  returns an empty string even if a key has been hit.
>
>  Here's what I think is happening:
>  in src/sysdep.cc, if macro HAVE__KBHIT is not defined,
>  then subroutine octave_kbhit () calls
>  raw_mode(...) at line 515. In turn, raw_mode
>  calls tcsetattr (tty_fd, TCSAFLUSH, &s) at line 395.
>
>  Per the tcsetattr man page:
>  "TCSAFLUSH ... all  input  that  has  been received but
>  not read will be discarded"
>
>  So, input keystrokes will be lost unless they occur
>  after the call to tcsetattr but before the read at
>  line 531.
>
> Repeat-By:
> ---------
>
>  ------ Test file keytest.m:
>  while 1
>    c1=kbhit(1);
>    if (~isempty(c1))
>      fprintf(2,'got %s\n',c1);
>      break;
>    end
>  end
>  ------
>  Execute using: octave -q -i keytest.m
>  then hit any keyboard key, say 'q'. Output should be:
>  qgot q
>  (where the first q is the input being echoed).
>  Actual output is
>  qqqqqgot q
>  (ie, the key must be struck several times before
>  it is detected).
>
>
> Fix:
> ---
> In src/sysdep.cc, subroutine raw_mode, line 395:
> TCSAFLUSH should be TCSADRAIN.
> (This makes kbhit() work correctly; I don't know
> if raw_mode is called anywhere else.)

OK. But I think we should still discard prior input if kbhit is called
without arguments. So I've put in
wait ? TCSAFLUSH : TCSADRAIN
instead.

thanks

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



More information about the Bug-octave mailing list