Hello, I wrote a program that copies text from any text field, performs some operation on it (the switch method) and inserts it back. Everything is good, the program works, but there is a problem: it works through time. Either insert what is in the clipboard, then insert a string, without passing through the switch method, or insert nothing at all. In general, a complete nonsense. I suppose that the whole thing is in the delay of the pyperclip methods, but I don’t know how to remove the delay. If this is not possible - advise how to simulate keystrokes. Program text:
from Switcher import switch from pynput import keyboard from pyperclip import copy, paste import win32com.client class Main: shell = win32com.client.Dispatch('WScript.Shell') def on_click(self, key): if str(key) == 'Key.f7': # Вытаскиваем из буфера то, что там сейчас есть buffer = paste() # Выделяем и вырезаем текст из текстового поля self.shell.SendKeys('^a^x') # Вытаскиеваем транслит-строку, переводим и вставляем в буфер copy(switch(paste())) # Вставляем текст нажатием Ctrl + V self.shell.SendKeys('^v') # Вставляем в буфер старые данные copy(buffer) def on_release(self, key): pass def __init__(self): with keyboard.Listener( on_press=self.on_click, on_release=self.on_release) as listener: listener.join() main = Main() UPD: I tried to upgrade Python 2.7 with its SendKeys, the problem persists, the program behaves in the same way as it did:
# -*- coding: utf-8 -*- from Switcher import switch from pynput import keyboard from clipboard import copy, paste from SendKeys import * class Main: def on_click(self, key): if str(key) == 'Key.f7': # Вытаскиваем из буфера то, что там есть buffer = paste() # Выделяем и вырезаем текст из текстового поля SendKeys('^a^x', pause=0, turn_off_numlock=False) # Вытаскиеваем транслит-строку, переводим и вставляем в буфер copy(switch(paste())) # Вставляем текст нажатием Ctrl + V SendKeys('^v', pause=0, turn_off_numlock=False) # Вставляем в буфер старые данные copy(buffer) def on_release(self, key): pass def __init__(self): with keyboard.Listener( on_press=self.on_click, on_release=self.on_release) as listener: listener.join() main = Main()