Click here to get back home

Module to detect keyboard presses

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Module to detect keyboard presses Ignoramus3938 03-30-2007
Posted by Ignoramus3938 on March 30, 2007, 8:09 am
Please log in for more thread options


I have a script that runs in a loop and I would like it to accept
OPTIONAL keypresses.

e.g.

while( 1 ) {

do_my_stuff();

my $key = get_optional_key();
if( defined $key ) { # key was pressed
if( $key eq 'b' ) { skip_backwards(); }
elsif( $key eq 'f' ) { skip_forward(); }
elsif( $key eq ' ' ) { pause(); }
} else {
print "No key was pressed.\n";
}
}

The key point here is that I do NOT want to wait for mandatory keys at
every iteration, I just want a nonblocking call to get a [possible]
key press event[s];

MS-DOS used to have this facility a long time ago.

I would like to know if there is already a module for this.

I am using Linux.

thanks


i

Posted by anno4000 on March 30, 2007, 8:21 am
Please log in for more thread options


> I have a script that runs in a loop and I would like it to accept
> OPTIONAL keypresses.

See "perldoc -q 'just one key'".

Anno

Posted by Ignoramus31975 on March 30, 2007, 10:05 am
Please log in for more thread options


On 30 Mar 2007 12:21:17 GMT, anno4000@radom.zrz.tu-berlin.de
comp.lang.perl.misc:
>> I have a script that runs in a loop and I would like it to accept
>> OPTIONAL keypresses.
>
> See "perldoc -q 'just one key'".
>
> Anno
Thanks, I just read this page

LANG="" man Term::ReadKey


and it looks like it will do just what I want.

i

Posted by anno4000 on March 30, 2007, 11:06 am
Please log in for more thread options


comp.lang.perl.misc:
> On 30 Mar 2007 12:21:17 GMT, anno4000@radom.zrz.tu-berlin.de
> comp.lang.perl.misc:
> >> I have a script that runs in a loop and I would like it to accept
> >> OPTIONAL keypresses.
> >
> > See "perldoc -q 'just one key'".
> >
> > Anno
> Thanks, I just read this page
>
> LANG="" man Term::ReadKey
>
> and it looks like it will do just what I want.

Right. That's what the FAQ answer also points to.

Anno

Posted by Ignoramus31975 on March 30, 2007, 11:29 am
Please log in for more thread options


On 30 Mar 2007 15:06:06 GMT, anno4000@radom.zrz.tu-berlin.de
comp.lang.perl.misc:
>> On 30 Mar 2007 12:21:17 GMT, anno4000@radom.zrz.tu-berlin.de
>> comp.lang.perl.misc:
>> >> I have a script that runs in a loop and I would like it to accept
>> >> OPTIONAL keypresses.
>> >
>> > See "perldoc -q 'just one key'".
>> >
>> > Anno
>> Thanks, I just read this page
>>
>> LANG="" man Term::ReadKey
>>
>> and it looks like it will do just what I want.
>
> Right. That's what the FAQ answer also points to.
>

Yes, I got this idea from the FAQ answer. That's why I thanked you --
your suggestion was great.

I wrote this script to test stuff.

#!/usr/bin/perl

use Term::ReadKey;

sub CleanUp { ReadMode 0; }
$SIG = sub { CleanUp; die 'SIGINT CALLED'; };

$| = 1;

ReadMode 3;

while( 1 ) {
#print "Type key: ";
#usleep 10;
my $c = ReadKey(-1);
if( $c ) {
print "C='$c'. ";
if( $c eq 'q' || $c eq 'Q' ) {
print "\n'q' pressed. Exiting.\n";
last;
} else {
print "Press 'q' to exit.\n";
}
} else {
#print "\n";
}
}

CleanUp;




Similar ThreadsPosted
Building module Encode::Detect on solaris 10 platform February 21, 2008, 9:13 am
remote keyboard and mouse IO February 21, 2005, 3:22 am
How to detect an image type with GD November 8, 2006, 12:10 pm
HTTP::Proxy - how to detect start and end of URL fetch September 26, 2004, 6:00 am
Lower case module name for non-pragma module January 4, 2005, 10:19 am
RFC: New module 'Module::Bundled::Files' August 26, 2005, 3:49 pm
help with an MD5.pm module!! September 3, 2005, 11:39 pm
Looking for RTP module December 9, 2004, 9:17 pm
module for FFT May 9, 2005, 3:06 pm
Module may not be right one? April 27, 2006, 12:57 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap