X11等待并获取剪贴板文本

时间:2021-04-08 08:58:48

I have to monitor the X11 Clipboard.

我必须监控X11剪贴板。

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...

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

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!

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

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 ?

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

3 个解决方案

#1


4  

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

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

Example:

// 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);
    }
  }
}
...

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

这适用于bufname ==“CLIPBOARD”和bufname ==“PRIMARY”选择。

Also see PrintSelection() function in this answer.

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

#2


2  

  1. Find window with selection using GetSelectionOwner (PRIMARY and CLIPBOARD)
  2. 使用GetSelectionOwner(PRIMARY和CLIPBOARD)查找选择窗口

  3. get copy of selection by sending SelectionRequest, notify your application
  4. 通过发送SelectionRequest获取选择的副本,通知您的应用程序

  5. watch for SelectionClear event
  6. 观察SelectionClear事件

  7. update window with selection using id from SelectionClear event, goto step 2
  8. 使用SelectionClear事件中的id选择更新窗口,转到步骤2

#3


0  

Here's a good reference: http://www.jwz.org/doc/x-cut-and-paste.html

这是一个很好的参考:http://www.jwz.org/doc/x-cut-and-paste.html

#1


4  

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

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

Example:

// 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);
    }
  }
}
...

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

这适用于bufname ==“CLIPBOARD”和bufname ==“PRIMARY”选择。

Also see PrintSelection() function in this answer.

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

#2


2  

  1. Find window with selection using GetSelectionOwner (PRIMARY and CLIPBOARD)
  2. 使用GetSelectionOwner(PRIMARY和CLIPBOARD)查找选择窗口

  3. get copy of selection by sending SelectionRequest, notify your application
  4. 通过发送SelectionRequest获取选择的副本,通知您的应用程序

  5. watch for SelectionClear event
  6. 观察SelectionClear事件

  7. update window with selection using id from SelectionClear event, goto step 2
  8. 使用SelectionClear事件中的id选择更新窗口,转到步骤2

#3


0  

Here's a good reference: http://www.jwz.org/doc/x-cut-and-paste.html

这是一个很好的参考:http://www.jwz.org/doc/x-cut-and-paste.html