As said in the doc-string from pyautogui.keyDown():
Performs a keyboard key press without the release. This will put that key in a held down state.
NOTE: For some reason, this does not seem to cause key repeats like would happen if a keyboard key was held down on a text field.
You need a different approach - you can may use pygame - with this
Or, if you want to stay with pyautogui you can try something like this:
def hold_W (hold_time):
import time, pyautogui
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
Answer from Skandix on Stack OverflowAs said in the doc-string from pyautogui.keyDown():
Performs a keyboard key press without the release. This will put that key in a held down state.
NOTE: For some reason, this does not seem to cause key repeats like would happen if a keyboard key was held down on a text field.
You need a different approach - you can may use pygame - with this
Or, if you want to stay with pyautogui you can try something like this:
def hold_W (hold_time):
import time, pyautogui
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
with pyautogui.hold(key):
pyautogui.sleep(hold)
This will do the trick without making your own function.
How to make a script that will hold down a key for x seconds
python - How to press down a key? - Stack Overflow
Is there anyway to press and hold 2 key at the SAME time
pyautogui - How can I hold down the keyboard key with python - Stack Overflow
Videos
I basically want my code to press hold the key R for 5 seconds. For this I am using pyautogui library. The code I have written so far is given below:
import pyautogui
import time
def main():
Initialised PyAutoGUI
pyautogui.FAILSAFE = True # PyAutoGui will stop any automation if the mouse cursor is moved to one of the four corners of the screen. This is a safety measure.
Countdown Timer, this will count for 5 seconds before pressing the R key
print("Starting", end="", flush=True)
for i in range (0, 5):
print(".", end="", flush=True)
time.sleep(1)
print("Go")
Press hold command
pyautogui.keyDown('r') # Press the 'r' key
time.sleep(5) # Hold for 5 second
pyautogui.keyUp('r') # Release the 'r' key
Done
print("Done")
if __name__ == "__main__":
main()
I think the bold part is problematic, though I don't know what is happenning. The code thinks it is running perfectly, but in reality, after counting 5 seconds to start, my code just auto prints 1 (one) "r" letter, and then goes idle for 5 seconds (sleep function), and then prints done, instead of press holding r for 5 seconds. I was expecting a result such as "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" to be honest. I also tried using a different library, which is "keyboard", and replaced the bold part with this:
keyboard.press('r') # Press the 'r' key
time.sleep(5) # Hold for 5 secons
keyboard.release('r') # Release the 'r' key
It still prints 1 (one) "r" letter, and then does nothing for 5 seconds, and then prints done. Maybe I am using the sleep function wrong? Or maybe my code requires additional commands? Can someone explain me what I am doing wrong?
I want pyautogui.keyDown at the same time with 'a' and 'k' and release them together I tried this but understandably there is super small delay between each keyDown
pyautogui.keyDown('a')
pyautogui.keyDown('k')
I tried pyautogui.hotkey but it will instantly press nad release those keys I cant use it as I want to keep keys pressed for a certain period of time.
pyautogui.keyDown('winleft')
pyautogui.press('r')
pyautogui.keyUp('winleft')
try running this code, confirmed 100% working, should bring up the ''run'' window
Taken from my own script, you can always use the
time.sleep(1)
function to make it hold for however long you need, just remember to
import time
at the start of your code (at least that's how i made it work)
import time
import pyautogui
pyautogui.keyDown('d')
time.sleep(x) # Insert time
pyautogui.keyUp('d)
This is a foolproof method of imitating "holding" down a key. I recommend making a function for this just because pyautogui is most likely not adding a time-based hold function.
To be exact function and arrow type of keys. I am not even a beginners in Python but some how I ended up coming a small part of my project in Python, it will be really helpful if someone could provide with a cheat sheet for names to use to call actual keys, I found most of the key names and they are pretty same but I am unable to find the pyautogui name for the FUNCTION key.
The hotkey() function only works with keys. To shift-click, you need code like this:
import pyautogui
pyautogui.keyDown('shift')
pyautogui.click()
pyautogui.keyUp('shift')
I tried the solution above but for some reason keyDown('shift') didnt work. I used the solution in this thread: keyDown function not working with shift key
pyautogui.keyDown('shiftleft')
pyautogui.keyDown('shiftright')
pyautogui.hotkey('right','right','ctrl','up')
pyautogui.keyUp('shiftleft')
pyautogui.keyUp('shiftright')
Hi I think you will need a different approach to solving this issue.
Also i don't advice using .keyDown() or .keyUp(). Instead, use .press()
Here's a code snippet to hold down a key for 5 seconds.
import time, pyautogui
start = time.time()
while time.time() - start < 5: #Hold Key for 5 Seconds
pyautogui.press('d')
So you would need to change this flow to break out of the while loop as soon as it finds the Fertilizer.png
Like such:
import time, pyautogui
start = time.time()
while time.time() - start < 5: #Hold Key for 5 Seconds
pyautogui.press('d')
if pyautogui.locateOnScreen("Fertilizer.png", confidence=1.0) == True:
break
This should not create infinte loops, however you will have to rearrange the code to make it work for you.
import pyautogui
import keyboard
import asyncio
import time
async def wait4keystroke():
while True:
if keyboard.is_pressed('YOUR KEY HERE') == True :
return
async def main():
while True :
keystroke = asyncio.create_task(wait4keystroke())
await keystroke
pyautogui.keyDown(key='YOUR KEY HERE')
time.sleep(2)
pyautogui.keyUp(key='YOUR KEY')
asyncio.run(main())`
I think you can just add your locate on screen condition where the keyboard.is_pressed condition is. Just add an and statement.