Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - python sys.stdin()无需按回车键(python sys.stdin() without pressing enter)

I need to have my program ask the user to enter a letter for it to continue in one direction or the other (I'm making a state machine), but my terminal only registers the answer if the user presses "enter" after entering the letter.

(我需要让我的程序要求用户输入一个字母,以便它在一个方向或另一个方向上继续(我正在制造状态机),但是如果用户在输入密码后按“ enter”(进入),则终端仅注册答案。信件。)

From what I was able to find, I need to be either in raw mode or in cbreak mode to remedy this problem, but no matter what I do I can't seem to find a functionning solution.

(根据我的发现,我需要处于原始模式或cbreak模式才能解决此问题,但是无论我做什么,我似乎都找不到一个可行的解决方案。)

Right now, as a functionning code I have that but it does ask for the user to press "enter".

(现在,作为功能代码,我已经知道了,但是它确实要求用户按下“输入”。)

def interact():  

    global NextState

    while isData() == False :
        isData()

    isData()
    #si une touche est appuyee
    if isData():
        c = sys.stdin.read(1)
        if c == 'a': 
            NextState = 1
        elif c=='b' :
            NextState = 2
        else:
             showH()
             showTxt("Wrong input, please enter 'a' or 'b'")
             interact()
    return


def isData():


    return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

And also, I can't seem to get my code out of the cbreak mode or raw mode when I actually do something that gets closer to working.

(而且,当我实际上做一些接近工作的事情时,我似乎无法使代码脱离cbreak模式或原始模式。)

If you could help me solve that problem without changing too much of the code (so that I don't get lost since I'm new at coding), it would really help me.

(如果您可以在不更改太多代码的情况下帮助我解决该问题(以免我因为编码方面的新手而不会迷路),那么它将对我有帮助。)

  ask by Dat_Bucket translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...