Python simulates the way mouse drags operations _python

Source: Internet
Author: User
Tags 0xc0 sleep

This example describes how Python simulates the mouse drag operation. Share to everyone for your reference. Specifically as follows:

Bookmarks in a PDF have only page numbers, ready to drag an existing bookmark to a table of contents, and then add your own page sign. Repetitive drag work is boring, or let the program to help me achieve it, I can drink some water, take a break

1. Python Code

Copy Code code as follows:
#


# _*_ Coding:utf-8 _*_


__author__ = ' WP '


Import Win32API


Import Win32con


Import Win32gui


From ctypes Import *


Import time


SW = 1377


SH = 768


Vk_code = {


' Backspace ': 0x08,


' tab ': 0x09,


' Clear ': 0x0C,


' Enter ': 0x0D,


' Shift ': 0x10,


' Ctrl ': 0X11,


' Alt ': 0x12,


' Pause ': 0x13,


' Caps_lock ': 0x14,


' Esc ': 0x1b,


' SPACEBAR ': 0x20,


' Page_up ': 0x21,


' Page_down ': 0x22,


' End ': 0x23,


' Home ': 0x24,


' Left_arrow ': 0x25,


' Up_arrow ': 0x26,


' Right_arrow ': 0x27,


' Down_arrow ': 0x28,


' SELECT ': 0x29,


' Print ': 0x2a,


' Execute ': 0x2b,


' Print_screen ': 0x2c,


' ins ': 0x2d,


' del ': 0x2e,


' Help ': 0x2f,


' 0 ': 0x30,


' 1 ': 0x31,


' 2 ': 0x32,


' 3 ': 0x33,


' 4 ': 0x34,


' 5 ': 0x35,


' 6 ': 0x36,


' 7 ': 0x37,


' 8 ': 0x38,


' 9 ': 0x39,


' A ': 0x41,


' B ': 0x42,


' C ': 0x43,


' d ': 0x44,


' E ': 0x45,


' F ': 0x46,


' G ': 0x47,


' H ': 0x48,


' I ': 0x49,


' J ': 0x4a,


' K ': 0x4b,


' L ': 0x4c,


' m ': 0x4d,


' n ': 0x4e,


' O ': 0x4f,


' P ': 0x50,


' Q ': 0x51,


' R ': 0x52,


' s ': 0x53,


' t ': 0x54,


' U ': 0x55,


' V ': 0x56,


' W ': 0x57,


' x ': 0x58,


' Y ': 0x59,


' Z ': 0x5a,


' Numpad_0 ': 0x60,


' Numpad_1 ': 0x61,


' Numpad_2 ': 0x62,


' Numpad_3 ': 0x63,


' Numpad_4 ': 0x64,


' Numpad_5 ': 0x65,


' Numpad_6 ': 0x66,


' numpad_7 ': 0x67,


' Numpad_8 ': 0x68,


' Numpad_9 ': 0x69,


' Multiply_key ': 0x6a,


' Add_key ': 0x6b,


' Separator_key ': 0x6c,


' Subtract_key ': 0x6d,


' Decimal_key ': 0x6e,


' Divide_key ': 0x6f,


' F1 ': 0x70,


' F2 ': 0x71,


' F3 ': 0x72,


' F4 ': 0x73,


' F5 ': 0x74,


' F6 ': 0x75,


' F7 ': 0x76,


' F8 ': 0x77,


' F9 ': 0x78,


' F10 ': 0x79,


' F11 ': 0x7a,


' F12 ': 0x7b,


' F13 ': 0x7c,


' F14 ': 0x7d,


' F15 ': 0x7E,


' F16 ': 0x7F,


' F17 ': 0x80,


' F18 ': 0x81,


' F19 ': 0x82,


' F20 ': 0x83,


' F21 ': 0x84,


' F22 ': 0x85,


' F23 ': 0x86,


' F24 ': 0x87,


' Num_lock ': 0x90,


' Scroll_lock ': 0x91,


' Left_shift ': 0xa0,


' Right_shift ': 0xa1,


' Left_control ': 0xa2,


' Right_control ': 0xa3,


' Left_menu ': 0xa4,


' Right_menu ': 0xa5,


' Browser_back ': 0xa6,


' Browser_forward ': 0xa7,


' Browser_refresh ': 0xa8,


' Browser_stop ': 0xa9,


' Browser_search ': 0xAA,


' Browser_favorites ': 0xAB,


' Browser_start_and_home ': 0xAC,


' Volume_mute ': 0xAD,


' Volume_down ': 0xAE,


' Volume_up ': 0xAF,


' Next_track ': 0xb0,


' Previous_track ': 0xb1,


' Stop_media ': 0xb2,


' Play/pause_media ': 0xb3,


' Start_mail ': 0xb4,


' Select_media ': 0xb5,


' start_application_1 ': 0xb6,


' Start_application_2 ': 0xb7,


' Attn_key ': 0xf6,


' Crsel_key ': 0xf7,


' Exsel_key ': 0xf8,


' Play_key ': 0xFA,


' Zoom_key ': 0xFB,


' Clear_key ': 0xFE,


' + ': 0xBB,


', ': 0xBC,


'-': 0xBD,


'. ': 0 XBE,


'/': 0xBF,


": 0xc0,


'; ': 0 XBA,


' [': 0xDB,


' \ \ ': 0xDC,


'] ': 0xDD,


"'": 0xDE,


': 0xc0}


Class Point (Structure):


_fields_ = [("X", C_ulong), ("Y", C_ulong)]


Def get_mouse_point ():


PO = Point ()


Windll.user32.GetCursorPos (ByRef (PO))


return int (po.x), int (PO.Y)


def mouse_click (X=none,y=none):


If not X are none and not Y are none:


Mouse_move (X,y)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def mouse_dclick (X=none,y=none):


If not X are none and not Y are none:


Mouse_move (X,y)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def mouse_move (x,y):


Windll.user32.SetCursorPos (x, y)


def mouse_absolute (x,y,x2,y2):


Windll.user32.SetCursorPos (x, y) #鼠标移动到


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0) #左键按下


Time.sleep (0.2)


MW = Int (x2 * 65535/sw)


MH = Int (y2 * 65535/sh)


Win32api.mouse_event (Win32con. Mouseeventf_absolute + Win32con. Mouseeventf_move, MW, MH, 0, 0)


Time.sleep (0.2)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def key_input (str= '):


For C in STR:


Win32api.keybd_event (vk_code[c],0,0,0)


Win32api.keybd_event (Vk_code[c],0,win32con. keyeventf_keyup,0)


Time.sleep (0.01)


Def t0 ():


Pass #do Nothing


Def t2 ():


Mouse_click (800,200)


For C in ' Hello ':


Win32api.keybd_event (65,0,0,0) #a键位码是86


Win32api.keybd_event (65,0,win32con. keyeventf_keyup,0)


#print Get_mouse_point ()


def t1 ():


#mouse_move (1024,470) AA


#time. Sleep (0.05)


#mouse_dclick () HELLO


Mouse_dclick (1024,470)


def t3 ():


Mouse_click (1024,470)


str = ' Hello '


For C in STR:


Win32api.keybd_event (vk_code[c],0,0,0) #a键位码是86


Win32api.keybd_event (Vk_code[c],0,win32con. keyeventf_keyup,0)


Time.sleep (0.01)


def t4 ():


Mouse_click (900,300)


str = ' Hello '


Key_input (str)


Def T5 ():


#拖动pdf中的书签


PO = Point ()


Windll.user32.GetCursorPos (ByRef (PO))


j = #操作次数


x = 766 #x


y = #y


n = Ten #上移10


For I in Range (0,J):


Mouse_absolute (X,y,x,y-n)


#windll. User32.setcursorpos (po.x, PO.Y)


Mouse_click (po.x, PO.Y)


Print (str (j-i))


if __name__ = = "__main__":


T5 () #move PDF bookmark


#t4 ()


#t3 ()


#t2 ()


#t1 ()


#t0 ()



2. mouse.py Documents


Copy Code code as follows:



#


# _*_ Coding:utf-8 _*_


__author__ = ' WP '


Import Win32API


Import Win32con


Import Win32gui


From ctypes Import *


Import time


SW = 1377


SH = 768


Vk_code = {


' Backspace ': 0x08,


' tab ': 0x09,


' Clear ': 0x0C,


' Enter ': 0x0D,


' Shift ': 0x10,


' Ctrl ': 0X11,


' Alt ': 0x12,


' Pause ': 0x13,


' Caps_lock ': 0x14,


' Esc ': 0x1b,


' SPACEBAR ': 0x20,


' Page_up ': 0x21,


' Page_down ': 0x22,


' End ': 0x23,


' Home ': 0x24,


' Left_arrow ': 0x25,


' Up_arrow ': 0x26,


' Right_arrow ': 0x27,


' Down_arrow ': 0x28,


' SELECT ': 0x29,


' Print ': 0x2a,


' Execute ': 0x2b,


' Print_screen ': 0x2c,


' ins ': 0x2d,


' del ': 0x2e,


' Help ': 0x2f,


' 0 ': 0x30,


' 1 ': 0x31,


' 2 ': 0x32,


' 3 ': 0x33,


' 4 ': 0x34,


' 5 ': 0x35,


' 6 ': 0x36,


' 7 ': 0x37,


' 8 ': 0x38,


' 9 ': 0x39,


' A ': 0x41,


' B ': 0x42,


' C ': 0x43,


' d ': 0x44,


' E ': 0x45,


' F ': 0x46,


' G ': 0x47,


' H ': 0x48,


' I ': 0x49,


' J ': 0x4a,


' K ': 0x4b,


' L ': 0x4c,


' m ': 0x4d,


' n ': 0x4e,


' O ': 0x4f,


' P ': 0x50,


' Q ': 0x51,


' R ': 0x52,


' s ': 0x53,


' t ': 0x54,


' U ': 0x55,


' V ': 0x56,


' W ': 0x57,


' x ': 0x58,


' Y ': 0x59,


' Z ': 0x5a,


' Numpad_0 ': 0x60,


' Numpad_1 ': 0x61,


' Numpad_2 ': 0x62,


' Numpad_3 ': 0x63,


' Numpad_4 ': 0x64,


' Numpad_5 ': 0x65,


' Numpad_6 ': 0x66,


' numpad_7 ': 0x67,


' Numpad_8 ': 0x68,


' Numpad_9 ': 0x69,


' Multiply_key ': 0x6a,


' Add_key ': 0x6b,


' Separator_key ': 0x6c,


' Subtract_key ': 0x6d,


' Decimal_key ': 0x6e,


' Divide_key ': 0x6f,


' F1 ': 0x70,


' F2 ': 0x71,


' F3 ': 0x72,


' F4 ': 0x73,


' F5 ': 0x74,


' F6 ': 0x75,


' F7 ': 0x76,


' F8 ': 0x77,


' F9 ': 0x78,


' F10 ': 0x79,


' F11 ': 0x7a,


' F12 ': 0x7b,


' F13 ': 0x7c,


' F14 ': 0x7d,


' F15 ': 0x7E,


' F16 ': 0x7F,


' F17 ': 0x80,


' F18 ': 0x81,


' F19 ': 0x82,


' F20 ': 0x83,


' F21 ': 0x84,


' F22 ': 0x85,


' F23 ': 0x86,


' F24 ': 0x87,


' Num_lock ': 0x90,


' Scroll_lock ': 0x91,


' Left_shift ': 0xa0,


' Right_shift ': 0xa1,


' Left_control ': 0xa2,


' Right_control ': 0xa3,


' Left_menu ': 0xa4,


' Right_menu ': 0xa5,


' Browser_back ': 0xa6,


' Browser_forward ': 0xa7,


' Browser_refresh ': 0xa8,


' Browser_stop ': 0xa9,


' Browser_search ': 0xAA,


' Browser_favorites ': 0xAB,


' Browser_start_and_home ': 0xAC,


' Volume_mute ': 0xAD,


' Volume_down ': 0xAE,


' Volume_up ': 0xAF,


' Next_track ': 0xb0,


' Previous_track ': 0xb1,


' Stop_media ': 0xb2,


' Play/pause_media ': 0xb3,


' Start_mail ': 0xb4,


' Select_media ': 0xb5,


' start_application_1 ': 0xb6,


' Start_application_2 ': 0xb7,


' Attn_key ': 0xf6,


' Crsel_key ': 0xf7,


' Exsel_key ': 0xf8,


' Play_key ': 0xFA,


' Zoom_key ': 0xFB,


' Clear_key ': 0xFE,


' + ': 0xBB,


', ': 0xBC,


'-': 0xBD,


'. ': 0 XBE,


'/': 0xBF,


": 0xc0,


'; ': 0 XBA,


' [': 0xDB,


' \ \ ': 0xDC,


'] ': 0xDD,


"'": 0xDE,


': 0xc0}


Class Point (Structure):


_fields_ = [("X", C_ulong), ("Y", C_ulong)]


Def get_mouse_point ():


PO = Point ()


Windll.user32.GetCursorPos (ByRef (PO))


return int (po.x), int (PO.Y)


def mouse_click (X=none,y=none):


If not X are none and not Y are none:


Mouse_move (X,y)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def mouse_dclick (X=none,y=none):


If not X are none and not Y are none:


Mouse_move (X,y)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0)


Time.sleep (0.05)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def mouse_move (x,y):


Windll.user32.SetCursorPos (x, y)


def mouse_absolute (x,y,x2,y2):


Windll.user32.SetCursorPos (x, y) #鼠标移动到


Win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0, 0, 0) #左键按下


Time.sleep (0.2)


MW = Int (x2 * 65535/sw)


MH = Int (y2 * 65535/sh)


Win32api.mouse_event (Win32con. Mouseeventf_absolute + Win32con. Mouseeventf_move, MW, MH, 0, 0)


Time.sleep (0.2)


Win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0, 0, 0)


def key_input (str= '):


For C in STR:


Win32api.keybd_event (vk_code[c],0,0,0)


Win32api.keybd_event (Vk_code[c],0,win32con. keyeventf_keyup,0)


Time.sleep (0.01)


Def t0 ():


Pass #do Nothing


Def t2 ():


Mouse_click (800,200)


For C in ' Hello ':


Win32api.keybd_event (65,0,0,0) #a键位码是86


Win32api.keybd_event (65,0,win32con. keyeventf_keyup,0)


#print Get_mouse_point ()


def t1 ():


#mouse_move (1024,470) AA


#time. Sleep (0.05)


#mouse_dclick () HELLO


Mouse_dclick (1024,470)


def t3 ():


Mouse_click (1024,470)


str = ' Hello '


For C in STR:


Win32api.keybd_event (vk_code[c],0,0,0) #a键位码是86


Win32api.keybd_event (Vk_code[c],0,win32con. keyeventf_keyup,0)


Time.sleep (0.01)


def t4 ():


Mouse_click (900,300)


str = ' Hello '


Key_input (str)


Def T5 ():


#拖动pdf中的书签


PO = Point ()


Windll.user32.GetCursorPos (ByRef (PO))


j = #操作次数


x = 766 #x


y = #y


n = Ten #上移10


For I in Range (0,J):


Mouse_absolute (X,y,x,y-n)


#windll. User32.setcursorpos (po.x, PO.Y)


Mouse_click (po.x, PO.Y)


Print (str (j-i))


if __name__ = = "__main__":


T5 () #move PDF bookmark


#t4 ()


#t3 ()


#t2 ()


#t1 ()


#t0 ()





3. The operation effect is as follows:

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.