#!/usr/bin/env perl # # checkkeys.pl # # Copyright (C) 2005 David J. Goehrig # # ------------------------------------------------------------------------------ # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # ------------------------------------------------------------------------------ # # Please feel free to send questions, suggestions or improvements to: # # David J. Goehrig # dgoehrig@cpan.org # use SDL; use SDL::App; use SDL::Event; my %options; $options{-flags} = SDL_SWSURFACE; $options{-title} = $0; $options{-width} ||= 640; $options{-height} ||= 480; $options{-depth} ||= $options{-bpp} || 24; my $app = new SDL::App %options; # SDL::EventState(SDL_KEYUP,SDL_DISABLE); sub print_modifiers { $mod = SDL::GetModState(); print " modifiers:", ($mod & KMOD_LSHIFT) ? " LSHIFT" : "", ($mod & KMOD_RSHIFT) ? " RSHIFT" : "", ($mod & KMOD_LCTRL) ? " LCTRL" : "", ($mod & KMOD_RCTRL) ? " RCTRL" : "", ($mod & KMOD_LALT) ? " LALT" : "", ($mod & KMOD_RALT) ? " RALT" : "", ($mod & KMOD_LMETA) ? " LMETA" : "", ($mod & KMOD_RMETA) ? " RMETA" : "", ($mod & KMOD_CAPS) ? " CAPS" : "", ($mod & KMOD_NUM) ? " NUM" : "", ($mod & KMOD_MODE) ? " MODE" : "", "\n" ; } sub print_key { my ($e) = @_; print "pressed " if (SDL::KeyEventState($e) == SDL_PRESSED); print "released " if ( SDL::KeyEventState($e) == SDL_RELEASED); my $sym = SDL::KeyEventSym($e); if ($sym) { print SDL::GetKeyName($sym); } else { printf "Unknown Key (scancode = %d) ", SDL::KeyEventScanCode($e); } } my $event = new SDL::Event; my $done = 0; $process_keys = sub { print_key($_[0]); print_modifiers(); }; my %events = ( SDL_KEYUP() => $process_keys, SDL_KEYDOWN() => $process_keys, SDL_QUIT() => sub { $done = 1; }, ); while (!$done && $event->wait()) { if ( $events{$event->type()}) { &{$events{$event->type()}}($$event); } };