I know you said commandline, but if you can use Gtk2, you might like this. It's much cleaner.
#! /usr/bin/perl -w
use strict;
use Gtk2::Gdk::Keysyms;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;
my $window = Gtk2::Window->new ('toplevel');
$window->signal_connect (delete_event => sub { Gtk2->main_quit });
$window->signal_connect('key-press-event' => \&show_key);
my $label = Gtk2::Label->new();
$label->set_markup("<span foreground=\"blue\" size=\"x-large\">Ty
+pe something on the keyboard!</span>");
$window->add ($label);
$window->show_all;
$window->set_position ('center-always');
Gtk2->main;
sub show_key {
my ($widget,$event,$parameter)= @_;
my $key_nr = $event->keyval();
#run trough the available key names, and get the values of each,
#compare this with $event->keyval(), when you get a match exit the
+ loop
foreach my $key (keys %Gtk2::Gdk::Keysyms){
my $key_compare = $Gtk2::Gdk::Keysyms{$key};
if($key_compare == $key_nr){
#print ("You typed $key \n");
$label->set_markup("<span foreground=\"blue\" size=\"x-large\"
+>You typed a</span><span foreground=\"red\" size=\"30000\"><i><tt> $k
+ey</tt></i></span><span foreground=\"blue\" size=\"x-large\"> which h
+as a numeric value of </span><span foreground=\"red\" size=\"30000\">
+<i><tt> $key_nr!</tt></i></span>");
$widget->set_title("key pressed: $key -> numeric value: $key_n
+r");
last;
}
}
#good practice to let the event propagate, should we need it somew
+here else
return FALSE;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|