###   Hi David,
###        better use "sprintf" in two places, which stands
###        for *s*tring *print* with *f*ormat, OSTLT.
###        And maybe it is better to mathematically _add_ those
###        seconds and microseconds rather than _concatenating_
###        their strings.
###
###        ;) My best result in quick typing was 0.008 seconds.
###
###   Regards, Detlef



###  David's report on typed keys:


#!perl

use Mac::Events;
use Mac::Events qw(@Event $CurrentEvent);
use Mac::LowMem;
if($] > 5.006) { require Time::HiRes }

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

print "Hold down one or more keys. (Press cmd \".\" to quit)\n\n";
while (! $flag) {
    WaitNextEvent }
print "\nDONE!\n";
exit;

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

sub keyUp_Handler {
    &getEvent;
    if ($k{$key}) {
        $dif = $sec - $k{$key};
        # $elapsed = substr($dif,0,index($dif,'.') + 4);    ## better do this:
        $elapsed = sprintf("%4.3f", $dif);

        printf(" \'$key\' = up    ASCII = %3d  (down for $elapsed seconds)\n",$ascii);
        $k{$key} = "";     }
    else {
        printf(" \'$key\' = up    ASCII = %3d (wasn\'t down",$ascii);
        if ($k{"\u$key"}) {
            print " but \'\u$key\' was)\n";
            $k{"\u$key"} = "";    }
        elsif ($k{"\l$key"}) {
            print " but \'\l$key\' was)\n";
            $k{"\l$key"} = "";     }
        else { print "... What key was down?)\n"   }   }   }

sub getEvent {
    if($] > 5.006)   {
        ($seconds, $microseconds) = Time::HiRes::gettimeofday();
        $sec = "$seconds.".sprintf("%06s", $microseconds);    }   ## use sprintf here
    else { $sec = time }
    my($ev) = @_;
    $ascii = $ev->character;
    $key = chr($ascii);       }

__END__




Reply via email to