Re: Key press events on Canvas



On Sat, 2015-10-31 at 12:20 -0300, John Coppens wrote:
On Fri, 30 Oct 2015 23:22:35 +0000
Damon Chaplin <damonachaplin gmail com> wrote:

On Fri, 2015-10-30 at 14:24 -0300, John Coppens wrote:
Hi all,

I tried to react on keypress events over a Canvas widget. Though the
code seems right (no compiler complaints), no events are generated.
Button press/release events and Enter/Leave events work fine.

I thought the button press events might be masked out, but even
after    canvas.set_events(Gdk.EventMask.KEY_PRESS_EVENT), no events
appeared.

I'm using GooCanvas 2.0.2 (using introspection), and Python 3.4.3.

Is there any way to detect keypresses?

It should work. There is a page in the demo app to demonstrate it.

Have you set the CAN_FOCUS widget flag, to let GTK+ know the canvas can
take the keyboard focus?

  gtk_widget_set_can_focus (canvas, TRUE);

Damon



Hi Damon:

Thanks for replying. Here's (some of) the code (I added can_focus for testing):

      self.canvas = GooCanvas.Canvas()
...
        self.canvas.set_events(Gdk.EventMask.KEY_PRESS_MASK)          # Makes no difference

        self.canvas.connect("button-release-event", self.on_darea_button_released)
        self.canvas.connect("key-press-event",      self.on_darea_key_pressed)
        self.canvas.connect("key-release-event",    self.on_darea_key_released)
        self.canvas.set_can_focus(True)                                       # Makes no difference

...

    def on_darea_button_released(self, darea, event):                 # Gets called correctly
        print("released")

    def on_darea_key_pressed(self, darea, event):                     # Never gets called
        print("key pressed")

    def on_darea_key_released(self, darea, event):                    # Never gets called
        print("key released")

So, no luck with the can_focus setting. I'm using Python, so maybe a problem with the
bindings? I believe I've seen a Python version of the demo - I'll try and search for
it. I remember it did behave differently from the C version.

John


You may need to make sure the canvas has the keyboard focus too, if
there are multiple widgets that can take the focus in the window.

Either manually by tabbing to it, or using gtk_widget_grab_focus().

(I doubt it is a bindings problem, as it is basic GTK+ stuff.)

Damon




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]