Linux::Joystick is an object-oriented, pure Perl API for accessing joystick devices under Linux-based operating systems. Linux::Joystick module is capable of using either blocking or non-blocking I/O, and represents each axis change or button press as a Linux::Joystick::Event object.
USAGE
If you want your application to be driven by joystick events, use blocking I/O and an event loop:
use Linux::Joystick;
my $js = new Linux::Joystick;
my $event;
print "Joystick has " . $js->buttonCount() . " buttons ".
"and " . $js->axisCount() . " axes.n";
# blocking reads:
while( $event = $js->nextEvent ) {
print "Event type: " . $event->type . ", ";
if($event->isButton) {
print "Button " . $event->button;
if($event->buttonDown) {
print " pressed";
} else {
print " released";
}
} elsif($event->isAxis) {
print "Axis " . $event->axis . ", value " . $event->axisValue . ", ";
print "UP" if $event->stickUp;
print "DOWN" if $event->stickDown;
print "LEFT" if $event->stickLeft;
print "RIGHT" if $event->stickRight;
} else { # should never happen
print "Unknown event " . $event->hexDump;
}
print "n";
}
# if the while loop terminates, we got a false (undefined) event:
die "Error reading joystick: " . $js->errorString;
You can also use non-blocking I/O, in which case nextEvent() returning undef just means there was no event to read:
my $js = Linux::Joystick->new(nonblocking => 1);
# use this to open 2nd joystick in nonblocking mode instead:
# my $js = Linux::Joystick->new(device => 1, nonblocking => 1);
while(1) {
my $event = $js->nextEvent;
if($event) {
print "Got a joystick eventn";
# process the event here
}
# Do other processing here (graphics, sound, I/O, calculation)
}
It is possible to switch between blocking and non-blocking I/O without reopening the device (see the setNonblocking() method, below).
Product's homepage
Requirements:
· Perl