- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写小的 C++ 程序,它应该检测用户是否按下键盘上的任意键或让鼠标移动。我需要在 Ubuntu 或 Centos 上运行的那个程序。这就是我使用 X11 库进行按键检测的原因。
这是我用谷歌搜索的代码:
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main(void)
{
Display * dpy = XOpenDisplay(0x0);
XEvent ev;
if(!dpy) return 1;
Time t = CurrentTime;
XGrabKeyboard(dpy, DefaultRootWindow(dpy), false,
GrabModeAsync, GrabModeAsync,t);
for(;;)
{
//XGrabKeyboard(dpy, DefaultRootWindow(dpy), false,
// GrabModeAsync, GrabModeAsync,t);
XNextEvent(dpy, &ev);
if(ev.type == KeyPress)
cout << "Key pressed" << endl;
// XGrabKeyboard(dpy, DefaultRootWindow(dpy), false,
// GrabModeAsync, GrabModeAsync,t);
}
}
效果不错,但不适合我。它在所有窗口中锁定键盘输入,除了它自己的程序(取消注释循环中的第一行和最后一行给了我相同的结果)。
也许有人知道我该如何修复它或我可以改用什么库。
谢谢。
最佳答案
XSendEvent() 对我有帮助。参见 http://tronche.com/gui/x/xlib/event-handling/XSendEvent.html
switch(ev.type)
{
case KeyPress:
XSendEvent(display,InputFocus,False,KeyPressMask,&ev);
break;
case KeyRelease:
XSendEvent(display,InputFocus,True,KeyReleaseMask,&ev);
break;
case ButtonPress:
XSendEvent(display,PointerWindow,True,ButtonPressMask,&ev);
break;
case ButtonRelease:
XSendEvent(display,PointerWindow,True,ButtonPressMask,&ev);
break;
default:
break;
}
关于C++ 和 XGrabKeyboard 键盘锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13570880/
我正在开发一个应用程序,它应该在一段时间内卡住所有输入,包括键盘和鼠标。我试过使用 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 我发现我可以抓取某些键,然后将事件发送到
我是一名优秀的程序员,十分优秀!