python模拟鼠标拖动操作的方法
Posted Jaww
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python模拟鼠标拖动操作的方法相关的知识,希望对你有一定的参考价值。
本文实例讲述了python模拟鼠标拖动操作的方法。分享给大家供大家参考。具体如下:
pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签。重复的拖动工作实在无趣,还是让程序帮我实现吧,我可以喝点水,休息一下了
1. Python代码:
1 # 2 # _*_ coding:UTF-8 _*_ 3 __author__ = ‘wp‘ 4 import win32api 5 import win32con 6 import win32gui 7 from ctypes import * 8 import time 9 SW = 1377 10 SH = 768 11 VK_CODE = { 12 ‘backspace‘:0x08, 13 ‘tab‘:0x09, 14 ‘clear‘:0x0C, 15 ‘enter‘:0x0D, 16 ‘shift‘:0x10, 17 ‘ctrl‘:0x11, 18 ‘alt‘:0x12, 19 ‘pause‘:0x13, 20 ‘caps_lock‘:0x14, 21 ‘esc‘:0x1B, 22 ‘spacebar‘:0x20, 23 ‘page_up‘:0x21, 24 ‘page_down‘:0x22, 25 ‘end‘:0x23, 26 ‘home‘:0x24, 27 ‘left_arrow‘:0x25, 28 ‘up_arrow‘:0x26, 29 ‘right_arrow‘:0x27, 30 ‘down_arrow‘:0x28, 31 ‘select‘:0x29, 32 ‘print‘:0x2A, 33 ‘execute‘:0x2B, 34 ‘print_screen‘:0x2C, 35 ‘ins‘:0x2D, 36 ‘del‘:0x2E, 37 ‘help‘:0x2F, 38 ‘0‘:0x30, 39 ‘1‘:0x31, 40 ‘2‘:0x32, 41 ‘3‘:0x33, 42 ‘4‘:0x34, 43 ‘5‘:0x35, 44 ‘6‘:0x36, 45 ‘7‘:0x37, 46 ‘8‘:0x38, 47 ‘9‘:0x39, 48 ‘a‘:0x41, 49 ‘b‘:0x42, 50 ‘c‘:0x43, 51 ‘d‘:0x44, 52 ‘e‘:0x45, 53 ‘f‘:0x46, 54 ‘g‘:0x47, 55 ‘h‘:0x48, 56 ‘i‘:0x49, 57 ‘j‘:0x4A, 58 ‘k‘:0x4B, 59 ‘l‘:0x4C, 60 ‘m‘:0x4D, 61 ‘n‘:0x4E, 62 ‘o‘:0x4F, 63 ‘p‘:0x50, 64 ‘q‘:0x51, 65 ‘r‘:0x52, 66 ‘s‘:0x53, 67 ‘t‘:0x54, 68 ‘u‘:0x55, 69 ‘v‘:0x56, 70 ‘w‘:0x57, 71 ‘x‘:0x58, 72 ‘y‘:0x59, 73 ‘z‘:0x5A, 74 ‘numpad_0‘:0x60, 75 ‘numpad_1‘:0x61, 76 ‘numpad_2‘:0x62, 77 ‘numpad_3‘:0x63, 78 ‘numpad_4‘:0x64, 79 ‘numpad_5‘:0x65, 80 ‘numpad_6‘:0x66, 81 ‘numpad_7‘:0x67, 82 ‘numpad_8‘:0x68, 83 ‘numpad_9‘:0x69, 84 ‘multiply_key‘:0x6A, 85 ‘add_key‘:0x6B, 86 ‘separator_key‘:0x6C, 87 ‘subtract_key‘:0x6D, 88 ‘decimal_key‘:0x6E, 89 ‘divide_key‘:0x6F, 90 ‘F1‘:0x70, 91 ‘F2‘:0x71, 92 ‘F3‘:0x72, 93 ‘F4‘:0x73, 94 ‘F5‘:0x74, 95 ‘F6‘:0x75, 96 ‘F7‘:0x76, 97 ‘F8‘:0x77, 98 ‘F9‘:0x78, 99 ‘F10‘:0x79, 100 ‘F11‘:0x7A, 101 ‘F12‘:0x7B, 102 ‘F13‘:0x7C, 103 ‘F14‘:0x7D, 104 ‘F15‘:0x7E, 105 ‘F16‘:0x7F, 106 ‘F17‘:0x80, 107 ‘F18‘:0x81, 108 ‘F19‘:0x82, 109 ‘F20‘:0x83, 110 ‘F21‘:0x84, 111 ‘F22‘:0x85, 112 ‘F23‘:0x86, 113 ‘F24‘:0x87, 114 ‘num_lock‘:0x90, 115 ‘scroll_lock‘:0x91, 116 ‘left_shift‘:0xA0, 117 ‘right_shift ‘:0xA1, 118 ‘left_control‘:0xA2, 119 ‘right_control‘:0xA3, 120 ‘left_menu‘:0xA4, 121 ‘right_menu‘:0xA5, 122 ‘browser_back‘:0xA6, 123 ‘browser_forward‘:0xA7, 124 ‘browser_refresh‘:0xA8, 125 ‘browser_stop‘:0xA9, 126 ‘browser_search‘:0xAA, 127 ‘browser_favorites‘:0xAB, 128 ‘browser_start_and_home‘:0xAC, 129 ‘volume_mute‘:0xAD, 130 ‘volume_Down‘:0xAE, 131 ‘volume_up‘:0xAF, 132 ‘next_track‘:0xB0, 133 ‘previous_track‘:0xB1, 134 ‘stop_media‘:0xB2, 135 ‘play/pause_media‘:0xB3, 136 ‘start_mail‘:0xB4, 137 ‘select_media‘:0xB5, 138 ‘start_application_1‘:0xB6, 139 ‘start_application_2‘:0xB7, 140 ‘attn_key‘:0xF6, 141 ‘crsel_key‘:0xF7, 142 ‘exsel_key‘:0xF8, 143 ‘play_key‘:0xFA, 144 ‘zoom_key‘:0xFB, 145 ‘clear_key‘:0xFE, 146 ‘+‘:0xBB, 147 ‘,‘:0xBC, 148 ‘-‘:0xBD, 149 ‘.‘:0xBE, 150 ‘/‘:0xBF, 151 ‘`‘:0xC0, 152 ‘;‘:0xBA, 153 ‘[‘:0xDB, 154 ‘\‘:0xDC, 155 ‘]‘:0xDD, 156 "‘":0xDE, 157 ‘`‘:0xC0} 158 class POINT(Structure): 159 _fields_ = [("x", c_ulong),("y", c_ulong)] 160 def get_mouse_point(): 161 po = POINT() 162 windll.user32.GetCursorPos(byref(po)) 163 return int(po.x), int(po.y) 164 def mouse_click(x=None,y=None): 165 if not x is None and not y is None: 166 mouse_move(x,y) 167 time.sleep(0.05) 168 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 169 time.sleep(0.05) 170 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 171 def mouse_dclick(x=None,y=None): 172 if not x is None and not y is None: 173 mouse_move(x,y) 174 time.sleep(0.05) 175 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 176 time.sleep(0.05) 177 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 178 time.sleep(0.05) 179 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 180 time.sleep(0.05) 181 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 182 def mouse_move(x,y): 183 windll.user32.SetCursorPos(x, y) 184 def mouse_absolute(x,y,x2,y2): 185 windll.user32.SetCursorPos(x, y) #鼠标移动到 186 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) #左键按下 187 time.sleep(0.2) 188 mw = int(x2 * 65535 / SW) 189 mh = int(y2 * 65535 / SH) 190 win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, mw, mh, 0, 0) 191 time.sleep(0.2) 192 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 193 def key_input(str=‘‘): 194 for c in str: 195 win32api.keybd_event(VK_CODE[c],0,0,0) 196 win32api.keybd_event(VK_CODE[c],0,win32con.KEYEVENTF_KEYUP,0) 197 time.sleep(0.01) 198 def t0(): 199 pass #do nothing 200 def t2(): 201 mouse_click(800,200) 202 for c in ‘hello‘: 203 win32api.keybd_event(65,0,0,0) #a键位码是86 204 win32api.keybd_event(65,0,win32con.KEYEVENTF_KEYUP,0) 205 #print get_mouse_point() 206 def t1(): 207 #mouse_move(1024,470)aa 208 #time.sleep(0.05) 209 #mouse_dclick()HELLO 210 mouse_dclick(1024,470) 211 def t3(): 212 mouse_click(1024,470) 213 str = ‘hello‘ 214 for c in str: 215 win32api.keybd_event(VK_CODE[c],0,0,0) #a键位码是86 216 win32api.keybd_event(VK_CODE[c],0,win32con.KEYEVENTF_KEYUP,0) 217 time.sleep(0.01) 218 def t4(): 219 mouse_click(900,300) 220 str = ‘hello‘ 221 key_input(str) 222 def t5(): 223 #拖动pdf中的书签 224 po = POINT() 225 windll.user32.GetCursorPos(byref(po)) 226 j = 50 #操作次数 227 x = 766 #x 228 y = 500 #y 229 n = 10 #上移10 230 for i in range(0,j): 231 mouse_absolute(x,y,x,y-n) 232 #windll.user32.SetCursorPos(po.x, po.y) 233 mouse_click(po.x, po.y) 234 print(str(j-i)) 235 if __name__ == "__main__": 236 t5() #move pdf bookmark 237 #t4() 238 #t3() 239 #t2() 240 #t1() 241 #t0()
2. mouse.py文件
1 # 2 # _*_ coding:UTF-8 _*_ 3 __author__ = ‘wp‘ 4 import win32api 5 import win32con 6 import win32gui 7 from ctypes import * 8 import time 9 SW = 1377 10 SH = 768 11 VK_CODE = { 12 ‘backspace‘:0x08, 13 ‘tab‘:0x09, 14 ‘clear‘:0x0C, 15 ‘enter‘:0x0D, 16 ‘shift‘:0x10, 17 ‘ctrl‘:0x11, 18 ‘alt‘:0x12, 19 ‘pause‘:0x13, 20 ‘caps_lock‘:0x14, 21 ‘esc‘:0x1B, 22 ‘spacebar‘:0x20, 23 ‘page_up‘:0x21, 24 ‘page_down‘:0x22, 25 ‘end‘:0x23, 26 ‘home‘:0x24, 27 ‘left_arrow‘:0x25, 28 ‘up_arrow‘:0x26, 29 ‘right_arrow‘:0x27, 30 ‘down_arrow‘:0x28, 31 ‘select‘:0x29, 32 ‘print‘:0x2A, 33 ‘execute‘:0x2B, 34 ‘print_screen‘:0x2C, 35 ‘ins‘:0x2D, 36 ‘del‘:0x2E, 37 ‘help‘:0x2F, 38 ‘0‘:0x30, 39 ‘1‘:0x31, 40 ‘2‘:0x32, 41 ‘3‘:0x33, 42 ‘4‘:0x34, 43 ‘5‘:0x35, 44 ‘6‘:0x36, 45 ‘7‘:0x37, 46 ‘8‘:0x38, 47 ‘9‘:0x39, 48 ‘a‘:0x41, 49 ‘b‘:0x42, 50 ‘c‘:0x43, 51 ‘d‘:0x44, 52 ‘e‘:0x45, 53 ‘f‘:0x46, 54 ‘g‘:0x47, 55 ‘h‘:0x48, 56 ‘i‘:0x49, 57 ‘j‘:0x4A, 58 ‘k‘:0x4B, 59 ‘l‘:0x4C, 60 ‘m‘:0x4D, 61 ‘n‘:0x4E, 62 ‘o‘:0x4F, 63 ‘p‘:0x50, 64 ‘q‘:0x51, 65 ‘r‘:0x52, 66 ‘s‘:0x53, 67 ‘t‘:0x54, 68 ‘u‘:0x55, 69 ‘v‘:0x56, 70 ‘w‘:0x57, 71 ‘x‘:0x58, 72 ‘y‘:0x59, 73 ‘z‘:0x5A, 74 ‘numpad_0‘:0x60, 75 ‘numpad_1‘:0x61, 76 ‘numpad_2‘:0x62, 77 ‘numpad_3‘:0x63, 78 ‘numpad_4‘:0x64, 79 ‘numpad_5‘:0x65, 80 ‘numpad_6‘:0x66, 81 ‘numpad_7‘:0x67, 82 ‘numpad_8‘:0x68, 83 ‘numpad_9‘:0x69, 84 ‘multiply_key‘:0x6A, 85 ‘add_key‘:0x6B, 86 ‘separator_key‘:0x6C, 87 ‘subtract_key‘:0x6D, 88 ‘decimal_key‘:0x6E, 89 ‘divide_key‘:0x6F, 90 ‘F1‘:0x70, 91 ‘F2‘:0x71, 92 ‘F3‘:0x72, 93 ‘F4‘:0x73, 94 ‘F5‘:0x74, 95 ‘F6‘:0x75, 96 ‘F7‘:0x76, 97 ‘F8‘:0x77, 98 ‘F9‘:0x78, 99 ‘F10‘:0x79, 100 ‘F11‘:0x7A, 101 ‘F12‘:0x7B, 102 ‘F13‘:0x7C, 103 ‘F14‘:0x7D, 104 ‘F15‘:0x7E, 105 ‘F16‘:0x7F, 106 ‘F17‘:0x80, 107 ‘F18‘:0x81, 108 ‘F19‘:0x82, 109 ‘F20‘:0x83, 110 ‘F21‘:0x84, 111 ‘F22‘:0x85, 112 ‘F23‘:0x86, 113 ‘F24‘:0x87, 114 ‘num_lock‘:0x90, 115 ‘scroll_lock‘:0x91, 116 ‘left_shift‘:0xA0, 117 ‘right_shift ‘:0xA1, 118 ‘left_control‘:0xA2, 119 ‘right_control‘:0xA3, 120 ‘left_menu‘:0xA4, 121 ‘right_menu‘:0xA5, 122 ‘browser_back‘:0xA6, 123 ‘browser_forward‘:0xA7, 124 ‘browser_refresh‘:0xA8, 125 ‘browser_stop‘:0xA9, 126 ‘browser_search‘:0xAA, 127 ‘browser_favorites‘:0xAB, 128 ‘browser_start_and_home‘:0xAC, 129 ‘volume_mute‘:0xAD, 130 ‘volume_Down‘:0xAE, 131 ‘volume_up‘:0xAF, 132 ‘next_track‘:0xB0, 133 ‘previous_track‘:0xB1, 134 ‘stop_media‘:0xB2, 135 ‘play/pause_media‘:0xB3, 136 ‘start_mail‘:0xB4, 137 ‘select_media‘:0xB5, 138 ‘start_application_1‘:0xB6, 139 ‘start_application_2‘:0xB7, 140 ‘attn_key‘:0xF6, 141 ‘crsel_key‘:0xF7, 142 ‘exsel_key‘:0xF8, 143 ‘play_key‘:0xFA, 144 ‘zoom_key‘:0xFB, 145 ‘clear_key‘:0xFE, 146 ‘+‘:0xBB, 147 ‘,‘:0xBC, 148 ‘-‘:0xBD, 149 ‘.‘:0xBE, 150 ‘/‘:0xBF, 151 ‘`‘:0xC0, 152 ‘;‘:0xBA, 153 ‘[‘:0xDB, 154 ‘\‘:0xDC, 155 ‘]‘:0xDD, 156 "‘":0xDE, 157 ‘`‘:0xC0} 158 class POINT(Structure): 159 _fields_ = [("x", c_ulong),("y", c_ulong)] 160 def get_mouse_point(): 161 po = POINT() 162 windll.user32.GetCursorPos(byref(po)) 163 return int(po.x), int(po.y) 164 def mouse_click(x=None,y=None): 165 if not x is None and not y is None: 166 mouse_move(x,y) 167 time.sleep(0.05) 168 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 169 time.sleep(0.05) 170 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 171 def mouse_dclick(x=None,y=None): 172 if not x is None and not y is None: 173 mouse_move(x,y) 174 time.sleep(0.05) 175 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 176 time.sleep(0.05) 177 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 178 time.sleep(0.05) 179 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 180 time.sleep(0.05) 181 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 182 def mouse_move(x,y): 183 windll.user32.SetCursorPos(x, y) 184 def mouse_absolute(x,y,x2,y2): 185 windll.user32.SetCursorPos(x, y) #鼠标移动到 186 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) #左键按下 187 time.sleep(0.2) 188 mw = int(x2 * 65535 / SW) 189 mh = int(y2 * 65535 / SH) 190 win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, mw, mh, 0, 0) 191 time.sleep(0.2) 192 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 193 def key_input(str=‘‘): 194 for c in str: 195 win32api.keybd_event(VK_CODE[c],0,0,0) 196 win32api.keybd_event(VK_CODE[c],0,win32con.KEYEVENTF_KEYUP,0) 197 time.sleep(0.01) 198 def t0(): 199 pass #do nothing 200 def t2(): 201 mouse_click(800,200) 202 for c in ‘hello‘: 203 win32api.keybd_event(65,0,0,0) #a键位码是86 204 win32api.keybd_event(65,0,win32con.KEYEVENTF_KEYUP,0) 205 #print get_mouse_point() 206 def t1(): 207 #mouse_move(1024,470)aa 208 #time.sleep(0.05) 209 #mouse_dclick()HELLO 210 mouse_dclick(1024,470) 211 def t3(): 212 mouse_click(1024,470) 213 str = ‘hello‘ 214 for c in str: 215 win32api.keybd_event(VK_CODE[c],0,0,0) #a键位码是86 216 win32api.keybd_event(VK_CODE[c],0,win32con.KEYEVENTF_KEYUP,0) 217 time.sleep(0.01) 218 def t4(): 219 mouse_click(900,300) 220 str = ‘hello‘ 221 key_input(str) 222 def t5(): 223 #拖动pdf中的书签 224 po = POINT() 225 windll.user32.GetCursorPos(byref(po)) 226 j = 50 #操作次数 227 x = 766 #x 228 y = 500 #y 229 n = 10 #上移10 230 for i in range(0,j): 231 mouse_absolute(x,y,x,y-n) 232 #windll.user32.SetCursorPos(po.x, po.y) 233 mouse_click(po.x, po.y) 234 print(str(j-i)) 235 if __name__ == "__main__": 236 t5() #move pdf bookmark 237 #t4() 238 #t3() 239 #t2() 240 #t1() 241 #t0()
3. 运行效果如下:
希望本文所述对大家的Python程序设计有所帮助。
以上是关于python模拟鼠标拖动操作的方法的主要内容,如果未能解决你的问题,请参考以下文章