tags:

views:

341

answers:

3

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):
    ...
A: 

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

Robert Wohlfarth
+1  A: 

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.

RAOF
The weird thing is, I'm porting PyGTK code, and it works in PyGTK. I'll edit my original question to include the working python code.
Matthew
+1  A: 

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);
    ...
}
Matthew
Ah! So the Gtk.Entry was eating those keypress events! Strange that it didn't do that in Python.
RAOF