Perl/Tk does not provide such functionality. So you would have to track the events yourself. Note that there are the Any-KeyPress
and Any-KeyRelease
events, so you don't have to create a binding for every key:
$mw->bind("<Any-KeyPress>" => sub {
warn $_[0]->XEvent->K; # prints keysym
});
If you're on X11, then using the X11::Protocol
module (which can be used within a Perl/Tk script) and calling the QueryKeymap
method would give you the actually pressed keycodes. Here's a little script which demonstrates this:
use strict;
use X11::Protocol;
# Get the keycode-to-keysym mapping. Being lazy, I just parse
# the output of xmodmap -pke. The "real" approach would be to
# use X11 functions like GetKeyboardMapping() and the
# X11::Keysyms module.
my %keycode_to_keysym;
{
open my $fh, "-|", "xmodmap", "-pke" or die $!;
while(<$fh>) {
chomp;
if (m{^keycode\s+(\d+)\s*=(?:\s*(\S+))?}) {
if (defined $2) {
$keycode_to_keysym{$1} = $2;
}
} else {
warn "Cannot parse $_";
}
}
}
my $x11 = X11::Protocol->new;
while(1) {
my $keyvec = $x11->QueryKeymap;
for my $bit (0 .. 32*8-1) {
if (vec $keyvec, $bit, 1) {
warn "Active key: keycode $bit, keysym $keycode_to_keysym{$bit}\n";
}
}
sleep 1;
}