简体   繁体   English

暂停 python 脚本等待按键

[英]Pause python script wait for key press

I have a relatively simple script and a potentially simple question.我有一个相对简单的脚本和一个可能很简单的问题。 Around the inte.net I've gathered a few solutions on how to use direct key press' as input in my python code.在 inte.net 周围,我收集了一些关于如何在我的 python 代码中使用直接按键作为输入的解决方案。 I'd prefer these to raw_input as it feels faster.我更喜欢这些而不是 raw_input,因为它感觉更快。 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.就像我有一个包含 3 个选项的菜单,每个菜单中有 3 个选项,我可以轻松地按键盘上的 3 然后按 2 以到达我需要的位置 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.由于某种原因,我不能使用time.sleep() 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.如果我能得到一个 function 就太棒了,这样我就可以在其他地方输入它了。

If I didn't have my else statement at the end, the while loop just keeps going.如果最后没有我的 else 语句,while 循环就会继续运行。
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.如果我在 while 循环中没有它,脚本会在 1 秒内打印选项,然后默认为 else,因为我在 1 秒内没有按下任何键。
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.我知道它有效,因为如果我取出 else 语句,当它一遍又一遍地向选项发送垃圾邮件时,我按 3,IDLE 会询问我是否要在脚本运行时终止它。

You can use input() to have the same effect. 您可以使用input()获得相同的效果。

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()

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. 使用keyboard.read_key() ,因为它将阻止其余代码的执行,直到发生键盘事件为止,然后返回该事件的名称或(如果缺少)其扫描代码。

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()

import time导入时间

def mainmenu(): while(True): print ('1. Scan') print ('2. Ping') print ('3. Exit') 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()主菜单()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM