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 Overflow
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › keyboard.html
Keyboard Control Functions — PyAutoGUI documentation
See KEYBOARD_KEYS. >>> with pyautogui.hold('shift'): pyautogui.press(['left', 'left', 'left'])
Discussions

How to make a script that will hold down a key for x seconds
I am very new to python, I have been coding in Lua for about 3 years but just the past couple of days I have been learning python, anyways I am trying to make a script that will hold down the “e” key on my keyboard for x… More on discuss.python.org
🌐 discuss.python.org
2
0
December 12, 2022
python - How to press down a key? - Stack Overflow
Been using the pyautogui module to do most of my things, but I have come across one problem: I cannot hold down a key for a certain length of time. Does anyone know any modules that can do this, or More on stackoverflow.com
🌐 stackoverflow.com
Is there anyway to press and hold 2 key at the SAME time
I haven't used pyautogui, but try this: Look at the documentation on how to hold keys inside a with block: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hold-context-manager Use Import time and time.sleep(number_of_seconds) inside the with block. More on reddit.com
🌐 r/learnpython
1
1
October 3, 2022
pyautogui - How can I hold down the keyboard key with python - Stack Overflow
Im using the PyAutoGui module, but I cant do a keyboard key hold. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › press holding any key for 5 second is not working
r/learnpython on Reddit: Press holding any key for 5 second is not working
May 1, 2024 -

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?

🌐
CodersLegacy
coderslegacy.com › home › python › pyautogui keyboard automation
PyAutoGUI Keyboard Automation - CodersLegacy
May 25, 2022 - In this tutorial for PyAutoGUI, we will discuss Keyboard Automation and how we can effectively manipulate keyboard presses and clicks.
🌐
Read the Docs
pyautogui.readthedocs.io
Welcome to PyAutoGUI’s documentation! — PyAutoGUI documentation
The hold() Context Manager · The hotkey() Function · KEYBOARD_KEYS · Message Box Functions · The alert() Function · The confirm() Function · The prompt() Function · The password() Function · Screenshot Functions · The screenshot() Function · The Locate Functions · Testing · Platforms Tested · Roadmap · pyautogui ·
🌐
Python.org
discuss.python.org › python help
How to make a script that will hold down a key for x seconds - Python Help - Discussions on Python.org
December 12, 2022 - I am very new to python, I have been coding in Lua for about 3 years but just the past couple of days I have been learning python, anyways I am trying to make a script that will hold down the “e” key on my keyboard for x…
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › is there anyway to press and hold 2 key at the same time
r/learnpython on Reddit: Is there anyway to press and hold 2 key at the SAME time
October 3, 2022 -

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.

🌐
GitHub
github.com › asweigart › pyautogui › blob › master › docs › keyboard.rst
pyautogui/docs/keyboard.rst at master · asweigart/pyautogui
To make holding a key convenient, the hold() function can be used as a context manager and passed a string from the pyautogui.KEYBOARD_KEYS such as shift, ctrl, alt, and this key will be held for the duration of the with context block.
Author   asweigart
🌐
OpenGenus
iq.opengenus.org › python-script-to-control-keyboard
Python script to control keyboard
March 23, 2022 - import pyautogui import time # 5 seconds cooldown to allow user to terminate the program by moving the mouse to one of the 4 corners time.sleep(5) # make the text editor window active pyautogui.click(300,1060) pyautogui.keyDown('shift') pyautogui.press('2') pyautogui.keyUp('shift') pyautogui.press('2') We can achieve the same result as above by using with pyautogui.hold('shift'): statement.
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › quickstart.html
Cheat Sheet — PyAutoGUI documentation - Read the Docs
This is a quickstart reference to using PyAutoGUI. PyAutoGUI is cross-platform GUI automation module that works on Python 2 & 3. You can control the mouse and keyboard as well as perform basic image recognition to automate tasks on your computer.
🌐
Reddit
reddit.com › r/learnpython › having trouble with pyautogui
r/learnpython on Reddit: having trouble with pyautogui
June 23, 2021 - Right in the documentation, there is this: For example, to press the left arrow key three times while holding down the Shift key, call the following: pyautogui.keyDown('shift') # hold down the shift key pyautogui.press('left') # press the left ...
🌐
Read the Docs
pyautogui.readthedocs.io › _ › downloads › en › latest › epub epub
PyAutoGUI documentation
To make holding a key convenient, the hold() function can be used as a context manager and passed a string from the pyautogui.KEYBOARD_KEYS such as shift, ctrl, alt, and this key will be held for the duration of the with context block.
🌐
GitHub
github.com › asweigart › pyautogui › issues › 600
use alt+numpad to input character got nothing? · Issue #600 · asweigart/pyautogui
July 14, 2021 - `import pyautogui pyautogui.keyDown('alt') pyautogui.press(['num1', 'num0', 'num7']) pyautogui.keyUp('alt')` If I holding down the alt key then enter 1,0,7 u...
Author   lionwong
🌐
Read the Docs
buildmedia.readthedocs.org › media › pdf › pyautogui › latest › pyautogui.pdf pdf
PyAutoGUI Documentation Al Sweigart Sep 14, 2021
To make holding a key convenient, the hold() function can be used as a context manager and passed a string from · the pyautogui.KEYBOARD_KEYS such as shift, ctrl, alt, and this key will be held for the duration of the
🌐
Reddit
reddit.com › r/learnpython › how to press keys with pyautogui?
r/learnpython on Reddit: How to press keys with pyautogui?
February 5, 2018 -

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.

🌐
Automate the Boring Stuff
automatetheboringstuff.com › 3e › chapter23.html
Chapter 23 - Controlling the Keyboard and Mouse, Automate the Boring Stuff with Python, 3rd Ed
They are passed a keyboard key ... complete key press. Run the following code, which will type a dollar sign ($) character (obtained by holding the SHIFT key and pressing 4):...