Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char to keycode in python

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

like image 803
Martin Trigaux Avatar asked Nov 19 '25 09:11

Martin Trigaux


1 Answers

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))

like image 118
Martin Trigaux Avatar answered Nov 22 '25 00:11

Martin Trigaux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!