问题
As far as i know the two ways to get mouse position are using libgpm
or reading /dev/input/mice
file. But the latter just returns a relative position from the last position. So my question is how can i get the absolute mouse position though reading /dev/input/mice
or other way.
And i want to implement this function by C
or C++
. Any information will be appreciate.
回答1:
First, a mouse device is probably sending only relative movements, so there is no way to get the absolute position (just try to raise the mouse with your hand and put it elsewhere), except by integrating the movement.
And almost all Linux GUI environments are above X11, so it is the X11 server (usually the Xorg
process) which deals with the mouse (it is the only process actually reading /dev/input/mice
)
You'll then need to make an X11 client application. See this & that question. But you'll be much better in using some existing toolkit library, like Qt or GTK; see e.g. QMouseEvent & QWidget::mouseMoveEvent in Qt, and GtkWidget "motion-notify-event" signal in Gtk (and many other functions).
See also this question
来源:https://stackoverflow.com/questions/14556305/how-can-i-get-the-absolute-mouse-position-in-linux-in-c