代码之家  ›  专栏  ›  技术社区  ›  Ricardo Iglesias

窗口getch与Ruby中的Curses getch

  •  0
  • Ricardo Iglesias  · 技术社区  · 8 年前

    我目前正在学习使用Ruby和curses库,我试着制作一个简单的应用程序,它接受两个字符并退出。但是,对于对象的“窗口”实例和Curses实例之间的区别,我有点困惑。

    例如,下面的脚本试图以两种方式使用库的“getch”功能:一种是使用 Curses::getch $window.getch

    def init_curses()
      Curses::init_screen
      win = Curses::Window.new( Curses.lines, Curses.cols, #Set window to be as large as terminal window
                                0, 0) #Start window on top-left corner
      win.clear
      return win
    end
    
    $window = init_curses
    input_ch = nil
    
    begin
      Curses::crmode
      $window.addstr(INPUT_STR)
    
      # Changing the order of these two lines
      # changes what I see on the screen
      Curses::refresh
      $window.refresh
    
      # Read characters using different methods
      input_ch = $window.getch
      input_ch = Curses::getch
    ensure 
      $window.clear #Prevent output after exiting
      $window.close
      Curses::close_screen
    end
    

    当我将调用顺序更改为刷新时(现在调用 $window.refresh 之前 Curses::refresh

    这是为什么?

    另外,是否有一些获得输入的首选方法?我应该通过 window.getch 或 ? 它们各有利弊吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Thomas Dickey    8 年前

    Ruby的curses只是C curses库的一个接口。

    诅咒 屏幕 . 在curses中,作为一种特殊情况,有一个预定义的窗口 stdscr 对应于整个屏幕。

    窗口 标准差减 getch 在某种程度上读“从”窗口。这意味着,当从窗口读取时,光标会将光标移动到当前位置(该窗口)的窗口中,并且根据选项的不同,可能会回显输入字符。另外(由于移动和回声),curses使用 refresh