Python 通过api操控鼠标键盘

发布时间:2019-09-10 08:48:45编辑:auto阅读(1862)

    Pymouse

    标签: python


    PyMouse

    http://blog.sina.com.cn/s/blog_60b45f230101kucn.html
    
    [python-sendkeys 模拟键盘事件的模块](http://blog.csdn.net/fangkailove/article/details/7614492)
    
    - 导入需要的包
    ```
    import win32api
    import win32con
    import win32gui
    from ctypes import *
    import time
    ```
    
    • 设置鼠标双击的函数,通过坐标控制双击位点

      def double_click(x=0,y=0):
      mouse_move(x,y)
      time.sleep(0.05)    #延迟时间,尤其是在电脑反映不是很快的时候,
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) #点击鼠标
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0,0,0,0)  #抬起鼠标
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
      win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0,0,0,0)
    • 捕获鼠标当前位置函数

      def get_mouse_position():
          po = POINT()
          windll.user32.GetCursorPos(byref(po))
          return int(po.x),int(po.y)
      class POINT(Structure):
          fields_=[("x",c_ulong),("y",c_ulong)]
    • 模拟键盘输入

          win32api.keybd_event(86,0,0,0)
    • 键码表
      Win32 api函数表
      附个键位码表:

    字母和数字键 数字小键盘的键 功能键 其它键
    键码 键码 键码 键码
    A 65 0
    B 66 1 97 F2 113 Tab 9
    C 67 2 98 F3 114 Clear 12
    D 68 3 99 F4 115 Enter 13
    E 69 4 100 F5 116 Shift 16
    F 70 5 101 F6 117 Control 17
    G 71 6 102 F7 118 Alt 18
    H 72 7 103 F8 119 Caps Lock
    I 73 8 104 F9 120 Esc 27
    J 74 9 105 F10 121 Spacebar 32
    K 75 * 106 F11 122 Page Up
    L 76 + 107 F12 123 Page Down
    M 77 Enter 108 End 35
    N 78 - 109 Home 36
    O 79 . 110 Left Arrow
    P 80 / 111 Up Arrow
    Q 81 Right Arrow
    R 82 Down Arrow
    S 83 Insert 45
    T 84 Delete 46
    U 85 Help 47
    V 86 Num Lock

    其他未列出的字母和数字键盘为:ord(c)
    “`

关键字