0

i'm making a small app with python run on terminal

and i would like to ask for easy way to make options Exmaple when user choose "a" it run add skill directly without pressing enter

Please advise your action :-
    "A" => Add Skill.
    "S" => Show Skills.
    "U" => Update Skill.        
    "D" => Delete Skill.        
    "Q" => Quite App.
while True:  # looping to validate user input
        text_asking = input("""***Welcome to Skill Manager***
    Please advise your action :-
    "A" => Add Skill.
    "S" => Show Skills.
    "U" => Update Skill.
    "D" => Delete Skill.
    "Q" => Quite App.
    Choose Action :""").lower().strip()
        if text_asking in ("a", "s", "u", "d", "q"):
            break
        else:
            os.system("cls")
            print("Invalid input >> Please advise correct action \n\n")
            continue
    if text_asking == "a":
        add_skill()
    elif text_asking == "s":
        show_skill()
    elif text_asking == "u":
        update_skill()
    elif text_asking == "d":
        delete_skill()
    elif text_asking == "q":
        quite()

i tried keyboard lib but didn't able to make my idea

dimani128
  • 75
  • 7

1 Answers1

0

using pynput looks like it does the trick:

see answer from Prakash Palnati here: What's the simplest way of detecting keyboard input in a script from the terminal?

so, I found another way based on keyboard that seems to do a better work, added to the warning in the comment of the answer above, I would finally not counsel to use pynput...

here is a working partial implementation:

import keyboard  # using module keyboard

def print_menu():
    print(
        """***Welcome to Skill Manager***
        Please advise your action :-
        "A" => Add Skill.
        "S" => Show Skills.
        "U" => Update Skill.
        "D" => Delete Skill.
        "Q" => Quite App.
        Choose Action :"""
    )

def add_skill():
    print("skill added")

def show_skill():
    print("skill list:")
    for skill in range(10):
        print(f"- skill {skill}")

def quit():
    print("application will close")
    exit()

print_menu()
while True:
    # Wait for the next event.
    event = keyboard.read_event()
    if event.event_type == keyboard.KEY_DOWN:
        print(event.name)
        if event.name == "a":
            add_skill()
        elif event.name == "s":
            show_skill()
        elif event.name == "q":
            quit()
        else:
            print("Invalid input >> Please advise correct action \n\n")
            print_menu()

I followed the advices on the github page of the keyboard project, found here: https://github.com/boppreh/keyboard

PandaBlue
  • 331
  • 1
  • 6