如何在不按回车键的情况下获取单个字符? [英] How to get a single character without pressing enter?
本文介绍了如何在不按回车键的情况下获取单个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不按 Enter 键的情况下使用 Ruby 从终端获取单个键盘字符?我尝试了 Curses::getch
,但这对我来说并不奏效.
How can I get a single keyboard character from the terminal with Ruby without pressing enter?
I tried Curses::getch
, but that didn't really work for me.
推荐答案
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/2999
#!/usr/bin/ruby
begin
system("stty raw -echo")
str = STDIN.getc
ensure
system("stty -raw echo")
end
p str.chr
(在我的 OS X 系统上测试,可能无法移植到所有 Ruby 平台).请参阅 http://www.rubyquiz.com/quiz5.html 以获取一些其他建议,包括窗户.
(Tested on my OS X system, may not be portable to all Ruby platforms). See http://www.rubyquiz.com/quiz5.html for some additional suggestions, including for Windows.
这篇关于如何在不按回车键的情况下获取单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文