Python - 检测终端屏幕保护程序中的输入

时间:2017-01-03 11:31:24

标签: python curses screensaver

我正在编写一个代码,基本上为了移动性和功能而没有循环运行。让我们说当我运行程序时,我使用-i选项继续使用python解释器,因为它加载了所有写入的函数。事情是,我在程序打印一些基本信息后使用类似功能的屏幕保护程序,使其看起来不那么无聊。

我的问题是:如何在不阻止解释器提示的情况下保持屏幕保护程序运行。我已经写过如何处理键盘输入以使用curses“getch()”来停止它。我尝试使用线程,但没有用,因为它没有检测到任何键盘输入,所以它一直在运行。

有什么方法可以解决这个问题吗?如何在不阻塞的情况下检测任何输入,从而保留解释器提示符?

知道它检测到一个输入并引发一个KeyboardException,以使屏幕保护程序停止。

提前致谢

我正在使用此代码并添加一些修改: Matrix-Curses

到目前为止我所拥有的: Completed Code

这是对代码所做的修改:

def clear(int=None):
   """ Clear Terminal Screen """

   from subprocess import call  
   call('clear')
   if int == 0:
      exit()

def Matrix():
   steps = 0
   global scr
   curses.curs_set(0)
   curses.noecho()

   if USE_COLORS:
      curses.start_color()
      curses.use_default_colors()
      curses.init_pair(COLOR_CHAR_NORMAL, curses.COLOR_GREEN, curses.COLOR_BLACK)
      curses.init_pair(COLOR_CHAR_HIGHLIGHT, curses.COLOR_WHITE, curses.COLOR_GREEN)
      curses.init_pair(COLOR_WINDOW, curses.COLOR_GREEN, curses.COLOR_GREEN)

   height, width = scr.getmaxyx()    
   window_animation = None
   lines = []
   for i in range(DROPPING_CHARS):
       l = FallingChar(width, MIN_SPEED, MAX_SPEED)
       l.y = randint(0, height-2)
       lines.append(l)

   scr.refresh()
   while True:
      height, width = scr.getmaxyx()
      for line in lines:
         line.tick(scr, steps)
      for i in range(RANDOM_CLEANUP):
          x = randint(0, width-1)
          y = randint(0, height-1)
          scr.addstr(y, x, ' ')
      if randint(0, WINDOW_CHANCE) == 1:
         if window_animation is None:
            #start window animation
            line = random.choice(lines)
            window_animation = WindowAnimation(line.x, line.y)
      if not window_animation is None:
         still_active = window_animation.tick(scr, steps)
         if not still_active:
            window_animation = None

      scr.refresh()
      time.sleep(SLEEP_MILLIS)
      if SCREENSAVER_MODE:
         key_pressed = scr.getch() != -1
         if key_pressed:
            raise KeyboardInterrupt

      steps += 1

def ScreenSaver():

   from time import sleep
   from datetime import datetime as dt

   global scr

   TimeLimit = 10
   StartTime = dt.now()

   while True:
      try:
         sleep(1)
         StopTime = (dt.now() - StartTime)
         LastTime = StopTime.days*86400000 + StopTime.seconds*1000 + StopTime.microseconds/1000

         if LastTime >= TimeLimit:
            GetLocaleStatus = locale.getlocale()
            locale.setlocale(locale.LC_ALL, '')
            scr = curses.initscr()
            scr.nodelay(1)

            key_being_pressed = scr.getch() != -1
            if not key_being_pressed:
               try:
                  Matrix()
               except KeyboardInterrupt:  
                  TimeLimit = 30000  
                  StartTime = dt.now()

                  raise KeyboardInterrupt 

      except KeyboardInterrupt:
         curses.endwin()
         curses.curs_set(1)
         curses.reset_shell_mode()
         curses.echo()
         clear()

         # return
         main()

0 个答案:

没有答案