Advertisement
viking_unet

pyautogui pass Cyrillic text

Jul 25th, 2020 (edited)
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import ctypes
  2. import time
  3.  
  4. import pyautogui
  5.  
  6.  
  7. """
  8. script for typing russian keys from pyautogui
  9. ideas was looked:
  10. translate converter: https://github.com/asweigart/pyautogui/issues/137
  11. change keyboard ctype: https://www.cyberforum.ru/post12039342.html
  12. """
  13.  
  14.  
  15. qwerty = """qwertyuiop[]asdfghjkl;'zxcvbnm,.?/QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>"""
  16. ycuken = """йцукенгшщзхъфывапролджэячсмитьбю,.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ"""
  17. tr = dict(zip(ycuken, qwerty))
  18. def translate(text):
  19.     """Returns qwerty text or the given text itself if no mapping found"""
  20.     return "".join(map(lambda x: tr.get(x, x), text))
  21.  
  22.  
  23. # only fow windows!
  24. def ruwrite(text, pause=True):
  25.     # change to ru_ru
  26.     ctypes.windll.user32.PostMessageW(ctypes.windll.user32.GetForegroundWindow(),0x0050, 2, 68748313)
  27.     # pause for change keyboard assurance
  28.     if pause:
  29.         time.sleep(1)
  30.     try:
  31.         converted_text = translate(text)
  32.         pyautogui.write(converted_text)
  33.     except Exception as err:
  34.         print('err in ruwrite fuicntion: %s' % err)
  35.     finally:
  36.         # change to en_us
  37.         ctypes.windll.user32.PostMessageW(ctypes.windll.user32.GetForegroundWindow(), 0x0050, 2, 67699721)
  38.         if pause:
  39.             time.sleep(1)
  40. pyautogui.ruwrite = ruwrite
  41.  
  42.  
  43. if __name__ == '__main__':
  44.  
  45.     s = 'Привет, мир!'
  46.     pyautogui.write('\n"write method only works for ascii chars, for cyrillc text use ruwrite method!"\n')
  47.     pyautogui.write('"')
  48.     pyautogui.ruwrite(s)
  49.     pyautogui.write('"\n')
  50.    
  51.     # ! stay cursor here:
  52.     # >>"write method only for ascii chars, for cyrillc text use ruwrite method!"
  53.     # >>"Привет, мир!"
  54.  
  55.     print('done')
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement