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

The following script is nonsense but it shows my goal. I want to "remap" the keyboard and be able to bind the keyboard key to the hexadecimal value of the keyboard: F1 -> 0x70.

use strict; use warnings; use Tk; our $name; my $mw = MainWindow->new(); my $entry = $mw->Entry( -textvariable => \$name )->pack(); $mw-> bind('<F1>', sub{entervalue()}); $mw->MainLoop(); exit(0); sub entervalue{ $name = "xxx"; }

I need this solution because I do want to "jump" the layout of a specific keyboard and assign a defined character/string to a specific physical key. Is this possible?

Replies are listed 'Best First'.
Re: Tk bind key hexadecimal value
by kcott (Archbishop) on Apr 21, 2018 at 11:18 UTC

    Perhaps something like this using a virtual event:

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(); $mw->eventGenerate('<<FakeF1>>'); $mw->eventAdd('<<FakeF1>>' => '<F2>'); $mw->bind('<F1>' => [\&f1_callback, Ev('%k')]); $mw->bind('<<FakeF1>>' => [\&f1_callback, Ev('%k')]); MainLoop; sub f1_callback { printf "F1 callback generated with %#x\n", $_[1]; }

    When I run this and press F1 followed by F2, I get this output:

    F1 callback generated with 0x43 F1 callback generated with 0x44

    Those methods are documented in Tk::event and Tk::bind.

    — Ken

Re: Tk bind key hexadecimal value
by Discipulus (Canon) on Apr 20, 2018 at 15:47 UTC
    Hello dunno if I understood, but you probably find XEvent useful. See it here explained

    use strict; use Tk; 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"; }

    See it Keyboard input with Perl/Tk's -bind()

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thank you. But this is not exactly what I want, because both $e->K and $e->N depend on the keyboard language. If I change my keyboard language /both on macOS and on Win32, for the same "physical" key I get different values. In this way I need to know beforehand the language of the keyboard to perform my new keyboard mapping. I was hoping that each key of the keyboard has a unique identifier which is bound with the hardware (also with the physical keys) and not change depending on the keyboard input language.

        Maybe I do not need to go so deep as I thought and get the key "hardware code", I could just "translate" any value of a keyboard press ($e->K) to another value