Python implements the method of simulating keystrokes and mouse clicks under Windows _python

Source: Internet
Author: User
Tags 0xc0

This article is an example of how Python implements windows to simulate keystrokes and mouse clicks. Share to everyone for your reference. Specifically as follows:

This code can simulate the Press button on the window, the left mouse button click, the right mouse button click, double-click the mouse, etc.

# # _*_ coding:utf-8 _*_ import win32api import win32con import Win32gui from ctypes import * Import Time 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, ' exe Cute ': 0x2b, ' print_screen ': 0x2c, ' ins ': 0x2d, ' del ': 0x2e, ' help ': 0x2f, ' 0 ': 0x30, ' 1 ': 0x31, ' 2 ': 0x32, ' 3 ': 0x , ' 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_ke Y ': 0x6c, ' Subtract_key ': 0x6d, ' Decimal_key ': 0x6e, ' Divide_key ': 0x6f, ' F1 ': 0x70, ' F2 ': 0x71, ' F3 ': 0x72, ' F4 ': 0 x73, ' F5 ': 0x74, ' F6 ': 0x75, ' F7 ': 0x76, ' F8 ': 0x77, ' F9 ': 0x78, ' F10 ': 0x79, ' F11 ': 0x7a, ' F12 ': 0x 7C, ' 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_f Orward ': 0xa7, ' Browser_refresh ': 0xa8, ' browser_stop ': 0xa9, ' browser_search ': 0xAA, ' browser_favorites ': 0xAB, ' bro Wser_start_and_home ': 0xAC, ' Volume_mute ': 0xAD, ' Volume_down ': 0xAE, ' volume_up ': 0xAF, ' next_track ': 0xb0, ' previous_track ': 0xb1, ' stop_m Edia ': 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, ' C Lear_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) d EF Mouse_click (x=none,y=none): If not X are None and not Y-is none:mouse_move (x,y) time.sleep (0.05) WIN32API.M Ouse_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 is None and Not Y-is 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): Windll.user32.SetCursorPos (x, y) 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) if __name__ = = "__main__": Mouse_click (1024,470) str = ' Hello ' key_input (str )

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.