我在网上找到的用python写的在windows下控制鼠标的操作,求高手讲解一下这个代码,本人新手,看不懂~~

代码是:from ctypes import *
import time
#<some pos>
win_start = (9, 753)
close_window = (1017, 4)
#</some pos>

PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
_fields_ = [("wVk", c_ushort),("wScan", c_ushort),("dwFlags", c_ulong),("time", c_ulong),("dwExtraInfo", PUL)]

class HardwareInput(Structure):
_fields_ = [("uMsg", c_ulong),("wParamL", c_short),("wParamH", c_ushort)]

class MouseInput(Structure):
_fields_ = [("dx", c_long),("dy", c_long),("mouseData", c_ulong),("dwFlags", c_ulong),("time",c_ulong),("dwExtraInfo", PUL)]

class Input_I(Union):
_fields_ = [("ki", KeyBdInput),("mi", MouseInput),("hi", HardwareInput)]

class Input(Structure):
_fields_ = [("type", c_ulong),("ii", Input_I)]

class POINT(Structure):
_fields_ = [("x", c_ulong),("y", c_ulong)]

#<Get Pos>
def get_mpos():
orig = POINT()
windll.user32.GetCursorPos(byref(orig))
return int(orig.x), int(orig.y)
#</Get Pos>

#<Set Pos>
def set_mpos(pos):
x,y = pos
windll.user32.SetCursorPos(x, y)
#</Set Pos>

#<move and click>
def move_click(pos, move_back = False):
origx, origy = get_mpos()
set_mpos(pos)
FInputs = Input * 2
extra = c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )
ii2_ = Input_I()
ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )
x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )
windll.user32.SendInput(2, pointer(x), sizeof(x[0]))
if move_back:
set_mpos((origx, origy))
return origx, origy
#</move and click>

def sendkey(scancode, pressed):
FInputs = Input * 1
extra = c_ulong(0)
ii_ = Input_I()
flag = 0x8 # KEY_SCANCODE
ii_.ki = KeyBdInput( 0, 0, flag, 0, pointer(extra) )
InputBox = FInputs( ( 1, ii_ ) )

if scancode == None:
return
InputBox[0].ii.ki.wScan = scancode
InputBox[0].ii.ki.dwFlags = 0x8 # KEY_SCANCODE
if not(pressed):
InputBox[0].ii.ki.dwFlags |= 0x2 # released
windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))
if pressed:
print "%x pressed" % scancode
else:
print "%x released" % scancode

if __name__ == '__main__':
origx, origy = get_mpos()
print 'Orig Pos:', origx, origy
#set_mpos(9, 753)
#move_click(win_start)
move_click((800,600))
time.sleep(1)
sendkey(0x99,1)
#keyboard_input()

#set_mpos(origx, origy)

第1个回答  2010-12-03
这个就是直接用的winapi,你到msdn上搜相应的函数就知道了。
第2个回答  2010-11-28
1、用python2.5。2.6和2.7其实是为了推广3.x,从python2.x到python3.x的过渡产物,部分语法同时向上下兼容。目前大部分代码都是2.5的,所以建议用2.5。

2、安装很简单,没什么要注意的,最好最后设置一下path。

3、直接去python的官方网下相应版本的文档。要是想快速入门,有本书叫《征服 python》,写的不怎么样,介绍面广但不全,入门还是不错,借着看看就行,不值得买。

PS:
不是必须的,直接用文本编辑器(记事本、UE、emacs之类的)编写就行,写完直接改后缀名字为.py就行。
另外你说的那截图是python提供的GUI,理解粗一点就和matlab的command window一样,逐句输入,逐句执行。本回答被网友采纳
相似回答