Single character input without pressing 'Enter'

Discussion in 'Ruby' started by unni.tallman, Sep 20, 2006.

  1. unni.tallman

    unni.tallman Guest

    how can i read input from stdin, char by char, i want to get the
    character as soon as it is entered, without having to press 'Enter'
    after each character.
    unni.tallman, Sep 20, 2006
    #1
    1. Advertising

  2. This is a simple way to achieve that. Maybe there are others

    def read_char
    system "stty raw -echo"
    STDIN.getc
    ensure
    system "stty -raw echo"
    end

    good luck



    unni.tallman wrote:
    > how can i read input from stdin, char by char, i want to get the
    > character as soon as it is entered, without having to press 'Enter'
    > after each character.
    >
    >
    >
    >
    Nguyen Huu Bach, Sep 20, 2006
    #2
    1. Advertising

  3. Nguyen Huu Bach wrote:
    > This is a simple way to achieve that. Maybe there are others
    >
    > def read_char

    # Save previous state
    old = `stty -g`
    > system "stty raw -echo"
    > STDIN.getc
    > ensure

    system "stty #{old}"
    > end
    >
    > good luck


    Might be better to use Termios or Curses, though.

    --
    Posted via http://www.ruby-forum.com/.
    Eero Saynatkari, Sep 20, 2006
    #3
  4. On Sep 19, 2006, at 11:33 PM, Eero Saynatkari wrote:

    > Nguyen Huu Bach wrote:
    >> This is a simple way to achieve that. Maybe there are others
    >>
    >> def read_char

    > # Save previous state
    > old = `stty -g`
    >> system "stty raw -echo"
    >> STDIN.getc
    >> ensure

    > system "stty #{old}"
    >> end
    >>
    >> good luck

    >
    > Might be better to use Termios or Curses, though.


    Or use HighLine which handles all these details for you.

    James Edward Gray II
    James Edward Gray II, Sep 20, 2006
    #4
    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. Jerry
    Replies:
    4
    Views:
    1,150
    John Saunders
    Jul 25, 2003
  2. paul
    Replies:
    1
    Views:
    500
    Jonathan Mcdougall
    Aug 4, 2003
  3. LaBird

    Input without pressing enter?

    LaBird, Oct 16, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,165
    Joona I Palaste
    Oct 16, 2003
  4. unni.tallman
    Replies:
    3
    Views:
    1,587
    Fredrik Lundh
    Sep 20, 2006
  5. bhutho
    Replies:
    3
    Views:
    556
    Turma 15 FCM
    Oct 28, 2011
Loading...

Share This Page