How to set time limit on raw_input

前端 未结 6 1521
抹茶落季
抹茶落季 2020-11-22 05:52

in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped?

6条回答
  •  孤街浪徒
    2020-11-22 06:21

    under linux one could use curses and getch function, its non blocking. see getch()

    https://docs.python.org/2/library/curses.html

    function that waits for keyboard input for x seconds (you have to initialize a curses window (win1) first!

    import time
    
    def tastaturabfrage():
    
        inittime = int(time.time()) # time now
        waitingtime = 2.00          # time to wait in seconds
    
        while inittime+waitingtime>int(time.time()):
    
            key = win1.getch()      #check if keyboard entry or screen resize
    
            if key == curses.KEY_RESIZE:
                empty()
                resize()
                key=0
            if key == 118:
                p(4,'KEY V Pressed')
                yourfunction();
            if key == 107:
                p(4,'KEY K Pressed')
                yourfunction();
            if key == 99:
                p(4,'KEY c Pressed')
                yourfunction();
            if key == 120:
                p(4,'KEY x Pressed')
                yourfunction();
    
            else:
                yourfunction
    
            key=0
    

提交回复
热议问题