naggiman has asked for the wisdom of the Perl Monks concerning the following question:

I am using the X11::Protocol and Tk modules, and I want to convert any given character into its keysym name. How can I do this? There doesnt seem to be any obvious way, apart from having to generate my own hash table by hand (laborious, time consuming and error prone), i.e.

my %char_to_keysym = ( '&' => "ampersand", ' ' => "space", '=' => "equal", '+' => "plus", ..... );

Reason is I want to generate some XKeyPress/Release events for any given string, but cannot find how to do a lookup on non-alphanumeric keys...

Thanks

Replies are listed 'Best First'.
Re: Converting characters to keysyms
by zentara (Cardinal) on Feb 18, 2005 at 13:45 UTC
    #!/usr/bin/perl use warnings; use strict; use Tk; #mouse must be positioned over "Exit" button for this to work. my $mw = MainWindow->new; $mw->bind( '<KeyPress>' => \&print_keysym ); $mw->Button( -text => 'Exit', -command => sub { exit(); } )->pack; MainLoop; sub print_keysym { my $widget = shift; my $e = $widget->XEvent; my ( $keysym_text, $keysym_decimal ) = ( $e->K, $e->N ); print "keysym=$keysym_text, numberic=$keysym_decimal\n"; }

    I'm not really a human, but I play one on earth. flash japh
      This will allow me to get the codes interactively, and so remove some of the hassle, but I may still miss some keys. I would like to grab the whole lot all in one go on the fly rather than labouriously hard-code.