Thang Nguyen Xuan
Thang Nguyen Xuan

Reputation: 23

Make auto key and mouse press on activate window on Python

I want to write an app in Python on Windows to do some jobs repeatedly.

For example, I need to convert some files into other type. I have a software installed in Windows to do that. However, that program was designed to do it file by file. Now I want to do it automatically.

Therefore, I need to write a software to simulate the key press on active windows. There are a lot of code on autokeyboard but it only works in terminal which run Python script. Specially, after I run Python script, I minimize the terminal, then open some program then the Python script will simulate the key press and/or mouse click in this program.

I found a lot of program can do something like hotkey and after press hotkey, it will simulate some key press and mouse. So I think it is possible.

Could anyone give me a solution for that?

Thanks.

Upvotes: 0

Views: 2868

Answers (1)

Vaibhav Jadhav
Vaibhav Jadhav

Reputation: 2076

This will help you to automate:

for mouse clicks:

import pyautogui
pyautogui.click(1319, 45)
pyautogui.scroll(200)
pyautogui.hotkey("ctrlleft", "a")

For keyboard

import keyboard
# It writes the keys r, k and endofline
keyboard.press_and_release('shift + r, shift + k, \n')
keyboard.press_and_release('R, K') 
# it blocks until esc is pressed 
keyboard.wait('esc')

# It records all the keys until escape is pressed
rk = keyboard.record(until='Esc')

# It replay back the all keys
keyboard.play(rk, speed_factor=1)

Upvotes: 2

Related Questions