Poll STDIN for waiting input on Win32

Discussion in 'Ruby' started by Bill Atkins, Aug 9, 2005.

  1. Bill Atkins

    Bill Atkins Guest

    How can I determine if there's input waiting on STDIN before calling
    gets? This code doesn't do the trick:

    ----

    while true
    r, w, e =3D select [$stdin], [], [], 1
    =20
    if r and r.include? $stdin
    puts "READY TO READ!"
    end
    end

    -----

    Apparently, r always includes $stdin, so it just continuously prints
    "READY TO READ".

    --=20
    Bill Atkins
     
    Bill Atkins, Aug 9, 2005
    #1
    1. Advertising

  2. Bill Atkins

    Bill Kelly Guest

    Hi,

    From: "Bill Atkins" <>
    >
    > How can I determine if there's input waiting on STDIN before
    > calling gets?


    <laugh> I just posted this a minute ago on the
    Threading+TCPServer thread; if I'd seen your message
    I'd have posted here instead... :)

    I use:

    if WINDOWS_VERSION
    require 'dl'
    $win32_console_kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
    def console_input_ready?
    $win32_console_kbhit.call != 0
    end
    else
    def console_input_ready?
    select([$stdin], nil, nil, 0) != nil
    end
    end

    Note that if you do a gets() when console_input_ready?
    is true, you'll still block until the user hits <enter>.

    BTW, I was using:

    WINDOWS_VERSION = (RUBY_PLATFORM =~ /win32/) != nil


    Regards,

    Bill
     
    Bill Kelly, Aug 9, 2005
    #2
    1. Advertising

  3. ---- Original message from Bill Kelly on 8/9/2005 10:25 AM:

    >From: "Bill Atkins" <>
    >
    >
    >>How can I determine if there's input waiting on STDIN before
    >>calling gets?
    >>
    >>

    >I use:
    >
    >if WINDOWS_VERSION
    > require 'dl'
    > $win32_console_kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
    > def console_input_ready?
    > $win32_console_kbhit.call != 0
    > end
    >else
    > def console_input_ready?
    > select([$stdin], nil, nil, 0) != nil
    > end
    >end
    >
    >Note that if you do a gets() when console_input_ready?
    >is true, you'll still block until the user hits <enter>.
    >
    >BTW, I was using:
    >
    >WINDOWS_VERSION = (RUBY_PLATFORM =~ /win32/) != nil
    >
    >
    >

    You can also define '_getch' to allow you to get the character right
    away and not have to wait for the user to press the <enter> key.

    require 'Win32API'

    $win32_console_kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
    $win32_console_cputs = Win32API.new("msvcrt", "_cputs", ['P'], 'I')
    $win32_console_getch = Win32API.new("msvcrt", "_getch", [], 'I')

    def console_input_ready?
    $win32_console_kbhit.call != 0
    end

    def console_input
    $win32_console_getch.call
    end

    def console_output( str )
    $win32_console_cputs.call( str )
    end

    while true
    if console_input_ready? then
    ch = console_input
    print "ch: <#{ch.chr}>\n"
    break
    else
    console_output( "." )
    sleep 0.5
    end
    end
     
    Dale Martenson, Aug 9, 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. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,354
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    815
    Dan Pop
    Jun 21, 2004
  3. birdsong

    select.poll.poll() never blocks

    birdsong, Feb 12, 2009, in forum: Python
    Replies:
    2
    Views:
    466
    birdsong
    Feb 12, 2009
  4. Jean-Paul Calderone

    Re: select.poll.poll() never blocks

    Jean-Paul Calderone, Feb 12, 2009, in forum: Python
    Replies:
    3
    Views:
    455
    birdsong
    Feb 12, 2009
  5. Mountain Drew

    win32 serial port listener: poll or interrupt

    Mountain Drew, Jun 22, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    250
    Ron Savage
    Jun 23, 2005
Loading...

Share This Page