>Okay, one more quick question after searching the usual resources and not
>finding anything that looks quite right.  (I never figured out how to do
>this in Unix Perl either, but I figure it has to be a piece of cake for
>someone out there.)
>
>I have a real-time control application that needs to poll STDIN, which
>will actually be the keyboard in this case. Specifically, I need a
>function that will return a keystroke if a key has been pressed (without
>requiring the return key to be pressed as well), and otherwise return null
>and continue with the main loop.
>
>An answer to this will keep many people from becoming vaguely unsettled on
>Saturday night when the lights get out of sync with the music.  :)
>
>Simple, right?  I hope so...
>
>Thanks,
>Ethan

The following script demonstrates a method of determining when a key is
pressed (keyDown) as well as when a key is released (keyUp). The line
"sleep(1);" makes the script a bit clunky but is there merely to slow
things down so you can see what's happening.

#!perl

use Mac::Events;
use Mac::Events qw(@Event $CurrentEvent);
use Mac::LowMem;

$Event[keyDown] = \&keyDown_Handler;
$Event[keyUp]   = \&keyUp_Handler;
LMSetSysEvtMask(LMGetSysEvtMask() | keyUpMask);

print "Hold down one or more keys. (Press cmd \".\" to quit)\n\n";

        for $i (1..30) {
                print "$i\n";
                WaitNextEvent;
                if($flag) { last }
                sleep(1);
        }

print "\nDONE!\n";
exit;

sub keyDown_Handler {
    &getEvent;
    if (($CurrentEvent->modifiers & 256) == 256 && $key eq ".") { $flag = 1 }
    printf(" \'$key\' = down\n",$ascii);
}

sub keyUp_Handler {
    &getEvent;
    printf(" \'$key\' = up\n",$ascii);
}

sub getEvent {
    my($ev) = @_;
    $ascii = $ev->character;
    $key = chr($ascii);
}

__END__



Reply via email to