Pythonでオートコレクトメカニズムを作成しようとしています。ユーザーのキーストロークをログに記録し、ユーザーが1秒間入力を停止したら、すべてを消去して、修正した文を再入力します。
以下のコードは、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()