Converting keyboard layouts

Greetings to all.

I use this function to press a key in a certain window:

win32gui.PostMessage(self.hwnd, win32con.WM_KEYDOWN, WINDOW_ID, 0)

The problem is this:

I need to type Russian text, but the postMessage function only accepts English keys to press.

But, for example, if you turn on the Russian keyboard layout and click on Q via postMessage , then "Y"will be pressed.

How can I convert Russian keyboard characters to English? Type:

HI -> GHBDTN

Maybe there is some table for this?

Author: HWorld, 2020-02-11

1 answers

Here is a working way to translate from the Russian layout to the English one:

x = str(input()) #Само слово, если не подходит, то надо поменять под ваш вариант
a = {'Й':'Q','Ц':'W','У':'E','К':'R','Е':'T',
    'Н':'Y','Г':'U','Ш':'I','Щ':'O','З':'P',
    'Х':'{','Ъ':'}','Ф':'A','Ы':'S','В':'D',
    'А':'F','П':'G','Р':'H','О':'J','Л':'K',
    'Д':'L','Ж':':','Э':'"','Я':'Z','Ч':'X',
    'С':'C','М':'V','И':'B','Т':'N','Ь':'M',
    'Б':'<','Ю':'>','Ё':'~','й':'q','ц':'w',
    'у':'e','к':'r','е':'t','н':'y','г':'u',
    'ш':'i','щ':'o','з':'p','х':'[','ъ':']',
    'ф':'a','ы':'s','в':'d','а':'f','п':'g',
    'р':'h','о':'j','л':'k','д':'l','ж':';',
    'э':"'",'я':'z','ч':'x','с':'c','м':'v',
    'и':'b','т':'n','ь':'m','б':',','ю':'.',
    'ё':'`'} #Список со всеми значениями
res = '' #Создание пустой строки
j = 0 #Используется для выбора определённой буквы из x
while j < len(x): #Пока j < длинны строки x выполняется цикл
    if x[j] in a: #Если в списке a есть буква из x под номером j, выполняется перевод в английскую раскладку
        i = a[x[j]]
        res += i
        j+=1
    else: #Иначе добавляет пробел
        res+= ' '
print(res) #Выводит результат в консоль(если не подходит, то меняйте под свой случай)

The code may not be completely suitable for you, if you change something, but I have described the main essence of the code.

 0
Author: DGDays, 2020-02-11 15:40:39