*** empty log message ***
[PsN.git] / lib / hotkey.pm
blobcb155bd889fd5cc93c426dfa2507fa98ff6c8453
1 # HotKey.pm
2 package hotkey;
4 @ISA = qw(Exporter);
5 @EXPORT = qw(cbreak cooked readkey);
7 use strict;
8 use POSIX qw(:termios_h);
9 my ($term, $oterm, $echo, $noecho, $fd_stdin);
11 $fd_stdin = fileno(STDIN);
12 $term = POSIX::Termios->new();
13 $term->getattr($fd_stdin);
14 $oterm = $term->getlflag();
16 $echo = ECHO | ECHOK | ICANON;
17 $noecho = $oterm & ~$echo;
19 sub cbreak {
20 $term->setlflag($noecho); # ok, so i don't want echo either
21 $term->setcc(VMIN, 0);
22 $term->setcc(VTIME, 1);
23 $term->setattr($fd_stdin, TCSANOW);
26 sub cooked {
27 $term->setlflag($oterm);
28 $term->setcc(VTIME, 0);
29 $term->setattr($fd_stdin, TCSANOW);
32 sub readkey {
33 my $key = '';
34 cbreak();
35 sysread(STDIN, $key, 1);
36 cooked();
37 return $key;
40 END { cooked() }
42 1;