搞定了几个东西

来源:互联网 发布:java wait 区别 编辑:程序博客网 时间:2024/04/30 00:59
搞定了几个东西
1,取得鼠标位置,get_mpos()
2,移动鼠标,set_mpos()
3,鼠标左键点击,move_click()
4,键盘输入(部分搞定,那个scancode还不明白是啥意思,为什么0x99输出的是字符'p'???)
代码如下
  1. from ctypes import *
  2. import time

  3. #<some pos>
  4. win_start = (9, 753)
  5. close_window = (1017, 4)
  6. #</some pos>

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

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

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

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

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

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

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

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

  31. #<move and click>
  32. def move_click(pos, move_back = False):
  33.         origx, origy = get_mpos()
  34.         set_mpos(pos)
  35.         FInputs = Input * 2
  36.         extra = c_ulong(0)
  37.         ii_ = Input_I()
  38.         ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )
  39.         ii2_ = Input_I()
  40.         ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )
  41.         x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )
  42.         windll.user32.SendInput(2, pointer(x), sizeof(x[0]))
  43.         if move_back:
  44.                 set_mpos((origx, origy))
  45.         return origx, origy
  46. #</move and click>


  47. def sendkey(scancode, pressed):
  48.         FInputs = Input * 1
  49.         extra = c_ulong(0)
  50.         ii_ = Input_I()
  51.         flag = 0x8 # KEY_SCANCODE
  52.         ii_.ki = KeyBdInput( 0, 0, flag, 0, pointer(extra) )
  53.         InputBox = FInputs( ( 1, ii_ ) )
  54.        
  55.         if scancode == None:
  56.                 return
  57.         InputBox[0].ii.ki.wScan = scancode
  58.         InputBox[0].ii.ki.dwFlags = 0x8  # KEY_SCANCODE  
  59.         if not(pressed):
  60.                 InputBox[0].ii.ki.dwFlags |= 0x2 # released
  61.         windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))
  62.         if pressed:
  63.                 print "%x pressed" % scancode
  64.         else:
  65.                 print "%x released" % scancode

  66. if __name__ == '__main__':
  67.         origx, origy = get_mpos()
  68.         print 'Orig Pos:', origx, origy
  69.         #set_mpos(9, 753)
  70.         #move_click(win_start)
  71.         move_click((800,600))
  72.         time.sleep(1)
  73.         sendkey(0x99,1)
  74.         #keyboard_input()
  75.        
  76.         #set_mpos(origx, origy)