so I have this program where what I want it to do is simulate keypress of numbers from 1 – 999 but it does not seem to work and it stopped working when it is supposed to type the number 10 and I am not sure how to fix it code:
JavaScript
x
15
15
1
import pynput
2
from pynput.keyboard import Key, Controller
3
keyboard = Controller()
4
import time
5
x = 0
6
time.sleep(1)
7
8
for write in range(1000):
9
keyboard.press(str(write))
10
keyboard.release(str(write))
11
print(str(write))
12
keyboard.press(Key.enter)
13
keyboard.release(Key.enter)
14
time.sleep(0.005)
15
Advertisement
Answer
press
and release
only accept one character. Try changing it to type
:
JavaScript
1
14
14
1
import pynput
2
from pynput.keyboard import Key, Controller
3
keyboard = Controller()
4
import time
5
x = 0
6
time.sleep(1)
7
8
for write in range(1000):
9
keyboard.type(str(write)) # Here, change it to .type() instead
10
print(str(write))
11
keyboard.press(Key.enter)
12
keyboard.release(Key.enter)
13
time.sleep(0.005)
14
You can also use the pyautogui
or the keyboard
module.
With pyautogui
JavaScript
1
7
1
import pyautogui
2
3
# Your other stuff here
4
5
for i in range(1000):
6
pyautogui.write(str(i)) # Write the number
7
With keyboard
JavaScript
1
7
1
import keyboard
2
3
# Your other stuff here
4
5
for i in range(1000):
6
keyboard.write(str(i)) # Write the number
7