xNinjaRose
xNinjaRose

Reputation: 117

How would I trigger an automatic enter key press after an Input function?

        choice = input(">>")
        if choice.lower() == "+":
            death = death0.IncreaseDeath()
        elif choice.lower() == "-":
            death = death0.DecreaseDeath()
        elif choice.lower() == "r":
            death = death0.ResetDeath()
        elif choice.lower() == "q":
            sys.exit()

I have this code, but its supposed to be a death counter, where the user should jus press + or - or r or q and it increments automatically, without the user needing to press enter. I tried keyboard.press_and_release('enter') and keyboard.add_hotkey('+', death = death0.IncreaseDeath()) but 2nd one doesn't work, and second one works AFTER input finishes and user presses Enter, and now its spamming enter on the input(which isnt what i want, i want the user to type one letter and then have it press Enter automatically. How would I do this to make it happen so user doesnt need to press "Enter" after the input

if msvcrt.kbhit():
            key_stroke = msvcrt.getch()
            if key_stroke == b'+':
                death = death0.IncreaseDeath()
            elif key_stroke == b'-':
                death = death0.DecreaseDeath()
            elif key_stroke == b'r':
                death = death0.ResetDeath()
            elif key_stroke == b'q':
                sys.exit()

Also tried this BUT my code before which is:

def DeathCount(death,DEATH_NAME):
    while True:
        os.system('cls' if os.name == 'nt' else 'clear')
        print ("####################")
        print (f"  {DEATH_NAME}: {death} ")
        print ("####################")
        print ("    * + : increase *      ")
        print ("    * - : decrease *      ")
        print ("    * r : reset    *      ")
        print ("    * q : quit     *      ")
        print ("####################")

doesn't show up and it gives no clue to the user what to press and whether its actually incrementing or not.

Upvotes: 0

Views: 953

Answers (1)

Shoaib Mirzaei
Shoaib Mirzaei

Reputation: 522

hope this code helps

import sys
from getkey import getkey
death0 = 15
death = death0
print(death)
while True:
    print(">>")
    choice = getkey()
    if choice.lower() == "+":
        death += 1
    elif choice.lower() == "-":
        death -= 1
    elif choice.lower() == "r":
        death = death0
    elif choice.lower() == "q":
        sys.exit()
    else:
        print("invalid input")
    print(death)

Upvotes: 1

Related Questions