Using PYWIN32 to implement Windows simulation mouse and keyboard actions

Source: Internet
Author: User
Because to batch with a software batch of EPS files, so to simulate the mouse and keyboard action, so that it can automate the operation.

Copy the Code code as follows:


#-*-coding:utf-8-*-
Import OS
Import time
Import Win32gui
Import Win32API
Import Win32con
From PIL import Imagegrab

#os. Startfile ("D:\\artcut6\\prog\\artcut6.exe")
#time. Sleep (1)

Wdname1=u "Wentaisculpturemulti painting 2009[]-[Untitled-1]"
W1hd=win32gui. FindWindow (0,WDNAME1)
Print W1HD
W2hd=win32gui. FindWindowEx (W1hd,none,none,none)
Print W2HD

def aotohelper_wt (i):
#获取窗口焦点
Win32gui. SetForegroundWindow (W2HD)
#快捷键Alt +f
Win32api.keybd_event (18,0,0,0) # ALT
Win32api.keybd_event (70,0,0,0) # F
Win32api.keybd_event (70,0,win32con. keyeventf_keyup,0) #释放按键
Win32api.keybd_event (18,0,win32con. keyeventf_keyup,0)

#快捷键I
Win32api.keybd_event (73,0,0,0) # I
Win32api.keybd_event (73,0,win32con. keyeventf_keyup,0)

Time.sleep (0.2)
Wdname3=u "Open"
W3hd=win32gui. FindWindow (none,wdname3) # Open Window handle
#print W3HD

#win32gui. MoveWindow (W3HD, Max, Max, Max, True)
If i<=9:
Msg= "YC-00" +str (i)
Elif 10<=i<=99:
Msg= "YC-0" +str (i)
Else
Msg= "yc-" +str (i)
Edithd=win32gui. FindWindowEx (W3hd,none, "Edit", None)
Win32API. SendMessage (Edithd,win32con. WM_SETTEXT,NONE,MSG)
Time.sleep (0.1)
#btnhd =win32gui. FindWindowEx (W3hd,none, "button", None)
#print BTNHD
#模拟快捷键Alt +o
Win32api.keybd_event (18,0,0,0) # ALT
Win32api.keybd_event (79,0,0,0) # O
Win32api.keybd_event (79,0,win32con. keyeventf_keyup,0) #释放按键
Win32api.keybd_event (18,0,win32con. keyeventf_keyup,0)
Time.sleep (0.1)
#模拟鼠标操作
Win32API. Setcursorpos ([30,150]) #为鼠标焦点设定一个位置
Win32api.mouse_event (Win32con. mouseeventf_leftdown,0,0,0,0)
Win32API. Setcursorpos ([500,500])
Win32api.mouse_event (Win32con. mouseeventf_leftup,0,0,0,0)
Time.sleep (0.1)
#模拟快捷键F7 (Limit observation)
Win32api.keybd_event (118,0,0,0) # F7
Win32api.keybd_event (118,0,win32con. keyeventf_keyup,0) #释放按键
Time.sleep (0.2)
#利用PIL截屏
Path= "C:\\users\\ly\\desktop\\pic\\"
Filename= "yc-" +str (i) + ". jpg"
Im=imagegrab.grab ()
Im.save (Path+filename)

#模拟快捷键F8 (back to original page size)
Win32api.keybd_event (119,0,0,0) # F8
Win32api.keybd_event (119,0,win32con. keyeventf_keyup,0) #释放按键

#模拟键盘事件delete
Win32api.keybd_event (46,0,0,0) # Delete
Win32api.keybd_event (46,0,win32con. keyeventf_keyup,0) #释放按键
Time.sleep (1)

For I in Range (2,85):
AOTOHELPER_WT (i)
Print I
Print "Work done!"

  • 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.