IO#getc behavior

Discussion in 'Ruby' started by Jon A. Lambert, Sep 26, 2005.

  1. I'm writing a console application with an input loop like the following:

    while running
    keyhit = select( [$stdin], nil, nil, 0.1 )
    if keyhit
    c = $stdin.getc
    session.message(c.chr)
    end
    end

    In the cygwin version of Ruby all is fine. The user hits a key select()
    indicates it and the character is gotten with $stdin.getc.

    However I ran this under the msvc version of windows and what happens is
    select() returns a ready state for $stdin whether a key is hit or not.
    Then getc() blocks? because there's nothing yet to be gotten. Worse no I/O
    seems to be going on until the user hits the return key and then whammo I
    see either the end result of the loops or it appears more likely getc blocks
    internally under cover until it sees a CR in the stream.

    Is there a workaround?

    Thanks

    --
    J Lambert
    Jon A. Lambert, Sep 26, 2005
    #1
    1. Advertising

  2. Hi Jon,

    this is what I use:

    --- CODE ---
    ##
    # getch and kbhit for win32
    # Sean O'Halpin
    #
    require 'dl/import'
    module Win32API
    extend DL::Importer

    dlload 'crtdll.dll'

    ##
    # kbhit - returns 0 if nothing waiting, non-zero otherwise
    #
    extern 'int _kbhit()', :stdcall

    ##
    # getch - blocks waiting for one character of input from console
    # Note: returns integer representing character
    # Use getch.chr to get one character string
    #
    extern 'int _getch()', :stdcall

    alias :kbhit :_kbhit
    alias :getch :_getch

    module_function :kbhit, :getch
    end

    if __FILE__ =3D=3D $0

    puts "kbhit"
    puts "Hit any key"

    while true
    x =3D Win32API.kbhit
    if x > 0
    puts Win32API.getch.chr
    break
    else
    print "."
    end
    end
    end
    --- END CODE ---

    HTH,

    Sean
    Sean O'Halpin, Sep 26, 2005
    #2
    1. Advertising

  3. Jon A. Lambert

    Tanaka Akira Guest

    In article <000701c5c27a$d77058b0$0200000a@agamemnon>,
    "Jon A. Lambert" <> writes:

    > However I ran this under the msvc version of windows and what happens is
    > select() returns a ready state for $stdin whether a key is hit or not.
    > Then getc() blocks? because there's nothing yet to be gotten. Worse no I/O
    > seems to be going on until the user hits the return key and then whammo I
    > see either the end result of the loops or it appears more likely getc blocks
    > internally under cover until it sees a CR in the stream.
    >
    > Is there a workaround?


    I heard Ruby 1.9 is improved recently. See [ruby-talk:156899].
    --
    Tanaka Akira
    Tanaka Akira, Sep 27, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irrwahn Grausewitz

    Re: getc hanging

    Irrwahn Grausewitz, Aug 31, 2003, in forum: C Programming
    Replies:
    1
    Views:
    347
    Irrwahn Grausewitz
    Aug 31, 2003
  2. spike
    Replies:
    9
    Views:
    306
    Martien Verbruggen
    Feb 27, 2004
  3. PL

    detect Ctrl+C with getc ?

    PL, May 25, 2004, in forum: C Programming
    Replies:
    2
    Views:
    870
  4. William L. Bahn

    getc() vs. fgetc()

    William L. Bahn, Jul 14, 2004, in forum: C Programming
    Replies:
    13
    Views:
    1,782
    Dave Thompson
    Jul 21, 2004
  5. Bill Cunningham

    getc and ungetc

    Bill Cunningham, Nov 18, 2004, in forum: C Programming
    Replies:
    8
    Views:
    393
    Malcolm
    Nov 22, 2004
Loading...

Share This Page