如何让gtk控制被处理,或者不被处理?

发布于 2024-09-14 00:11:23 字数 308 浏览 4 评论 0原文

我正在使用 pygtk,并且有时想处理 control+c 来执行特殊的复制操作,但其他时候则让 gtk 处理它。例如,我想将一个对象放在剪贴板上(如果可用),或者只是让 control+c 在文本输入中以正常方式使用。

目前,我有一个与“c”关联的 ActionGroup,但即使我返回 False,它也总是会占用击键。如果我删除 ActionGroup,它始终在文本区域中起作用。如果我添加 ActionGroup,它总是会处理它,并且复制在文本区域中不起作用。

让 control+c 出现在菜单中,有时处理击键,但其他时候,让它落在文本小部件上的正确方式是什么?

I'm using pygtk, and would like to handle control+c sometimes to do a special copy action, but other times to let gtk handle it. For example, I'd like to put an object on my clipboard if it is available, or just let control+c be used in the normal fashion in a text entry.

Currently I have an ActionGroup associated with "c" but that always eats the keystroke, even if I return False. If I remove the ActionGroup, it always works in the text areas. If I add the ActionGroup, it always handles it, and copy doesn't work in the text areas.

What is the proper manner to have control+c appear in the menu, handle the keystroke sometimes, but other times, let it fall to a text widget?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2024-09-21 00:11:23

我不知道这是否是“正确”的方式,但我是这样做的。我将应用程序窗口作为用户数据参数传递给操作回调。然后我找出窗口中聚焦的小部件,然后将复制命令传递给该小部件(如果这样做有意义的话)(即聚焦的小部件是像您所说的文本条目)。如果这没有意义,那么我从窗口的“默认”文本视图进行复制。

void
action_copy(GtkAction *action, gpointer user_data)
{
    GtkWidget *widget = gtk_window_get_focus(GTK_WINDOW(user_data));

    /* What actually happens depends on the type of widget that is focused */
    if(WEBKIT_IS_WEB_VIEW(widget))
        webkit_web_view_copy_clipboard(WEBKIT_WEB_VIEW(widget));
    else if((GTK_IS_LABEL(widget) && gtk_label_get_selectable(GTK_LABEL(widget)))
        || GTK_IS_ENTRY(widget) || GTK_IS_TEXT_VIEW(widget))
        g_signal_emit_by_name(widget, "copy-clipboard", NULL);
    else
        g_signal_emit_by_name(/* ...default text view... */, "copy-clipboard", NULL);
}

(获取默认文本视图实际上是通过在我的应用程序类上调用 get_default_view() 方法来完成的,该应用程序类是 GtkWindow 的子类;但我不想复杂化这里很重要。)

I don't know if this is the "proper" way, but here is how I do it. I pass the application window as the user data parameter to the action callback. Then I find out which widget is focused in the window, and I pass the copy command on to that widget if that makes sense to do (i.e. the focused widget is a text entry like you say). If that doesn't make sense, then I copy from the window's 'default' text view.

void
action_copy(GtkAction *action, gpointer user_data)
{
    GtkWidget *widget = gtk_window_get_focus(GTK_WINDOW(user_data));

    /* What actually happens depends on the type of widget that is focused */
    if(WEBKIT_IS_WEB_VIEW(widget))
        webkit_web_view_copy_clipboard(WEBKIT_WEB_VIEW(widget));
    else if((GTK_IS_LABEL(widget) && gtk_label_get_selectable(GTK_LABEL(widget)))
        || GTK_IS_ENTRY(widget) || GTK_IS_TEXT_VIEW(widget))
        g_signal_emit_by_name(widget, "copy-clipboard", NULL);
    else
        g_signal_emit_by_name(/* ...default text view... */, "copy-clipboard", NULL);
}

(Obtaining the default text view is actually done by calling a get_default_view() method on my application class, which is a subclass of GtkWindow; but I didn't want to complicate matters here.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文