RCR getch

Discussion in 'Ruby' started by Roger Pack, Jul 6, 2010.

  1. Roger Pack

    Roger Pack Guest

    It would be quite convenient to have a getch method in Ruby.
    Any opposition to my suggesting its addition to core?
    -r
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Jul 6, 2010
    #1
    1. Advertising

  2. Roger Pack wrote:
    > It would be quite convenient to have a getch method in Ruby.
    > Any opposition to my suggesting its addition to core?
    > -r


    $ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
    STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
    ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
    hello
    "hello\n"
    "world!"
    "\n"

    --
    Nobu Nakada
    --
    Posted via http://www.ruby-forum.com/.
     
    Nobuyoshi Nakada, Jul 6, 2010
    #2
    1. Advertising

  3. Roger Pack

    botp Guest

    On Tue, Jul 6, 2010 at 2:43 PM, Nobuyoshi Nakada <> wrote:
    > $ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
    > STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
    > ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
    > hello
    > "hello\n"
    > "world!"
    > "\n"


    uber cool. thank for the tip, nobu.
    best regards -botp
     
    botp, Jul 6, 2010
    #3
  4. Roger Pack

    Roger Pack Guest


    > $ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
    > STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
    > ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
    > hello
    > "hello\n"
    > "world!"
    > "\n"


    Excellent. I'm glad this was added then.

    This appears to not work on windows is that expected?
    -r
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Jul 6, 2010
    #4
  5. Roger Pack

    Roger Pack Guest

    Oops I missed a require:

    >> require 'io/console'

    => true
    >> STDIN.getch


    > Excellent. I'm glad this was added then.
    >
    > This appears to not work on windows is that expected?


    It does work, and, though it doesn't get arrow keys, it's good enough
    for my intents and purposes.

    NB if you *do* want arrow keys you'll want to wrap _getch or _getkbhit,
    like rb-readline does. See also [1]..

    -r

    [1] http://betterlogic.com/roger/?p=2930

    For google's sake, here's what it does when you *don't* first require
    io/console:

    >> STDIN.getch

    NoMethodError: undefined method `getch' for #<IO:<STDIN>>
    from (irb):1
    from C:/installs/ruby_trunk_installed/bin/irb.bat:20:in `<main>'
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Jul 6, 2010
    #5
  6. Nobuyoshi Nakada, Jul 23, 2010
    #6
  7. Roger Pack

    Roger Pack Guest

    >> NB if you *do* want arrow keys you'll want to wrap _getch or _getkbhit,
    >> like rb-readline does. See also [1]..

    > What does it return when arrow keys are hit?


    224 + (some number like 75 for left arrow). It appears to not track when
    the shift key is hit and things like that, though. Not sure if those
    are accessible...
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Jul 23, 2010
    #7
    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. azsx

    getch in Java

    azsx, May 3, 2006, in forum: Java
    Replies:
    5
    Views:
    9,501
  2. Matthew
    Replies:
    0
    Views:
    2,915
    Matthew
    Jun 28, 2003
  3. voidstar
    Replies:
    3
    Views:
    10,132
    Mark R Rivet
    Jul 27, 2004
  4. Shugong Wang
    Replies:
    2
    Views:
    9,646
    Raymond Martineau
    Nov 28, 2004
  5. aurgathor
    Replies:
    4
    Views:
    7,976
    Karl Heinz Buchegger
    Mar 16, 2005
Loading...

Share This Page