How to read a character at a time from STDIN without needing topress return key?

Discussion in 'Ruby' started by Kenneth McDonald, Sep 3, 2008.

  1. In the following bit of code:

    print STDIN.getc
    STDOUT.flush

    The code for whatever key I type isn't actually printed out until I go
    on to hit the return key. How can I have it printed as soon as I type
    a character key?

    Thanks,
    Ken
     
    Kenneth McDonald, Sep 3, 2008
    #1
    1. Advertisements

  2. Nobuyoshi Nakada, Sep 5, 2008
    #2
    1. Advertisements

  3. Hi,

    Am Donnerstag, 04. Sep 2008, 05:40:43 +0900 schrieb Kenneth McDonald:
    > In the following bit of code:
    >
    > print STDIN.getc
    > STDOUT.flush
    >
    > The code for whatever key I type isn't actually printed out until I go on
    > to hit the return key. How can I have it printed as soon as I type a
    > character key?


    On feasible operating systems you can install the "termios" gem.
    Then say something like:

    class IO
    def nocanon
    term = Termios::getattr self
    term.c_lflag &= ~Termios::ICANON
    Termios::setattr self, Termios::TCSANOW, term
    yield
    ensure
    term.c_lflag |= Termios::ICANON
    Termios::setattr self, Termios::TCSANOW, term
    end
    end

    $stdin.nocanon do
    c = $stdin.getc
    end

    Maybe you like to have a look at how I let the user enter
    passwords: <http://bertram-scharpf.homelinux.com/src/password.rb>.

    Good luck!

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Sep 5, 2008
    #3
  4. Kenneth McDonald

    James Gray Guest

    On Sep 3, 2008, at 3:40 PM, Kenneth McDonald wrote:

    > In the following bit of code:
    >
    > print STDIN.getc
    > STDOUT.flush
    >
    > The code for whatever key I type isn't actually printed out until I
    > go on to hit the return key. How can I have it printed as soon as I
    > type a character key?


    http://blog.grayproductions.net/articles/i_just_want_one_character

    Hope that helps.

    James Edward Gray II
     
    James Gray, Sep 5, 2008
    #4
    1. Advertisements

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. Johan
    Replies:
    3
    Views:
    790
    CBFalconer
    May 11, 2004
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    1,042
    Dan Pop
    Jun 21, 2004
  3. KwikRick
    Replies:
    1
    Views:
    506
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
  4. eap.britzkrieg
    Replies:
    4
    Views:
    2,862
    Eder Perez
    Jun 8, 2009
  5. M P
    Replies:
    1
    Views:
    841
  6. xdevel1999

    read DEL and BACK key from stdin

    xdevel1999, Dec 9, 2011, in forum: Java
    Replies:
    2
    Views:
    429
    Roedy Green
    Dec 9, 2011
  7. PerlFAQ Server
    Replies:
    0
    Views:
    293
    PerlFAQ Server
    Jan 24, 2011
  8. PerlFAQ Server
    Replies:
    0
    Views:
    243
    PerlFAQ Server
    Feb 20, 2011
Loading...