Listening for Keypress while Working

Discussion in 'Ruby' started by James Edward Gray II, Sep 9, 2004.

  1. I have this long running process which shows output as it goes. I need
    to listen for a key press, but keep writing out my results. What's the
    best way to go about this, select() over STDIN in my processing loop?
    Any tips appreciated. Thanks.

    James Edward Gray II
     
    James Edward Gray II, Sep 9, 2004
    #1
    1. Advertising

  2. On Sep 9, 2004, at 2:23 PM, James Edward Gray II wrote:

    > What's the best way to go about this, select() over STDIN in my
    > processing loop?


    Hmm, I don't appear to be able to select() over STDIN. Now I'm really
    in need of a good idea... <laughs>

    James Edward Gray II
     
    James Edward Gray II, Sep 9, 2004
    #2
    1. Advertising

  3. How about this:

    --
    require 'io/wait'

    loop do
    # my processing stuff
    if STDIN.ready?
    # handle input
    end
    end

    --
    Lennon
    rcoder.net
     
    Lennon Day-Reynolds, Sep 9, 2004
    #3
  4. James Edward Gray II

    Bill Kelly Guest

    Hi,

    From: "James Edward Gray II" <>
    > On Sep 9, 2004, at 2:23 PM, James Edward Gray II wrote:
    >
    > > What's the best way to go about this, select() over STDIN in my
    > > processing loop?

    >
    > Hmm, I don't appear to be able to select() over STDIN. Now I'm really
    > in need of a good idea... <laughs>


    Which operating system?

    On win32... I haven't tried it... they seem to have
    provided _kbhit() in <conio.h>... Apparently implemented
    in terms of win32 PeekConsoleInput()...

    I dunno if it really works...? :) But you might be able
    to try calling it using ruby/dl...

    On *nix, I think if you put the tty into raw mode you should
    be able to select on stdin and detect unbuffered keypresses.


    Sorry I can't be of more help . .

    Regards,

    Bill
     
    Bill Kelly, Sep 9, 2004
    #4
  5. James Edward Gray II

    Bill Kelly Guest

    From: "Bill Kelly" <>
    > From: "James Edward Gray II" <>
    > > On Sep 9, 2004, at 2:23 PM, James Edward Gray II wrote:
    > >
    > > > What's the best way to go about this, select() over STDIN in my
    > > > processing loop?

    > >
    > > Hmm, I don't appear to be able to select() over STDIN. Now I'm really
    > > in need of a good idea... <laughs>

    >
    > On win32... I haven't tried it... they seem to have
    > provided _kbhit() in <conio.h>... Apparently implemented
    > in terms of win32 PeekConsoleInput()...


    Wow it actually seems to work (!)

    >> require 'dl'

    => true
    >> kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')

    => #<Win32API:0x2b84670>
    >> kbhit.call

    => 0
    >> 10.times { puts kbhit.call; sleep 1 }

    0
    0
    1 # I hit spacebar here....
    1
    1
    1
    1
    1
    1
    1


    HTH,

    Regards,

    Bill
     
    Bill Kelly, Sep 9, 2004
    #5
  6. On Sep 9, 2004, at 3:42 PM, Bill Kelly wrote:

    > On *nix, I think if you put the tty into raw mode you should
    > be able to select on stdin and detect unbuffered keypresses.


    That was the trick I needed. Thank you.

    Also thanks to Lennon Day-Reynolds, who made it look good. ;)

    James Edward Gray II
     
    James Edward Gray II, Sep 9, 2004
    #6
    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. Replies:
    2
    Views:
    655
    Michael Hoffman
    Jun 24, 2005
  2. Mathias Dahl
    Replies:
    0
    Views:
    322
    Mathias Dahl
    Oct 22, 2005
  3. Mathias Dahl

    Listening for keypress in the background

    Mathias Dahl, Oct 22, 2005, in forum: Python
    Replies:
    4
    Views:
    514
    Peter Hansen
    Oct 23, 2005
  4. shannonl
    Replies:
    9
    Views:
    413
    shannonl
    Oct 31, 2005
  5. Replies:
    17
    Views:
    591
    Seamus MacRae
    May 20, 2009
Loading...

Share This Page