in reply to PgUp Home

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; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: PgUp Home
by halfcountplus (Hermit) on Mar 01, 2008 at 23:32 UTC
    Myself, i have no perl/Gtk2 installed, but i agree with your approach. Who really needs numeric keypad functionality or something on the console when Tk or Gtk will do this nicer and easier in X?

    Still, there can always be something learned from trying. But if the gods have ears may this save someone else a silly amount of time & everyone can move on.

    Or maybe a better Term::Readkey might come to exist.
      As far as commanline goes, GLib will readkeys too. See Readkey with timer using Glib. So you can get the keycodes with Glib in a commandlinr app, the only thing you need to do is find out where in the Gtk2 headers, they translate the keycodes to keyboard names like F2, etc. It's probably in a header file somewhere. In any event, it's alot easier to use an event-loop system, like Glib, Curses, POE, etc., than to use a while(1) loop.

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum