Perl Cookbook

Perl CookbookSearch this book
Previous: 15.8. Using POSIX termiosChapter 15
User Interfaces
Next: 15.10. Reading Passwords

15.9. Checking for Waiting Input


You want to know whether keyboard input is waiting without actually reading it.


Use the CPAN module Term::ReadKey, and try to read a key in non-blocking mode by passing it an argument of -1:

use Term::ReadKey;

ReadMode ('cbreak');

if (defined ($char = ReadKey(-1)) ) {
    # input was waiting and it was $char
} else {
    # no input was waiting

ReadMode ('normal');                  # restore normal tty settings


The -1 parameter to ReadKey indicates a non-blocking read of a character. If no character is available, ReadKey returns undef.

See Also

The documentation for the Term::ReadKey module from CPAN; Recipe 15.6

Previous: 15.8. Using POSIX termiosPerl CookbookNext: 15.10. Reading Passwords
15.8. Using POSIX termiosBook Index15.10. Reading Passwords