9

How can I read a single char from the console without pressing enter / return?

In ruby I would just use:

require 'io/console'
input = STDIN.getch
Leonard Schütz
  • 517
  • 1
  • 7
  • 16

1 Answers1

18

Try this:

char = STDIN.raw &.read_char
p char
asterite
  • 2,906
  • 1
  • 14
  • 14
  • Is there an equivalent of `STDIN.ready?` ? When I am reading Meta/Alt characters or Shifted-Fn keys, I keep reading while `STDIN.ready?` and return with the collected keys when it is not ready. – rahul Apr 30 '19 at 06:55