如何在 X11 中捕获原始鼠标输入?
对于我的应用程序的 Windows 版本,我使用 WM_INPUT 并直接注册鼠标设备以获得最精确的移动。我该如何在 X11 中做到这一点?
For the Windows version of my application, I'm using WM_INPUT and registering the mouse device directly to get the most precise movements. How would I go about doing this in X11?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有 11 个链接,提供参考、建议和两种不同的方法来在 Linux 上获取原始鼠标移动。 链接 #2 (Python) 显示了读取鼠标的传统方法设备和 #1 (C)< /a>、#3 (Python) 和 #4 (C) 显示事件方式。 链接 #5 介绍了 ps/2 协议。 链接 #6 是一个命令行示例。
这两种方法均通过从设备驱动程序文件获取 x 和 y 方向上的增量流来绕过 X (Xlib)。 Xlib 传统上只提供窗口(或屏幕)位置,而不是增量,因此如果您想继续获取特定方向的读数,则必须不断将指针从窗口边缘移动。 [请参阅链接 #8 了解快速信息关于许多 X 程序使用的“扭曲到中心”黑客的讨论。] 新的 XInput 2.0 扩展 (链接#10),但是,提供了用于获取增量的 X API(您可能必须至少链接 -lX11 和 -lXi)。
这两种设备方法的本质是这样的:
1:打开以读取“/dev/input/mouse0”设备或类似的设备(这取决于您的发行版命名以及您要使用的方法,/另一种方法为 dev/input/event4)。
2:从中读出。每这么多字节代表一次鼠标移动。例如,对于 ps/2 原始协议方法,例如,通过如上所述打开 mouse[n],如果您只关心 x、y 移动,您基本上只需要每 3 个字节序列的第 2 个和第 3 个字节。第二个字节代表 X。第三个字节代表 Y。这两个字节中的每一个都是一个 int8_t(有符号 8 位)数量。正X好像是在右边,没问题;然而,正 Y 似乎是向上的(就像在大多数数学教科书中一样),而在屏幕上,正 Y 通常被认为是从屏幕的左上角向下(相对于典型的从左下角作为原点向上)笛卡尔坐标系)。
其他注意事项:如果您运行的是 X,您可能需要使用 open/read 而不是 fopen/getc (请参阅 链接 #7 和其他链接中的代码)。如果您想要前 3 个按钮的鼠标按钮状态或想要确认 x 或 y 的符号,那么还要查看第一个字节。 [我怀疑溢出是普通“流”使用中的一个问题,但这些位也存在。]如果您在 Linux 命令行上进行实验,您可能会注意到,例如,快速“cat”到“less”,大多数动作仅 1 个单位(大量 ^A ^@ 和)。这可能取决于鼠标的配置方式以及鼠标的功能。 链接 #5 涵盖了简单的协议,甚至涵盖了按钮 4 和 的 Microsoft intellimouse 标准扩展5、鼠标滚轮。 链接 #11 涵盖了 ps/ 的控制器芯片/逻辑配置2 鼠标或键盘。它解释了如何通过该接口配置鼠标。有一个工具(链接#9,xdotool)可以模拟鼠标点击帮助自动化窗口任务。最后,您可能需要成为 root 才能直接从这些设备读取(取决于您的发行版)。
Here are eleven links that provide reference, advice, and two different approaches to get raw mouse movements on Linux. Link #2 (Python) shows the traditional way to read the mouse device and #1 (C), #3 (Python), and #4 (C) show the events way. The ps/2 protocol is covered in link #5. Link #6 is a command line example.
Each of the two approaches bypasses X (Xlib) by getting from a device driver file the stream of deltas in the x and y directions. Rather than deltas, Xlib traditionally provides just the window (or screen) position so you must keep moving the pointer from the edge of the window if you want to keep getting readings in a particular direction. [See link #8 for a quick discussion on the "warping to center" hack used by many programs with X.] The new XInput 2.0 extension (link #10), however, offers an X API for getting the deltas (you may have to link at least -lX11 and -lXi).
The essence of either of the two device approaches is this:
1: Open for reading something like the "/dev/input/mouse0" device or something similar (it depends on your distro naming and also on which method you want to use, /dev/input/event4 for the other approach).
2: Read from it. Every so many bytes represents a mouse movement. For example, for the ps/2 raw protocol method, eg, by opening mouse[n] as above, you basically want just the 2nd and 3rd bytes of every 3 byte sequence if you just care about x, y movement. The second byte represents X. The third represents Y. Each of the two bytes is a single int8_t (signed 8 bit) quantity. Positive X seems to be to the right, no problem; however, positive Y seems to be upward (as in most math text books) while on a screen positive Y is usually considered going downward from the upper left corner of the screen (vs. going up from the lower left as the origin in a typical Cartesian coordinate system).
Other notes: You may want to use open/read rather than fopen/getc if you are running X (see link #7 and code in other links). If you want mouse button state for the first 3 buttons or want to confirm the signedness of x or y, then look at the first byte as well. [I doubt overflow is an issue in ordinary "stream" use, but those bits are there as well.] You may notice if you experiment on the Linux command line, eg, a quick "cat" to "less", that most movements are just of 1 unit (lots of ^A ^@ and <FF>). This may depend on how the mouse is configured and on how capable is the mouse. Link #5 covers the simple protocol and even covers the Microsoft intellimouse standard extension for buttons 4 and 5 and the mouse wheel. Link #11 covers the controller chips/logic configuration for ps/2 mouse or keyboard. It explains how the mouse is configured by this interface. There is a tool (link #9, xdotool) that simulates mouse clicks to help automate windowing tasks. Finally, you may need to be root to read directly from these devices (depends on your distro).
有多种方法可以做到这一点,具体取决于您使用的框架。
如果您打算使用“Xlib”(X11 编程的最基本方法)来执行此操作,您应该查看 Xlib手册,特别关注XInput。
可以使用 XI2 (XInput 版本 2)检索更详细的信息
其他更高级别的框架可以使事情变得简单更轻松。看一下 Qt。它是我见过的最好的 GUI API 之一。
如果您需要更多,请告诉我们。
There are several ways to do this, depending on the framework you are using.
If you intend to do this with "Xlib", the most basic way to program for X11, you should take a look at Xlib manual, with special attention to XInput.
More elaborated information can be retrieved by using XI2 (XInput version 2)
Other higher level frameworks can make things easier. Take a look at Qt. It's one of the best GUI API's I've ever seen.
Tell us if you need more.