Skip to content
Advertisement

program simulating keypress numbers 1 – 999

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:

    import pynput
from pynput.keyboard import Key, Controller
keyboard = Controller()
import time
x = 0
time.sleep(1)

for write in range(1000):
    keyboard.press(str(write))
    keyboard.release(str(write))
    print(str(write))
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    time.sleep(0.005)

Advertisement

Answer

press and release only accept one character. Try changing it to type:

import pynput
from pynput.keyboard import Key, Controller
keyboard = Controller()
import time
x = 0
time.sleep(1)

for write in range(1000):
    keyboard.type(str(write))  # Here, change it to .type() instead
    print(str(write))
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    time.sleep(0.005)

You can also use the pyautogui or the keyboard module.

With pyautogui

import pyautogui

# Your other stuff here

for i in range(1000):
     pyautogui.write(str(i))  # Write the number

With keyboard

import keyboard

# Your other stuff here

for i in range(1000):
     keyboard.write(str(i))  # Write the number
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement