Perl中的C++ getch()?

Kar*_*ngh 5 perl getch

在c ++中,有一个函数getch(),它返回你按下的键的变量 - 就像输入将是13.我怎么能在perl中这样做?

Sva*_*nte 8

您可以使用Term :: ReadKey.


Ole*_*aev 5

简而言之:

$x = ord(getc);
Run Code Online (Sandbox Code Playgroud)

详细的:

$c = getc(STDIN);
$x = ord($c);
Run Code Online (Sandbox Code Playgroud)

perldoc -f getc

"However, it cannot be used by itself to fetch single 
characters without waiting for the user to hit enter. 
For that, try something more like":

   1.  if ($BSD_STYLE) {
   2. system "stty cbreak </dev/tty >/dev/tty 2>&1";
   3. }
   4. else {
   5. system "stty", '-icanon', 'eol', "\001";
   6. }
   7.
   8. $key = getc(STDIN);
   9.
  10. if ($BSD_STYLE) {
  11. system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  12. }
  13. else {
  14. system 'stty', 'icanon', 'eol', '^@'; # ASCII NUL
  15. }
  16. print "\n";
Run Code Online (Sandbox Code Playgroud)