Python win32api.mouse_event TypeError

3.8k views Asked by At
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()

The function CursorLeft works fine every other time. It also works fine without any negative numbers as parameters. I am completely lost as to why this is happening!

First call, fine.

Second call,

TypeError: an integer is required

Third call, fine.

Fourth call,

TypeError: an integer is required.

so on and so on.





Solved

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

The last two parameters passed allow the function to behave properly. I am still not sure as to why and would still like to know but at least it is working now.

Solved

return True

Very important that the event functions return true.

1

There are 1 answers

0
DreadPirateShawn On

Copying the answer from the comments in order to remove this question from the "Unanswered" filter:

return True

Very important that the event functions return true.

~ answer per Junkah