Tuesday, December 1, 2009

Dish Washing. At Red Lobster

Gexto python-xlib: keyboard emulation

Salut!

post in the past half stayed. We saw how to do some things with the mouse, but did nothing with the keyboard. Try it now.

With the same function, fake_input () , you can send events keyboard. Change the ButtonPress / ButtonRelease by KeyPress / KeyRelease , and as a parameter, the code of the key you want. I will here the problem. What is the "code" and where we take it out?

would be interesting to have information about this, but this is quite rare. So we have to see the source code or the examples we find. Not that there are many ... but some if it is. In my search I found xautomation , a tool I can get many things: p

Viewing your code, you can deduce that fake_input function () must pass a Keycode , which in turn, is derived from a keysym . Two new concepts that will be defined. Apparently, keycodes are in a fairly small range (no less than 255), so I gather they have a match (almost) directly with a key on the keyboard (this, perhaps not, but let's assume that yes). There is a class method Display which makes the conversion between a keysym and keycode . And in the form XK there are plenty of keysyms defined (eg XK_colon, XK_plus or XK_space) .

infer that, on one hand we a number associated with the character you want to push, and secondly, the key that corresponds to the current keyboard layout (Spain, USA, etc). For example if I enter the character '.', First I have to identify your keysym. Apparently, it's 46 or XK_period (and also corresponds to the ASCII value for the point). Now we must translate this keysym the value of the key, using Display.keysym_to_keycode () . And the return value is that we can use in fake_input () .

I guess all this fuss is desirable to support multiple keyboard layouts and to different character sets. An example, the classic "Hello World!" Which has a bit of everything.

 Xlib.display import from 
Display Xlib.ext import from XTest
from Xlib import X, XK

d = Display ()
msg = "Hello World!"

for c in msg: keysym = XK.string_to_keysym
(c) keycode = d.keysym_to_keycode
(keysym)

xtest.fake_input (d, X. KeyPress, keycode)
xtest.fake_input (d, X. KeyRelease, keycode)

d.flush ()

I used a function of XK makes the translation from a string to a keysym (for example, makes the string "space" to keysym XK_space ). We tried it but does not show the expected result, but 'helloworld' What happened to the space, the capitalization and exclamation point? Well, let's go by parts. It's normal not be displayed because the case really are not the push of a button, but two (the 'Caps Lock' can be considered as the repeated use of 'Shift'). We will have to be taken into account. The same goes with admiration. The case of space is different. String_to_keysym function () recognizes only strings that have a direct correspondence with an attribute of the module XK. That is what we need is the keysym name without the prefix XK_ . In this case, will have to "translate" this space, '', the string 'space'. Making the necessary changes, we have:

 Xlib.display import from 
Display Xlib.ext from XTest
from Xlib import X import, XK

d = Display ()
msg = "Hello World!"
shift = d.keysym_to_keycode (XK.XK_Shift_L)

for c in msg:
if c == '': c = "space"
elif c == '!': C = "exclaimed"
keysym = XK. string_to_keysym (c) keycode = d.keysym_to_keycode
(keysym)

if c in ["H", "W", "exclaimed"]:
xtest.fake_input (d, X. KeyPress, shift)

xtest.fake_input (d, X. KeyPress, keycode)
xtest.fake_input (d, X. KeyRelease, keycode)

if c in ["H" "W", "exclaimed"]:
xtest.fake_input (d, X. KeyRelease, shift)

d.flush ()

We are a little more complicated code. As you can see, the strategy is to press the shift key before each character 'special'. Furthermore, it is necessary to convert the symbols to valid names for XK. Within Gexto , do all these conversions, to enter any text as simple as possible. I hope

boring not having a lot:) See you at the next entry.
Happy hacking!

Au \u200b\u200brevoir!

0 comments:

Post a Comment