gpt4 book ai didi

C++ 和 XGrabKeyboard 键盘锁定

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:21 25 4
gpt4 key购买 nike

我正在尝试编写小的 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/

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