2

I have a relatively simple script and a potentially simple question. Around the internet I've gathered a few solutions on how to use direct key press' as input in my python code. I'd prefer these to raw_input as it feels faster. Like if I have a menu with 3 options, and 3 options in each of those, I can easily press 3 then 2 on my keyboard to get where I need to go.

The code is:

import keyboard
import time

def mainmenu():
    while(True):
        print ('1. Scan')
        print ('2. Ping')
        print ('3. Exit')

        if keyboard.is_pressed('1'):
            print ('Option 1\n')
        elif keyboard.is_pressed('2'):
            print ('Option 2\n')
        elif keyboard.is_pressed('3'):
            print ('Exiting\n')
            exit(0)
        else:
            print ('none of the specified options were chosen')
            time.sleep(0.3)
            exit(0)
mainmenu()

I just want something that will pause the code where I can press a key.
I can't use time.sleep() for some reason. It doesn't like inputs in the split second before or after it either.
It would be awesome if I could get a function to do it so I could input it elsewhere along the way.

If I didn't have my else statement at the end, the while loop just keeps going.
If I don't have it in a while loop the script prints the options in 1 second and then defaults to else, because I haven't pressed a key in the 1 second.
I know it works because if I take out the else statement and while its spamming the options over and over I press 3, IDLE asks if I'd like to kill the script while its running.

1
  • 1
    Of course I had a more than just a quick Google. If you read my question, I don't want to input my code, I want it to read key press. The problem with that is it doesn't pause to wait for key press. But now it does, so don't worry. Commented Jun 19, 2018 at 0:58

3 Answers 3

4

Use keyboard.read_key() as it will block the execution of the rest of code until a keyboard event happens, then returns that event's name or, if missing, its scan code.

import keyboard
import time

def mainmenu():
    print ('1. Scan')
    print ('2. Ping')
    print ('3. Exit')

    while(True):
        a = keyboard.read_key()

        if a == '1' or a == '2':
            print("Option {} was pressed\n".format(a))
        elif a == '3':
            print("Exiting\n")
            exit(0)
        else:
            print("None\n")
            exit(0)

        time.sleep(0.3)

mainmenu()
Sign up to request clarification or add additional context in comments.

Comments

2

You can use input() to have the same effect.

import time

def mainmenu():
    while(True):
        print ('1. Scan')
        print ('2. Ping')
        print ('3. Exit')

        x= input()
        if x==1:
            print ('Option 1\n')
        elif x==2:
            print ('Option 2\n')
        elif x==3:
            print ('Exiting\n')
            exit(0)
        else:
            print ('none of the specified options were chosen')
            time.sleep(0.3)
            exit(0)
mainmenu()

2 Comments

As OP mentioned that he does not want to use raw_input, which like input, required addition press of return (two keystrokes instead of just one).
Yeah, Negi did you read my question? Also I submitted an edit on your code, as it requires the if/elif statements to look more like if x==1: than if x == '1':
-1
import time

def mainmenu():
    while(True):
        print ('1. Scan')
        print ('2. Ping')
        print ('3. Exit')

        x=input()
        if x=='1':
            print ('Option 1\n')
        elif x=='2':
            print ('Option 2\n')
        elif x=='3':
            print ('Exiting\n')
            exit(0)
        else:
            print ('none of the specified options were chosen')
            time.sleep(1)
            
mainmenu()

2 Comments

input() take whatever we type as string or we can casting like this x=int(input()) then we type integer for valuation as x==1 e.i.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.