g_signal_connect用于鼠标右键单击?

时间:2014-05-07 11:47:21

标签: c gtk

我得到的代码适用于鼠标左键单击按钮,但我如何使用鼠标右键单击信号:

g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button-action), NULL);

1 个答案:

答案 0 :(得分:6)

一种简单的方法来监听任何鼠标点击,无论是向左还是向右,都是这样的:

g_signal_connect(
    G_OBJECT(button)
    "button-press-event",
    G_CALLBACK(btn_press_callback),
    NULL
);

然后,对于回调函数:

gboolean btn_press_callback(GtkWidget *btn, GdkEventButton *event, gpointer userdata)
{
    if (event->type == GDK_BUTTON_PRESS  &&  event->button == 3)
    {//3 is right mouse btn
        //do stuff
        return true;//or false
    }
    if (event->type == GDK_BUTTON_PRESS  &&  event->button == 1)
    {//1 is left mouse btn
    }
}

依旧...... More info here

一些例子,使用GTK + -2但仍然有用,can be found here