Xlib 鼠标事件和 ButtonPressMask

发布于 2024-08-12 05:27:20 字数 931 浏览 6 评论 0原文

我编写了一个简单的程序,它将报告特定窗口的按键和释放事件。就我而言,它主要是终端,因为我从终端调用程序。我能够获取终端窗口中发生的按键和释放事件(我在终端上将 XSelectInput() 与 KeyPressMask 和 KeyReleaseMask 一起使用),但同样不适用于 ButtonPress 和 ButtonRelease。不仅是这些,与鼠标相关的任何事件都不会被报告。知道为什么会发生这种情况吗?

#include
#include
#include
#include
#include
#include

int main() {
Display *display = XOpenDisplay(NULL);
KeySym k;
int revert_to;
Window window;
XEvent event;

XGetInputFocus(display, &window, &revert_to);
XSelectInput(display, window, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);

while(1)
{
XNextEvent(display,&event);
  switch (event.type) {

  case KeyPress : printf("Key Pressed\n"); break;
  case KeyRelease : printf("Key Released\n"); break;
  case ButtonPress : printf("Button Pressed\n"); break;
  case ButtonRelease : printf("Button Released\n"); break;
  case EnterNotify : printf("Enter\n"); break;
  }
}
XCloseDisplay(display);
return 0;
}

I have written a simple program which will report key press and release events for a particular window. In my case, it is mostly the terminal since I invoke the program from the terminal. I am able to get the key press and release events taking place in the terminal window (I have used XSelectInput() with KeyPressMask and KeyReleaseMask on the terminal) but the same is not working with ButtonPress and ButtonRelease. Not just these, but any events related to the mouse are not being reported. Any idea why this is happening?

#include
#include
#include
#include
#include
#include

int main() {
Display *display = XOpenDisplay(NULL);
KeySym k;
int revert_to;
Window window;
XEvent event;

XGetInputFocus(display, &window, &revert_to);
XSelectInput(display, window, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);

while(1)
{
XNextEvent(display,&event);
  switch (event.type) {

  case KeyPress : printf("Key Pressed\n"); break;
  case KeyRelease : printf("Key Released\n"); break;
  case ButtonPress : printf("Button Pressed\n"); break;
  case ButtonRelease : printf("Button Released\n"); break;
  case EnterNotify : printf("Enter\n"); break;
  }
}
XCloseDisplay(display);
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-08-19 05:27:20

您遇到的问题是 Xlib 仅向一个客户端发送 ButtonPress/Release 事件。我认为您正在使用的窗口已经有一个正在侦听其鼠标事件的客户端。因此,您的 SelectInput 调用实际上并未设置 ButtonPress/Release 掩码,并生成了您未检查的错误。

The problem you encounter is that Xlib sends ButtonPress/Release events to only one client. I think that the window you're working with already has a client which is listening to its mouse events. Therefore your SelectInput call did not actually set ButtonPress/Release masks and generated an error which you didn't check for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文