raw_input in python without pressing enter

后端 未结 8 1575
情歌与酒
情歌与酒 2020-11-22 15:41

I\'m using raw_input in Python to interact with user in shell.

c = raw_input(\'Press s or n to continue:\')
if c.upper() == \'S\':
    print \'Y         


        
8条回答
  •  孤城傲影
    2020-11-22 16:05

    Actually in the meantime (almost 10 years from the start of this thread) a cross-platform module named pynput appeared. Below a first cut - i.e. that works with lowercase 's' only. I have tested it on Windows but I am almost 100% positive that it should work on Linux.

    from pynput import keyboard
    
    print('Press s or n to continue:')
    
    with keyboard.Events() as events:
        # Block for as much as possible
        event = events.get(1e6)
        if event.key == keyboard.KeyCode.from_char('s'):
            print("YES")
    

提交回复
热议问题