Reputation: 81
I'm using pynput and I would like to be able to hold keys down, specifically wasd but when I try and run this code it only presses the key and doesn't hold it for 2 seconds. If anyone knows what I'm doing wrong let me know. Thanks
import time
keyboard = Controller()
time.sleep(2)
keyboard.press('w')
time.sleep(2)
keyboard.release('w')
Upvotes: 8
Views: 19190
Reputation: 1
So I don't have enough to post the actual solution but I figured it out in a few minutes this works for python 3.10 and up.
keyboard = Controller()
key = new_word
try:
key_start = 0
key_end = 400
while key_start < key_end:
time.sleep(0.025)
keyboard.press(key)
key_start += 1
keyboard.release(key)
then just wrap that in a try/except. that will hold the key down without lifting the "finger" off the key for 10 seconds The problem with the solutions above is that it fails to account for the fact that once 'keyboard.press(key)' is initialized you can't use time.sleep(x) nor can you use a custom countdown function so what has to be done is to figure out the speed in which a keyboard registers once it is held down. I did this with twitch chat because it has a set limit of 500 characters and then divided 500/t where t= time and held down a key manually to get to roughly .025 seconds. this has to be initialized at the beginning of the while loop. once it leaves the while loop there is no time.sleep() between the initialization of keyboard.press(key) and keyboard.release(key) thus allowing it to be 'held" down in the manner that you are looking for. I hope this helped
Upvotes: 0
Reputation: 23
Use the keyboard Python module to hold down keys (works with most games) https://github.com/boppreh/keyboard
Upvotes: 1
Reputation: 31
Try to hold your keys by using the "with" statement. In my example it holds "alt" and tabs around.
import time
from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.alt):
keyboard.press(Key.tab)
time.sleep(1)
keyboard.press(Key.tab)
time.sleep(1)
keyboard.press(Key.tab)
time.sleep(1)
Upvotes: 3
Reputation: 21
You could make a two second loop. (I don't have enough reputation to comment.)
Upvotes: 2
Reputation: 133
Maybe try PyAutoGui. It is easier, and can be used within a few lines of code. I got the code from here
>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> currentMouseX, currentMouseY = pyautogui.position()
>>> pyautogui.moveTo(100, 150)
>>> pyautogui.click()
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
>>> pyautogui.press('esc')
>>> pyautogui.keyDown('shift')
>>> pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
>>> pyautogui.keyUp('shift')
>>> pyautogui.hotkey('ctrl', 'c')
If you want to just press a key down then do
from pyautogui import*
from time import sleep
keyDown("a") #pressing down key 'a'
sleep() #how ever long you want
keyUp("a") #stop pressing key 'a' down
Hope this helps.
Upvotes: 1