gpt4 book ai didi

c++ - XGrabKeyboard 仅在程序运行时阻止键盘

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:31 26 4
gpt4 key购买 nike

我正在编写代码来获取按下的键并阻止键盘输入。示例:如果按下 Super_L 或 Super_R 键,则阻止它们打开应用程序菜单。该代码在程序打开时有效,但当程序关闭时, key 将发送到系统。示例:如果用户在程序运行时按下 Super_L 键没有任何反应,但是当程序关闭时,会自动打开 Ubuntu 应用程序菜单。我该如何解决这个问题?

void hook()
{
Display *dpy = XOpenDisplay(0);
XEvent ev;

if(!dpy)
{
qDebug() << "Error";
return;
}

XGrabKeyboard(dpy, DefaultRootWindow(dpy), false, GrabModeAsync, GrabModeAsync, CurrentTime);

forever
{
XNextEvent(dpy, &ev);
switch (ev.type)
{
case KeyPress:
qDebug() << "KeyPress" << XKeysymToString(XKeycodeToKeysym(dpy, ev.xkey.keycode, 0));
break;
case KeyRelease:
qDebug() << "KeyRelease" << XKeysymToString(XKeycodeToKeysym(dpy, ev.xkey.keycode, 0));
break;
}
}
}

最佳答案

Quot the manual :

When the X server's connection to a client is closed either by an explicit call to XCloseDisplay() or by a process that exits, the X server performs the following automatic operations:

  • It disowns all selections owned by the client (see XSetSelectionOwner()).
  • It performs an XUngrabPointer() and XUngrabKeyboard() if the client has actively grabbed the pointer or the keyboard.
  • It performs an XUngrabServer() if the client has grabbed the server.
  • It releases all passive grabs made by the client.

Grabs 由客户请求并授予客户。在抓取期间,所有相关事件都将传递给抓取客户端。没有客户,就没有抢。

我看不出这里有什么要解决的。如果您希望某项功能正常运行,请确保实现它的应用程序正在运行。

关于c++ - XGrabKeyboard 仅在程序运行时阻止键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675920/

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