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.
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)
Related
For my code I am using the import keyboard, and after I execute the code, my keyboard thinks the ctrl key is pressed down and I need to press Control again for it to realize that the key isn't pressed down.
For example when I press 'A' (without really pressing on control), it selects all (like 'Ctrl + A'), or when I press 'F', it shows me the find in page thing (like 'Ctrl + F').
Just to clarify, after I execute my code, my keyboard does this stuff that I wrote here in every program.
Example for my code:
import keyboard
keyboard.press('Control+S')
keyboard.write('Hello')
keyboard.press('enter')
I run this code on google, this part of code saves an image, names it 'Hello', and then presses enter to download it.
I have a Python script that sends keystrokes, what I'm trying to do is, run the script in a process that's in the background. Meaning that while I'm focused on a different process for example chrome.exe, I want the script to send keystrokes to a process in the background for example test.exe.
Try this.
import keyboard
import time
while True:
time.sleep(.1)
k = keyboard.get_hotkey_name()
if k=='p': # change s with another key
print('p is pressed')
# OR #
if keyboard.is_pressed(hotkey='p'):
print('p is pressed')
Note: If you want to add a event on ctrl+s or ctrl+p You just need to change p to ctrl+p or anything you want.
I have been recently trying to create a program that makes new folder in python(pyautogui).
Here is my code:
import pyautogui;# import the library
pyautogui.press('ctrl');# makes our program to press 'ctrl'
pyautogui.press('n');# makes our program to press 'n'
Apparently what it does instead is pressing ctrl and n individually and I want from them to be pressed together. Please help.
According to the docs:
The press() function is really just a wrapper for the keyDown()
and keyUp() functions, which simulate pressing a key down and then
releasing it up.
As you want to combine several key presses, you need to call keyDown() and keyUp() separately:
pyautogui.keyDown('ctrl')
pyautogui.press('n')
pyautogui.keyUp('ctrl')
I've tried to open the "Run" application to execute Windows commands. But I use an ugly function for it. I want to decrease my code size.
I currently use
import pywinauto
pywinauto.Application().start("explorer.exe Shell:::{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}")
I want to use
import pywinauto
pywinauto.keyboard.send_keys('{RWIN} R')
but it doesn't work.
Execution Movie
I want pywinauto to press the Windows key at the same time as the R key.
You are right, in order to make it work you have to keep the win key pressed down while pressing r key. To achieve this you should use modifiers and actions:
import pywinauto
pywinauto.keyboard.send_keys("{VK_LWIN down}r{VK_LWIN up}")
See the related Pywinauto docs: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html?highlight=send_keys#pywinauto-keyboard
Is there anyway for python 3 to recognise a keypress? For example, if the user pressed the up arrow, the program would do one thing whereas if the down arrow was pressed, the program would do something else.
I do not mean the input() function where the user has to press enter after the keypress , I mean where the program recognises the keypress as some as it was pressed.
Is this question too confusing? xD
Python has a keyboard module with many features. You Can Use It In Both Shell and Console.
Install it, perhaps with this command:
pip3 install keyboard
Then use it in code like:
import keyboard #Using module keyboard
while True: #making a loop
try: #used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('up'): #if key 'up' is pressed.You can use right,left,up,down and others
print('You Pressed A Key!')
break #finishing the loop
else:
pass
except:
break #if user pressed other than the given key the loop will break
You can set it to multiple Key Detection:
if keyboard.is_pressed('up') or keyboard.is_pressed('down') or keyboard.is_pressed('left') or keyboard.is_pressed('right'):
#then do this
You Can Also Do Something like:
if keyboard.is_pressed('up') and keyboard.is_pressed('down'):
#then do this
It Also Detect Key For The Whole Windows.
Thanks.
I assume this is a gui program,
If using the built-in gui module Tkinter, you can use bind to connect a function to a keypress.
main.bind('<Up>', userUpkey)
Where userUpKey is a function defined in the current scope.