Question

I have code like this:

    ...

    entry.KeyPressEvent += EntryKeyPressEvent;

    ...
}

void EntryKeyPressEvent(object o, KeyPressEventArgs args)
{
    Console.WriteLine("DEBUG: KeyValue: " + args.Event.KeyValue);
    ...
}

EntryKeyPressEvent is called when most keys are pressed, but not the return key. Why is this?

Edit: It's actually not called when most keys are pressed. Some call it (e.g. uparrow, downarrow, escape), but most do not (e.g. any letter key, return).

For reference, I'm trying to port PyGTK code that looks like this:

    ...

    entry.connect('key_press_event', self.entry_key_pressed)

    ...

def entry_key_pressed(self, widget, event):
    ...
Was it helpful?

Solution

I was able to figure it out from this mailing list thread. All I had to do was apply to the ConnectBefore Attribute to the handler method:

[ConnectBefore]
void EntryKeyPressEvent(object o, KeyPressEventArgs args)
{
    Console.WriteLine("DEBUG: KeyValue: " + args.Event.KeyValue);
    ...
}

OTHER TIPS

From the PyGTK FAQ:

Keyboard events are handled differently. When your window receives a keyboard event, it is first dispatched to the toplevel window, which will check if it matches any keyboard shortcuts. If the key press doesn't match a shortcut, then the event is dispatched to the child widget that currently has focus.

In short: your Gtk.Entry is not seeing the key-press-event signal because the top-level window is doing something with it (namely, activating the currently selected widget).

A meta-answer: often, searching other languages' GTK docs is surprisingly helpful.

Are you trying to validate the input? The focus-out-event fires as focus leaves the entry box - like when the user hits Enter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top