Home > Net >  whats wrong with my script (key press, python)
whats wrong with my script (key press, python)

Time:03-22

I just wanted to make a simple script that key presses "4" seven times, if I press "4" once.

I want it to work in the background while, and work any time I key press "4"

here is what I tried

import pyautogui
import time
import msvcrt

while True:
    key = msvcrt.getch().lower()

    if key == b'4':
        for x in range(1,7):
            pyautogui.press(4)
            time.sleep(0.3)
        break

I tried to execute this on console and it didn't work. No errors pop up, but no matter how many times I press 4, it doesn't make me press 4 7 more times. Any help is appreciated.

CodePudding user response:

Try:

pyautogui.press('4')

From the documentation press takes strings, if it doesn't work you can also try:

while True:
    if msvcrt.kbhit():
        if str(msvcrt.getch()) == b'4':
            for _ in range(7):
                pyautogui.press('4')

CodePudding user response:

Try this

import keyboard
import pyautogui
import time

while True:
    if keyboard.read_key() == "4":
        for x in range(0,5):
            pyautogui.press('4')
            time.sleep(0.3)
        break
  • Related