如何在 GTK 中使用剪贴板? [英] How do I use clipboard in GTK?
问题描述
我如何使用 GtkClipboard 读取和写入剪贴板?例如,请告诉我如何获取当前剪贴板内容并将其打印到控制台.
How can I, using GtkClipboard, read and write to a clipboard? As an example, please show me how to get the current clipboard content and print it to console.
我试过这个来获取和打印剪贴板中当前的内容,但它不起作用:
I tried this to get and print what is currently in the clipboard, but it doesn't work:
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gtk_clipboard_request_text(clip, (GtkClipboardTextReceivedFunc)print_clip, NULL);
一切都在编译时没有任何警告,但永远不会到达 print_clip()
函数.也许我应该使用另一个函数,比如 gtk_clipboard_wait_for_text()
?请帮帮我,我该怎么办?
Everything compiles without any warnings, but print_clip()
function is never reached. Maybe I should use another function, like gtk_clipboard_wait_for_text()
? Please help me, what am I supposed to do?
如果重要的话,我使用 Linux/X11.另外,我使用 GTK+3,而不是 GTK+2 或其他版本.<小时>好的,我有一个工作示例:
I use Linux/X11, if it matters. Also, I use GTK+3, not GTK+2 or some other release.
Ok, I've got a working example:
#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()
,因为这会关闭应用程序.
The only thing that I need now is to somehow exit clipboard_callback()
without calling gtk_main_quit()
, since that closes an application.
推荐答案
真正应该使用 gtk_clipboard_wait_for_text()
而不是 gtk_clipboard_request_text()
.
One should really use gtk_clipboard_wait_for_text()
instead of gtk_clipboard_request_text()
.
例如,应该这样做:
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gchar *text = gtk_clipboard_wait_for_text(clip);
这篇关于如何在 GTK 中使用剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!