Nĭmen hao!
To conduct described in post above, I thought of using the implementation of Xlib in Python, called python-xlib . Although there is continued development, I think I will for what I do. The fact that it is written entirely in Python is an advantage to me to snoop your code;) In addition, it also covers several extensions. In particular, interests me is XTEST, which has some mechanisms to handle the input.
Let's do some tests. To follow, you must have installed the package python-xlib. This depends on your distribution. For example, to Debian (and derivatives like Ubuntu ), the package is in the official repositories, so a simple
# apt-get install python-xlibsolves it.
Let's start easy, let's try to move the mouse ... without touching: D. Python open a console (I like ipython ), or write to a file and run it as you like. First I put the code, then we are doing.
Xlib.display import from
Display
d = Display ()
d.screen r = (). Root
for i in range (200):
r.warp_pointer (i, i)
d.sync ()
Simple, right? Desgranemoslo. First, regardless of class Display . Xlib is a display for a workstation, ie a monitor, keyboard and mouse (this is very simplistic, because there may be many different combinations, for example, two or more monitors, or a tablet, etc.). . We note that X is a server, and we act as clients. When creating a display , really we do is connect to the server.
From this server, get the screen, a complex subject that interests us prince Window root, root . In this window you can say you move the mouse, warp_pointer () . Until we sync () in Display , no actions are sent to the server, and therefore it is as if we did not do anything. To enhance the effect, rather than move to a point, what we do is move around a line. For that is the loop.
If you run, you should see that the mouse is moved to the upper left corner (the point (0.0) of the root window), and travels 200 pixels in x and y axis, in steps of 1 pixel . Click
is a bit more complex. To this end, need the XTEST extension. We will use a feature called fake_input () , which simulates a keystroke, both mouse and the keyboard. Consider the code to press the right mouse button:
Xlib.ext import from XTest
from Xlib import X
xtest.fake_input (d, X. ButtonPress, 3)
xtest.fake_input (d, X. ButtonRelease, 3)
d.sync ()
import the relevant modules: XTest and X (for events that will generate). As you can see, it uses the same display, 'd', we had created earlier (the same connection). Indicate which event you want to send to the server, in this case first a ButtonPress and then a ButtonRelease , and data related to the event, here is the number of button we press. When you back a sync () , it sends all the X server, and depending on where the mouse pointer, you should see a contextual menu, or something: D
Do you want to test the keyboard? Well, I think that will be another post, because it is a bit messy: P.
Zaijian!
0 comments:
Post a Comment