2011-09-04 34 views
2

在Ruby中,我該如何讓終端不要回顯按鍵?在Ruby中,如何讓終端不要回顯按鍵?

與SSH要求輸入密碼相同。鍵被鍵入,但終端上不顯示任何內容。

+0

可能[如何從ruby腳本中的終端隱藏密碼輸入]的副本(http://stackoverflow.com/questions/2338889/how-to-hide-password-input-from-terminal-in-ruby-script) – krock

回答

1

您卡恩也使用stty的回聲禁用:

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

而在1.9.3中,IO /控制檯庫將允許你做一個更便攜的方式:

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