views:

294

answers:

2

Hello,

I want to be able to translate a string to keycode to write it with Xlib (to simulate user action on linux). The keycode are not the ascii but the code you get when you do use xev on

linuxKeyPress event, serial 33, synthetic NO, window 0x6400001,
    root 0x13c, subw 0x0, time 51212100, (259,9), root:(262,81),
    state 0x0, keycode 24 (keysym 0x61, a), same_screen YES,
    XLookupString gives 1 bytes: (61) "a"
    XmbLookupString gives 1 bytes: (61) "a"
    XFilterEvent returns: False

for exemple the keycode for 'a' is 24

I can easly detect if the letter is upercase and then make a combination ALT+lowercase(letter) but I don't know how to get the keycode.

One solution would be to be a list of every combination (a=24, b=56, c=54,...) but would be better if there is a function.

I'm using an azerty keyboard. Is the keycode for the same letter different on an qwerty keyboard ?

thank you

+2  A: 

The keycodes depend not only on the keyboard hardware, but also on the user's preference for keyboard layout -- a user may use a dvorak layout on a qwerty keyboard, for example.

The best solution would probably be to use python-xlib to find out the information per the user's keyboard preferences. I don't know the details on how to do that.

A crude solution would be to run xmodmap -pke and parse the output.

unutbu
thank you for the information but I've found a piece of code doing it (cf my answer)
Martin Trigaux
+3  A: 

I've found this code which is doing exactly what I wanted.

It uses the function display.keysym_to_keycode(Xlib.XK.string_to_keysym(char))

Martin Trigaux
Cool. Thanks for posting back with the solution.
unutbu