Python win32api.mouse_event TypeError

时间:2012-08-20 13:29:07

标签: python winapi pyhook pythoncom

import sys
import win32api, win32con
import pyHook
import pythoncom

def CursorLeft():
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -1, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

def Quit():
    print "Quitting"
    sys.exit()

# create a keyboard hook
def OnKeyboardEvent(event):
    print 'MessageName:', event.MessageName
    print 'Key:', event.Key
    if event.Key in ['Numpad2']:
        CursorLeft()
    elif event.Key in ['End']:
        Quit()
    return True

def OnMouseEvent(event):
    print 'Position:', event.Position
    return True

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

CursorLeft函数每隔一段时间都能正常工作。它也可以正常工作,没有任何负数作为参数。我完全不知道为什么会发生这种情况!

第一次打电话,很好。

第二次电话,

  

TypeError:需要一个整数

第三次电话,很好。

第四次电话,

  

TypeError:需要一个整数。

等等等等。




<小时/> <击>

解决

win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -1, 0, 0, 0)

传递的最后两个参数允许函数正常运行。我仍然不确定为什么,仍然想知道,但至少它现在正在运作

解决

return True

非常重要的是事件函数返回true。

1 个答案:

答案 0 :(得分:1)

从评论中复制答案,以便从“未答复”过滤器中删除此问题:

  

return True

     

非常重要的是事件函数返回true。

〜每Junkah

回答