X11 等待并获取剪贴板文本 [英] X11 Wait for and Get Clipboard Text

查看:52
本文介绍了X11 等待并获取剪贴板文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须监控 X11 剪贴板.

I have to monitor the X11 Clipboard.

目前,我每 5 秒请求一次剪贴板选择,然后对从剪贴板返回的文本进行散列,并将其与上次检查的散列计算结果进行比较.如果哈希不相同,我会分析文本内容并做一些事情...

For the moment, I request the ClipBoard Selection each 5 seconds, then I hash the text returned from clipboard and I compare it with the hash calculate from the last check. If hash are not the same, I analysis the text content and do some stuff...

我不喜欢我的方法.我来自 Windows,使用 winapi,当剪贴板发生变化时,内核会通知您的程序,而且效率更高!

I don't like my method. I'm from Windows, and with the winapi, it is the kernel that notify your program when the clipboard has changed, and it's more efficient!

我只想知道当剪贴板发生变化时,X11 是否可以将您的程序通知为 winapi?使用 X11 检查剪贴板修改的更有效方法是什么?

I just want to know if it is possible that X11 can notify your program as winapi when the clipboard has changed ? What is the more efficient way to check clipboard modifications with X11 ?

推荐答案

使用 Xfixes 扩展中的 XFixesSelectSelectionInput() 并等待 XFixesSelectionNotify 事件.

Use XFixesSelectSelectionInput() from Xfixes extension and wait for XFixesSelectionNotify event.

示例:

// gcc -o xclipwatch xclipwatch.c -lX11 -lXfixes
...
#include <X11/extensions/Xfixes.h>
...
void WatchSelection(Display *display, Window window, const char *bufname)
{
  int event_base, error_base;
  XEvent event;
  Atom bufid = XInternAtom(display, bufname, False);

  assert( XFixesQueryExtension(display, &event_base, &error_base) );
  XFixesSelectSelectionInput(display, DefaultRootWindow(display), bufid, XFixesSetSelectionOwnerNotifyMask);

  while (True)
  {
    XNextEvent(display, &event);

    if (event.type == event_base + XFixesSelectionNotify &&
        ((XFixesSelectionNotifyEvent*)&event)->selection == bufid)
    {
      if (!PrintSelection(display, window, bufname, "UTF8_STRING"))
        PrintSelection(display, window, bufname, "STRING");

      fflush(stdout);
    }
  }
}
...

这适用于 bufname == "CLIPBOARD"bufname == "PRIMARY" 选择.

This works both for bufname == "CLIPBOARD" and bufname == "PRIMARY" selection.

另请参阅本答案中的 PrintSelection() 函数.

Also see PrintSelection() function in this answer.

这篇关于X11 等待并获取剪贴板文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆