【问题标题】:In Ruby, how to ask the terminal not to echo key presses?在 Ruby 中,如何要求终端不回显按键?
【发布时间】:2011-11-09 23:57:45
【问题描述】:

在 Ruby 中,如何让终端不回显按键?

与 SSH 要求输入密码时相同。键入了键,但终端上不会显示任何内容。

【问题讨论】:

标签: ruby keypress


【解决方案1】:

您也可以使用 stty 来禁用 echo :

def getch
  %x[stty -echo raw]
  c = $stdin.getc
  %x[stty echo -raw]
  c
end

在 1.9.3 中,io/console 库将允许您以更便携的方式进行操作:

require 'io/console'
$stdin.getch # does the same thing as above :)

【讨论】:

    【解决方案2】:

    查看highline gem - HighLine::Question 类有一个参数来控制输入文本的回显。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-10
      • 2014-08-10
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多