Read a single char from stdin without pressing enter
Asked Answered
V

1

10

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
Vladimar answered 10/10, 2016 at 17:52 Comment(0)
I
19

Try this:

char = STDIN.raw &.read_char
p char
Ioyal answered 10/10, 2016 at 22:10 Comment(2)
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.Sarco
NB: This no longer works in 1.11.1.Autacoid

© 2022 - 2024 — McMap. All rights reserved.