为gtk#(Mono)启用键事件处理

时间:2014-01-12 01:16:11

标签: mono keyboard gtk keypress gtk#

当用户按下某个键时,以下代码不会触发KeyPress事件...

public class FooWidget : DrawingArea {

    public FooWidget () {
        this.AddEvents ((int)Gdk.EventMask.KeyPressMask);
    }

    [GLib.ConnectBefore]
    protected override bool OnKeyPressEvent (Gdk.EventKey evnt) {
        Console.WriteLine (evnt);
        return base.OnKeyPressEvent (evnt);
    }

}

出了什么问题?

1 个答案:

答案 0 :(得分:2)

自己找到它:

您需要专注于Widget

this.CanFocus = true;
this.Focus();