简体   繁体   English

GtkButton发出GDK_KEY_PRESS

[英]GtkButton to emit GDK_KEY_PRESS

I'm attempting to attach to a toolbar button a signal associated with a keystroke supported by GtkTextView (CTRL+a, CTRL+x, etc) using the following widget structure, signal connect, and callback: 我试图使用以下窗口小部件结构,信号连接和回调将与GtkTextView支持的按键相关的信号(CTRL + a,CTRL + x等)附加到工具栏按钮:

typedef struct
{
    GtkWidget *window;
    GtkWidget *text_view;
}EditorWidgets;

//...

    g_signal_connect(cut, "clicked", G_CALLBACK(cut_button_press), editor_widgets);

//...

static void cut_button_press(GtkWidget *button, EditorWidgets *editor_widgets)
{
    UNUSED(button);

    GdkEvent *event = gdk_event_new(GDK_KEY_PRESS);

    event->key.window = gtk_widget_get_window(editor_widgets->window);
    event->key.send_event = FALSE;
    event->key.time = 0;
    event->key.state = GDK_CONTROL_MASK;
    event->key.keyval = GDK_KEY_x;
    event->key.string = g_strdup("x");
    event->key.length = strlen(event->key.string);

    gtk_main_do_event(event);

    gdk_event_free(event);
}

When run, GDK complains with the following: 运行时,GDK会抱怨以下问题:

(ex1:7856): Gdk-WARNING **: Event with type 8 not holding a GdkDevice. It is most likely synthesized outside Gdk/GTK+

Which lets me know I've created the signal improperly (I assume). 这让我知道我创建了不正确的信号(我认为)。 However, there's very little out there about having buttons emit GDK signals, so I'm at a loss for what's missing here. 但是,关于按钮发出GDK信号的信息很少,因此我对这里缺少的内容感到迷惑。

As a secondary question, I remember reading somewhere that there was a GDK #define for TIME_NOW or something, but I couldn't find it again. 作为第二个问题,我记得在某处读到一个GDK #define用于TIME_NOW东西,但是我再也找不到了。 Any hints there? 有什么提示吗?

1.use "key-press-event" instead of clicked, clicked was associated with mouse pointer and keyboard enter key, something like (in python): 1.使用“ key-press-event”代替单击,单击与鼠标指针和键盘输入键相关联,类似于(在python中):

   def on_key_press (self, widget, event):
       if event.keyval == 120:
          print ('test')

2.GDK_CURRENT_TIME, define at Gdk.Event class 2.GDK_CURRENT_TIME,在Gdk.Event类中定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM