Python's approach to simulating keystrokes and mouse clicks under Windows

Source: Internet
Author: User
Tags 0xc0

Go to blog https://www.cnblogs.com/huidaoli/p/7398392.html, original Huidaoli

# _*_ Coding:utf-8 _*_import win32apiimport win32conimport win32guifrom ctypes import *import TimeVK_CODE = {' Backspac E ': 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, ' F1 6 ': 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, ' B Rowser_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 ': 0x B2, ' Play/pause_media ': 0xb3, ' start_mail ': 0xb4, ' Select_media ': 0xb5, ' start_application_1 ': 0xb6, ' Start_app ' Lication_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) 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) 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 mouse_move (x, y): def key_input (str= "): Windll.user32.SetCursorPos (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) if __name__ = = "__main__": Mouse_click (500,280) str1 = ' python ' ke Y_input (STR1) Mouse_click (1000,280)



Add some parts of yourself: Invoke the statement of the individual keys:
if __name__ = = ' __main__ ':
  
Win32api.keybd_event (0x12, 0, 0, 0)
Win32api.keybd_event (0x41, 0, 0, 0)
Time.sleep (1)
Win32api.keybd_event (0x12, 0, Win32con. Keyeventf_keyup, 0)
Win32api.keybd_event (0x41, 0, Win32con. Keyeventf_keyup, 0)


Python's approach to simulating keystrokes and mouse clicks under Windows

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.