如何在GTK中使用剪贴板?

时间:2018-09-06 13:15:00

标签: c gtk clipboard x11 gtk3

我如何使用GtkClipboard来读写剪贴板?例如,请向我展示如何获取当前剪贴板内容并将其打印到控制台。

我试图这样做来获取并打印剪贴板中当前的内容,但是它不起作用:

GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gtk_clipboard_request_text(clip, (GtkClipboardTextReceivedFunc)print_clip, NULL);

所有编译都不会发出任何警告,但是永远不会达到print_clip()功能。也许我应该使用另一个功能,例如gtk_clipboard_wait_for_text()?请帮助我,我该怎么办?

如果重要的话,我使用Linux / X11。另外,我使用的是GTK + 3,而不是GTK + 2或其他版本。


好的,我有一个有效的示例:

#include <gtk/gtk.h>

void clipboard_callback(GtkClipboard *clip, const gchar *text, gpointer data)
{
        g_print("Now we're in clipboard_callback function.\n");
        gtk_main_quit();
}

int main(int argc, char **argv)
{
        gtk_init(&argc, &argv);
        GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
        gtk_clipboard_request_text(clip, clipboard_callback, NULL);
        gtk_main();
        return 0;
}

我现在唯一需要做的就是以某种方式退出clipboard_callback()而不调用gtk_main_quit(),因为这将关闭应用程序。

2 个答案:

答案 0 :(得分:1)

您可以通过this链接使用以下代码。

   #include <gdk/gdk.h> 
   #include <gtk/gtk.h>

    // This callback is invoked when the clipboard contents have been
    // received. The text contents of the clipboard will be in UTF-8 in
    // the `text` parameter.
    void text_request_callback(GtkClipboard *clipboard,
                               const gchar *text,
                               gpointer data)
    {
        // To demonstrate setting a new value into the clipboard, we
        // choose some text.
        const gchar* new_clipboard_text = "Clipboard test 1";
        if(text == 0 || g_utf8_collate(text, new_clipboard_text) == 0)
        {
            // If the program was already run, and the clipboard contains
            // our string already, use a different string. This way when
            // you run the program multiple times in a row, you'll see the
            // string changing.
            new_clipboard_text = "Clipboard test 2";
        }

        // This sets the text. I'm passing -1 because I'm lazy -- the
        // function will call strlen on the string to figure out how long
        // it is.
        gtk_clipboard_set_text(clipboard, new_clipboard_text, -1);

        // Display the content and the contents of the variable we passed
        // in.
        printf("Clipboard text was %s, value is %8X\n",
               text, *(int*)data);

        // Now that we've monkeyed with the clipboard, our job is done
        // here.
        gtk_main_quit();
    }

    int main(int argc, char** argv)
    {
        // Standard boilerplate: initialize the toolkit.
        gtk_init(&argc, &argv);

        // Get a handle to the given clipboard. You can also ask for
        // GDK_SELECTION_PRIMARY (the X "primary selection") or
        // GDK_SELECTION_SECONDARY.
        GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);

        // This is just an arbitrary value to pass through to the
        // callback. You could pass a pointer to a local struct or
        // something similar if that was useful to you.
        int value = 0xDECAFBAD;

        // There are more complex things you can place in the clipboard,
        // but this demonstrates text. The callback will be invoked when
        // the clipboard contents has been received.
        //
        // For a much simpler method of getting the text in the clipboard,
        // see gtk_clipboard_wait_for_text(), which is used in the example
        // program clipboard_watch.
        gtk_clipboard_request_text(clipboard, text_request_callback, &value);

        // We have to run the GTK main loop so that the events required to
        // fetch the clipboard contents can be processed.
        gtk_main();

        return 0;
    }

答案 1 :(得分:0)

一个人应该真正使用gtk_clipboard_wait_for_text()而不是gtk_clipboard_request_text()

例如,这是应该这样做的方式:

GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gchar *text = gtk_clipboard_wait_for_text(clip);