gpt4 book ai didi

c++ - 剪贴板选择传输不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:42 24 4
gpt4 key购买 nike

我一直在将我的系统移植到 X11,但我在复制剪贴板时遇到了问题(粘贴已经可以了)。我关注了this .过了一会儿,我注意到他的例子也不起作用。问题是当我尝试将它粘贴到某个地方时,而不是 XA_STRING 请求的原子 ID 是 434。我找不到这个原子的用途。当我将 XA_STRING 更改为 434 时,出现了不同的错误。以下是代码。

void SetClipboardText(const std::string &text) {
XSetSelectionOwner (display, XA_CLIPBOARD, windowhandle, CurrentTime);
copiedtext=text;
XFlush(display);
std::cout<<"COPIED"<<std::endl;
}

...
case SelectionRequest:
XEvent respond;

std::cout<<"SELTYPE: "<<event.xselectionrequest.target<<std::endl;

if(event.xselectionrequest.target==XA_STRING &&
event.xselectionrequest.selection==XA_CLIPBOARD) {
std::cout<<"REQUESTED"<<std::endl;

XChangeProperty (display,
event.xselectionrequest.requestor,
event.xselectionrequest.property,
XA_STRING,
copiedtext.length(),
PropModeReplace,
(unsigned char*) copiedtext.c_str(),
copiedtext.length()
);
respond.xselection.property=event.xselectionrequest.property;
}
else {
respond.xselection.property= None;
}
respond.xselection.type= SelectionNotify;
respond.xselection.display= event.xselectionrequest.display;
respond.xselection.requestor= event.xselectionrequest.requestor;
respond.xselection.selection=event.xselectionrequest.selection;
respond.xselection.target= event.xselectionrequest.target;
respond.xselection.time = event.xselectionrequest.time;
XSendEvent (display, event.xselectionrequest.requestor,0,0,&respond);
XFlush (display);
break;

我用 434 替换 XA_STRING 时出错:

X Error of failed request:  BadValue (integer parameter out of range for operation)
Major opcode of failed request: 18 (X_ChangeProperty)
Value in failed request: 0x4
Serial number of failed request: 33
Current serial number in output stream: 36

如果相关,我使用 KDE 4.8 并且 klipper 目前已关闭。

编辑:来自网站的示例。

#include <X11/Xlib.h> 
#include <X11/Xatom.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main()
{
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, 0, 0);
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XSelectionRequestEvent *req;
XEvent e, respond;
for(;;) {
XNextEvent(dpy, &e);
if (e.type == MapNotify) break;
}
XFlush(dpy);
//
Atom a1, a2, a3, type;
XSelectInput(dpy, w, StructureNotifyMask+ExposureMask);
int format, result;
unsigned long len, bytes_left, dummy;
unsigned char *data;
Window Sown;
for (int ii = 0; ii < 50; ii++) {
XSetSelectionOwner (dpy, XA_PRIMARY, w, CurrentTime);
XFlush (dpy);
XNextEvent (dpy, &e);
if (e.type == SelectionRequest)
//
// Somebody wants our data
//
{
req=&(e.xselectionrequest);
printf ("Selection Request from Mr %i I am %i\n",
(int)e.xselection.requestor, (int)w);
printf ("prop:%i tar:%i sel:%i\n", req->property,
req->target, req->selection);
if (req->target == XA_STRING)
{
XChangeProperty (dpy,
req->requestor,
req->property,
XA_STRING,
8,
PropModeReplace,
(unsigned char*) "It Works",
8);
respond.xselection.property=req->property;
}
else // Strings only please
{
printf ("No String %i\n",
(int)req->target);
respond.xselection.property= None;
}
respond.xselection.type= SelectionNotify;
respond.xselection.display= req->display;
respond.xselection.requestor= req->requestor;
respond.xselection.selection=req->selection;
respond.xselection.target= req->target;
respond.xselection.time = req->time;
XSendEvent (dpy, req->requestor,0,0,&respond);
XFlush (dpy);
}
}
}

编译使用

gcc copytest.c -o copytest -std=c99 -lX11

最佳答案

KDE 应用程序需要以下内容

else if(event.xselectionrequest.target==XA_TARGETS && 
event.xselectionrequest.selection==XA_CLIPBOARD) {
Atom supported[]={XA_STRING};
XChangeProperty (display,
event.xselectionrequest.requestor,
event.xselectionrequest.property,
XA_TARGETS,
8,
PropModeReplace,
(unsigned char *)(&supported),
sizeof(supported)
);
}

关于c++ - 剪贴板选择传输不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com