time.sleep(获取类型NoneType)

时间:2016-03-05 11:50:45

标签: python pyhook

所以我一直在研究一个非常基本的'fastclick宏'。如果我点击速度超过每秒约5次点击,我希望程序点击更多。当它达到每秒5次点击的限制时,我希望它在您的上次和下次点击之间点击,完成time.sleep(clicktime / 2)。出于某种原因,我收到了一个错误:

  

需要一个整数(获取类型NoneType)

这是我的代码:

import pythoncom, pyHook, sys, winsound, os
import win32api, win32con, random, time
from pyHook import HookManager
from pyHook.HookManager import HookConstants

macro = False
tip = 1
firstclick = 0
secondclick = 0
clicktime = 1

bot = False

def ExtraClick():
    global clicktime
    global bot
    x, y = win32api.GetCursorPos()
    print('click')
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) 
    bot = True  

def OnMouseClick(event):
    global tip
    global firstclick
    global secondclick
    global clicktime
    global macro
    global bot

    if bot == True:
        bot = False

    if bot == False:
        if tip == 0:
            tip = 1
            firstclick = time.time()
            clicktime = firstclick - secondclick
            print(clicktime)


        elif tip == 1:
            tip = 0
            secondclick = time.time()
            clicktime = secondclick - firstclick
            print(clicktime)                    


def OnKeyEvent(event):
    global macro
    if event.KeyID == 192:
        if macro == True:
            macro = False
            print('off')
            winsound.Beep(600, 30)
            winsound.Beep(550, 30)
        else:
            macro = True
            print('on')
            winsound.Beep(1000, 30)
            winsound.Beep(1100, 30)
            clickspeed = 1

hm = pyHook.HookManager()
while True:
    hm.MouseLeftDown = OnMouseClick
    hm.KeyDown = OnKeyEvent    
    if clicktime < 0.20 and macro == True:
        time.sleep(clicktime / 2)
        ExtraClick()      
    hm.HookMouse()
    hm.HookKeyboard()
    pythoncom.PumpWaitingMessages()

我也尝试过,而不是使用time.sleep(clicktime / 2)time.sleep(0.1),但这会产生同样的错误。

1 个答案:

答案 0 :(得分:0)

问题不在于time.sleep而在于pyhook - 它似乎需要处理程序返回一个值(True或False)。

请参阅此答案:Help with pyHook error

return True添加到事件处理程序的末尾。