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

I'm trying to intercept keypresses like "g" or "CTRL P", or whatever.

The following test scripts works fine under Gtk2, but under Gtk3, I get this error message:

*** Gtk3::Gdk::keyval_name: passed too many parameters (expected 1, go +t 2); ignoring excess at keyval.pl line 22. Argument "Gtk3::Gdk" isn't numeric in subroutine entry at /usr/lib/x86_64-linux-gnu/perl5/5.26/Glib/Object/Introspection.pm +line 67 (#1)

I checked the docs (at https://developer.gnome.org/); I can't see any difference between Gtk2 and Gtk3 versions of the ->keyval function.

Any ideas, oh infinitely wise ones?

#!/usr/bin/perl use strict; use diagnostics; use warnings; use Gtk3 '-init'; my $window = Gtk3::Window->new('toplevel'); $window->set_title('Hello world'); $window->set_position('center'); $window->set_default_size(500, 500); $window->signal_connect('delete-event' => sub { Gtk3->main_quit(); exit; }); $window->signal_connect('key-press-event' => sub { my ($widget, $event) = @_; print "keyval " . Gtk3::Gdk->keyval_name($event->keyval) . "\n"; }); $window->show_all(); Gtk3->main();

Replies are listed 'Best First'.
Re: Use Gtk3::Gdk to intercept keypresses
by kcott (Archbishop) on Feb 13, 2019 at 05:56 UTC

    [Disclaimer: I'm not a user of Gtk3. The following is based on its documentation.]

    Firstly, a note on your error messages. The code

    Gtk3::Gdk->keyval_name($event->keyval)

    passes the class name, Gtk3::Gdk, as the first argument to keyval_name(). It's equivalent to

    keyval_name('Gtk3::Gdk', $event->keyval)

    From the messages, it's discarding $event->keyval, leaving

    keyval_name('Gtk3::Gdk')

    Presumably, $event->keyval returns a number, which seems to be what keyval_name() wants; however, its getting a string, hence: "Argument "Gtk3::Gdk" isn't numeric ...".

    You might try:

    Gtk3::Gdk::keyval_name($event->keyval)

    However, before doing that, I suggest you look at the "Porting from Gtk2 to Gtk3" section of the Gtk3 documentation. It has some information on Keysyms; although, I don't know if that helps you directly with this issue. Other parts may also be useful if you are indeed porting a current Gtk2 application to Gtk3.

    — Ken

      Using this:

      Gtk3::Gdk::keyval_name($event->keyval)

      instead of this:

      Gtk3::Gdk->keyval_name($event->keyval)

      ...was the first thing I tried, and for some inexplicable reason, it produces the same error message.

      I had also checked the Gtk2 > Gtk3 migration documents. The change to Keysyms seems to be a red herring; the test code translates a value like 65478 into a keycode like f10, which bypasses the corresponding key symbol GDK_F10 entirely.

        "The change to Keysyms seems to be a red herring; ..."

        Fair enough. I didn't spend any time investigating that avenue, but was somewhat doubtful, hence: "... although, I don't know if that helps you directly ...".

        From your follow-up post:

        "Correction: Gtk3::Gdk::keyval_name does work."

        Glad to hear it. I don't know if that's the best solution — as stated, "I'm not a user of Gtk3." — but at least it is a solution. Others may come up with something better.

        — Ken

        Correction: Gtk3::Gdk::keyval_name does work. I was getting the same error message because my code contained two ->keyval_name calls, only one of which had been updated.

Re: Use Gtk3::Gdk to intercept keypresses
by Anonymous Monk on Feb 12, 2019 at 20:36 UTC
    What does keyval actually return in both cases?