我正在尝试在 Python 中编写一个自动更正机制。我记录用户的击键,当他们停止输入一秒钟时,我想删除所有内容并重新输入更正的句子。
下面的代码运行良好,除了 SendKeys 运行非常缓慢。我认为 PumpMessages 调用以某种方式干扰它。有谁知道我该如何处理这个问题?
import threading
import pyHook
import pythoncom
from SendKeys import SendKeys
# Store typed keys. Correct words when stop typing for a bit.
def info_handler():
def event_info(e):
if e.MessageName == 'key down':
v.keys_pressed.append(e.Key)
if v.t: v.t.cancel()
v.t = threading.Timer(1, correct_words)
v.t.start()
return True
return event_info
def correct_words():
SendKeys('{BS %i}' % len(v.keys_pressed))
# Listen to keys.
class v:
keys_pressed = []
t = None
hm = pyHook.HookManager()
hm.KeyDown = info_handler()
hm.HookKeyboard()
pythoncom.PumpMessages()