简体   繁体   English

我如何询问用户的输入并在用户键入时分析输入?

[英]How do I ask an input from a user and analyse the input as the user types it?

I'm building a particular program in python that requires an input from the user but that input has to be simultaneously analyzed as the user types.我正在 python 中构建一个特定的程序,该程序需要用户输入,但必须同时分析该输入作为用户类型。
How do I achieve this?我如何实现这一目标?
The user specifies the input through the command line.用户通过命令行指定输入。

On Windows you can get the input from the user using msvcrt.getch() instead of input() .在 Windows 上,您可以使用msvcrt.getch()而不是input()从用户那里获取输入。 This way you can process the input as it is being typed.这样,您可以在输入时处理输入。

import msvcrt

def process_key(key):
    # ADD YOUR CODE
    print("processed:", key)

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

On Linux you can use getch in a similar way.在 Linux 上,您可以以类似的方式使用getch

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

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