Perl:睡眠时间为x秒,除非是按键

时间:2015-04-05 19:08:21

标签: perl sleep

在perl中有没有办法让程序在一定时间内休眠,除非用户按下某个键?以下是我的代码:

use Term::ReadKey;
ReadMode 4;
while (not defined ($key = ReadKey(-1))) {
    sleep(50);
}
print "Get key $key\n";
ReadMode 0; 

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。这是一个基本答案

use Term::ReadKey;
ReadMode 4;
while(1) {
    my $key = ReadKey 50; #Sleep for 50
    if(defined $key) {
        print "Pressed $key\n";
    } else {
        print "Timed out\n";
    }
}
ReadMode 0;