- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写代码来获取按下的键并阻止键盘输入。示例:如果按下 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()
andXUngrabKeyboard()
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/
我正在开发一个应用程序,它应该在一段时间内卡住所有输入,包括键盘和鼠标。我试过使用 XGrabKeyboard,但我无法使用 XUngrabKeyboard 恢复它的效果,它什么都不做。 这是一个您可
我正在编写代码来获取按下的键并阻止键盘输入。示例:如果按下 Super_L 或 Super_R 键,则阻止它们打开应用程序菜单。该代码在程序打开时有效,但当程序关闭时, key 将发送到系统。示例:如
考虑一个应用程序,它希望在获得焦点时捕获键盘,以便捕获所有窗口管理器命令(Alt+F4 等)进行处理。现在,这有一个缺点,即用户在捕获键盘时无法通过键盘切换到另一个应用程序或虚拟桌面。我想要一个用户定
我正在尝试编写小的 C++ 程序,它应该检测用户是否按下键盘上的任意键或让鼠标移动。我需要在 Ubuntu 或 Centos 上运行的那个程序。这就是我使用 X11 库进行按键检测的原因。 这是我用谷
我正在编写一个需要监听用户键盘敲击的程序。 我使用 XGrabKeyboard() 函数,这是我的代码: XGrabKeyboard(pDisplay, DefaultRootWindow(pDisp
调用XGrabKeyboard()后,我的应用程序捕获并显示所有按键/释放,包括 GUI 和 PrintScreen 键。 但是,用户不再能够移动应用程序的窗口。到目前为止,这发生在 Fedora 1
目标 我想根据按键将键盘输入重定向到不同的窗口。不是所有的键都会被重定向到同一个窗口,我不能使用 XSetInputFocus。 第一次尝试:XGrabKey 我发现我可以抓取某些键,然后将事件发送到
我是一名优秀的程序员,十分优秀!