I'm trying to write an auto-correct mechanism in Python. I log the user's keystrokes, and when they stop typing for a second I want to erase everything and retype the corrected sentence.
The code below is working fine, except for the fact that SendKeys is running very slowly. I think the PumpMessages call is interfering with it someho开发者_Python百科w. Does anyone know how I can deal with this problem?
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()
Nevermind. I just needed to call hm.UnhookKeyboard() before calling SendKeys.
Edit: Somebody asked me for more info. I decided to just dump my key related experiments onto GitHub: https://github.com/JesseAldridge/Keyboard-Tricks
精彩评论