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

    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?

    Kenneth McDonald, Sep 3, 2008
    1. Advertisements

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

  3. Hi,

    Am Donnerstag, 04. Sep 2008, 05:40:43 +0900 schrieb Kenneth McDonald:
    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
    term.c_lflag |= Termios::ICANON
    Termios::setattr self, Termios::TCSANOW, term

    $stdin.nocanon do
    c = $stdin.getc

    Maybe you like to have a look at how I let the user enter
    passwords: <>.

    Good luck!

    Bertram Scharpf, Sep 5, 2008
  4. Kenneth McDonald

    James Gray Guest

    Hope that helps.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.