1

我正在尝试编写在双击 CTRL 后执行某些操作的脚本。第一次双击后效果很好,但后来我得到了那个错误。此外,如果我在计时器执行功能触发后一次又一次地按 CTRL,我会得到同样的错误。

import pythoncom, pyHook, threading

press = False

def triger():
    global press
    press=False
    
def something():
    print 'hello'
    
def OnKeyboardEvent(event):
    global press
    if event.Key=='Lcontrol':
        if press:
            something()
            press = False
        else:
            press=True
            threading.Timer(1,triger).start()
            

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

错误:

hello

Warning (from warnings module):
  File "C:\Python27\lib\threading.py", line 828
    return _active[_get_ident()]
RuntimeWarning: tp_compare didn't return -1 or -2 for exception
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch
    return func(event)
  File "C:\Users\123\Desktop\code\hooks.py", line 20, in OnKeyboardEvent
    threading.Timer(1,triger).start()
  File "C:\Python27\lib\threading.py", line 731, in Timer
    return _Timer(*args, **kwargs)
  File "C:\Python27\lib\threading.py", line 742, in __init__
    Thread.__init__(self)
  File "C:\Python27\lib\threading.py", line 446, in __init__
    self.__daemonic = self._set_daemon()
  File "C:\Python27\lib\threading.py", line 470, in _set_daemon
    return current_thread().daemon
  File "C:\Python27\lib\threading.py", line 828, in currentThread
    return _active[_get_ident()]
TypeError: an integer is required
4

1 回答 1

3

根据文档,在 OnKeyboardEvent 函数的末尾必须有“return True”。这是它的样子。

def OnKeyboardEvent(event):
    global press
    if event.Key=='Lcontrol':
        if press:
            something()
            press = False
        else:
            press=True
            threading.Timer(1,triger).start()
            print 'waiting'
    return True
于 2012-08-04T16:27:11.060 回答