让pyHook和SendKeys一起工作

时间:2011-05-01 22:40:13

标签: python winapi sendkeys keylogger

我正在尝试在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()

1 个答案:

答案 0 :(得分:0)

没关系。我只需要在调用SendKeys之前调用hm.UnhookKeyboard()。

修改:有人问我更多信息。我决定将我的关键相关实验转储到GitHub上:https://github.com/JesseAldridge/Keyboard-Tricks