STDIN.getc
returns a buffered character from STDIN. How do I get a character from STDIN immediately without buffering?
This code looks well, but it doesn't work as you expect. The input characters will be still buffered.
STDIN.sync = true
p STDIN.getc
Curses.getch
require 'curses'
p Curses.getch
This works, but there is an undesired side-effect. Curses.getch
clears the screen.
stty raw
orig_stty = `stty -g`
system 'stty raw echo'
p STDIN.getc
system 'stty', orig_stty
This works, but some platform don't support the command.
Conclusion
Getting a character immediately looks easy, but it actually isn't easy.
No comments:
Post a Comment