获取鼠标按钮状态不阻塞linux

问题描述 投票:0回答:1

我正在尝试为游戏编写插件,我需要阅读鼠标按钮的状态,而不能阻止或“吃掉”事件。由于具有root用户访问权限,因此无法直接读取/ dev / input。我接近需要的是以下代码:

#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>


int main (int argc, char **argv)
{
Display *display;
XEvent ev;

display = XOpenDisplay(NULL);
Window window = RootWindow(display, XDefaultScreen(display));

XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask);
XGrabButton(display, Button1, AnyModifier, window, False, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);

while(1) {

XAllowEvents(display, ReplayPointer, CurrentTime);
    if(XCheckTypedEvent(display, ButtonPress ,&ev))
    {
        printf("Button pressed\n");
    }
    else if(XCheckTypedEvent(display, ButtonRelease ,&ev))
    {
        printf("Button relised\n");
    }

}

return 0;
}

问题是,通过设置GrabModeSync并将XAllowEvents与ReplayPointer结合使用,我可以传递事件,但只能得到ButtonPress。 ButtonRelease从未被捕获,但我需要它。 GrabModeAsync捕获两个事件,但“吃掉”点击。谁能提出解决方案?10x

c linux x11
1个回答
0
投票
#include <stdio.h> #include <string.h> #include <X11/Xlib.h> #include <X11/extensions/XInput2.h> static void select_events(Display *dpy, Window win) { XIEventMask evmasks[1]; unsigned char mask1[(XI_LASTEVENT + 7)/8]; memset(mask1, 0, sizeof(mask1)); /* select for button and key events from all master devices */ XISetMask(mask1, XI_RawButtonPress); XISetMask(mask1, XI_RawButtonRelease); evmasks[0].deviceid = XIAllMasterDevices; evmasks[0].mask_len = sizeof(mask1); evmasks[0].mask = mask1; XISelectEvents(dpy, win, evmasks, 1); XFlush(dpy); } int main(){ Display *dpy; XEvent ev; XIEvent *xi_event; XIRawEvent *xev; XGenericEventCookie *cookie = &ev.xcookie; dpy = XOpenDisplay(NULL); select_events(dpy, DefaultRootWindow(dpy)); int count = 0; while(1){ if (XCheckTypedEvent(dpy, GenericEvent ,&ev)){ if (cookie->type != GenericEvent || !XGetEventData(dpy, cookie)){ continue; } xi_event = (XIEvent *) cookie->data; xev = (XIRawEvent *) xi_event; switch (cookie->evtype) { case XI_RawButtonPress: printf("Button pressed %d %d\n", xev->detail, ++count); break; case XI_RawButtonRelease: printf("Button released %d %d\n", xev->detail, count); break; } XFreeEventData(dpy, cookie); } } return 1; }
© www.soinside.com 2019 - 2024. All rights reserved.