【问题标题】:Python win32api.mouse_event TypeErrorPython win32api.mouse_event 类型错误
【发布时间】:2012-08-20 13:29:07
【问题描述】:
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 非常重要。

【问题讨论】:

  • 哈哈,非常感谢!我正在处理同样的问题,但我的解决方案是制作两个事件,一个用于鼠标 UP,一个用于 DOWN,以便其中一个可以工作:P
  • @Junkah - 如果您想将解决方案概括为您自己的答案,我将删除我的答案。 (请参阅 meta.stackexchange.com/questions/90263/… 详细说明为什么这样做会有所帮助。)谢谢!

标签: python winapi pyhook pythoncom


【解决方案1】:

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

return True

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

~回答Junkah

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2013-09-30
    • 2015-06-25
    • 2014-03-04
    • 2013-09-14
    • 2014-11-30
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多