void Callback (XPointer, XRecordInterceptData *pRecord) { std::cout << "my logsn"; }
int main ()
{
if(auto* const pDisplay = XOpenDisplay(nullptr))
{
XRecordClientSpec clients = XRecordAllClients;
auto* pRange = ::XRecordAllocRange();
pRange->device_events = XRecordRange8{KeyPress, ButtonRelease};
auto context = ::XRecordCreateContext(pDisplay, 0, &clients, 1, &pRange, 1);
::XRecordEnableContextAsync(pDisplay, context, Callback, nullptr); // use with/without `...Async()`
::XRecordDisableContext(pDisplay, context);
::XRecordFreeContext(pDisplay, context);
::XFree(pRange);
::XFlush(pDisplay);
::XSync(pDisplay, true);
}
}
我注意到即使在XRecordDisableContext()
之后,Callback()
继续被调用。
我们如何禁用记录,使回调不再被调用?
注意:
- 以本网站为例。 不知道如何使用
XRecordEnableContext()
,所以使用XRecordEnableContextAsync()
。这就是问题的根源吗?一种方法是将下面的语句移到Callback()
或一些等效的其他线程中。出于测试目的,我将代码更改为如下所示,在几个事件引发后,我禁用Callback()
并且它有效。
::Display* pDisplay;
XRecordRange* pRange;
XRecordContext context;
#define CHECK(EVENT) if(*pDatum == EVENT) qDebug() << #EVENT
void Handle (XPointer, XRecordInterceptData *pRecord)
{
std::cout << "my logsn";
static int i = 0;
if(++i < 10)
return;
::XRecordDisableContext(pDisplay, context);
::XRecordFreeContext(pDisplay, context);
::XFree(pRange);
::XFlush(pDisplay);
::XSync(pDisplay, true);
}
// other code same, except 3 variables are global and "Free"-up functions are not required