5
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.

Junkah
  • 51
  • 1
  • 3
  • Haha thank you so much! I was dealing with the same problem but my solution was to make two events one for mouse UP and one for DOWN so that one of them will work :P – Piotr Dabkowski Dec 27 '12 at 19:42
  • @Junkah - If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 10 '13 at 04:59

1 Answers1

1

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

Community
  • 1
  • 1
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71