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() 
  • Do exceptions occur? - Andrio Skur
  • @AndrioSkur no, exceptions do not crash. I tried to wrap the imitation of keystrokes in the streams. It turned out, but the program began to run for 2-3 seconds, which is unacceptable, of course. - garbart
  • Can you update the question in accordance with current practices? - gil9red
  • @ gil9red updated - garbart
  • Do you also have keybd_event calls for 5 seconds? :) I got to your code, tried it and was a little surprised at the slow code execution - gil9red

1 answer 1

It turned out to improve the speed of the program, using win32api and wrapping it all in the stream. Thus, the text is cut using:

 win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0) # CTRL press win32api.keybd_event(65, 0, 0, 0) # A win32api.keybd_event(88, 0, 0, 0) # X win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0) # CTRL release 

UPD: At the request of complement

  def cut(): keybd_event(VK_CONTROL, 0, 0, 0) # CTRL press keybd_event(65, 0, 0, 0) # A (ACSII) keybd_event(88, 0, 0, 0) # X keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0) # CTRL release @staticmethod def paste(): keybd_event(VK_CONTROL, 0, 0, 0) # CTRL press keybd_event(86, 0, 0, 0) # V keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0) # CTRL release def on_click(self, key): if str(key) == 'Key.f7': # Вытаскиваем из буфера то, что там есть buf = paste() # Выделяем и вырезаем текст из текстового поля t1 = Thread(target=self.cut) t1.start() t1.join() # Вытаскиеваем транслит-строку, переводим и вставляем в буфер copy(self.switch(paste())) # Вставляем текст нажатием Ctrl + V t1 = Thread(target=self.paste) t1.start() t1.join() # Вставляем в буфер старые данные copy(buf) def __init__(self): with keyboard.Listener( on_press=self.on_click, on_release=self.on_release) as listener: listener.join() 
  • Well done, that figured out :) Your code is the equivalent of SendKeys('^a^x', pause=0, turn_off_numlock=False) ? Can you add the whole code with the correction to the answer? :) - gil9red
  • @ gil9red added - garbart
  • Thanks, I wonder then we will play around with the code :) - gil9red