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

Dear Monks,

Could someone help me in how to catch the simultaneous pressing of AltGr (or left Alt + Control) and any other character in a Tk application? Specifically, I want to catch the asterisk (*) character, even if it is not pressed on NumPad. If I print out the character codes (or texts) with XEvent, I see the AltGr and minus key codes (or texts) /keysym=Alt_R, numberic=65514 and keysym=minus, numberic=45/ in sequence.

Something like "<KeyPress-Asterisk>" would be best, but unfortunately there is no such thing ("bad event type or keysym").

Thanks in advance for your help!

Replies are listed 'Best First'.
Re: Binding AltGr + minus key (=asterisk)
by choroba (Cardinal) on May 01, 2024 at 21:36 UTC
    You can experiment with the following code. It might need some tweaking on MSWin or Mac:
    #!/usr/bin/perl use warnings; use strict; use Tk qw{ MainLoop Ev }; my $mw = 'MainWindow'->new; $mw->Label(-text => 'Press any key')->pack; $mw->Entry(-textvariable => \ my $text, -state => 'readonly')->pack; $mw->bind('<KeyPress>', [sub { $text = $_[1] }, Ev('K')]); my @mods = ("", qw( Alt Shift Control Mod4 )); for my $p1 (0 .. $#mods) { for my $p2 (0 .. $#mods) { next if $p2 == 0 && $p1 != 0 || $p2 && $p1 >= $p2; for my $p3 (1 .. $#mods) { next if $p3 <= $p2; my $prefix = join '-', grep length, @mods[$p1, $p2, $p3]; $mw->bind("<$prefix-KeyPress>", [sub { $text = "$_[1] + $_ +[2]" }, "$prefix", Ev('K')]); } } } $mw->bind("<Alt-Shift-Control-Mod4-KeyPress>", [sub { $text = "$_[1] + $_[2]" }, 'Alt-Shift-Control-Meta', Ev('K')]); MainLoop();

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Dear Choroba,

      Thank you very much for the script. Unfortunately, it did not work on windows (I will try it on Linux tomorrow, although the target application must run on windows). The situation is: if I press AltGr, your script prints Control + ALT_R, but if I hold it down and press the minus key (which would create the asterisk), only minus appears in the entry. The situation is similar if I want to use 3 keys to create the asterisk: control + left alt + minus key. In this case also only the minus will remain in the entry at the end of the process.

      I also tried Term::Readkey to get the asterisk (not entered from numpad), but this is very difficult to implement in a Tk environment.