首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby中的Window getch vs Curses getch

Ruby中的Window getch vs Curses getch
EN

Stack Overflow用户
提问于 2018-08-05 22:39:45
回答 1查看 147关注 0票数 0

我目前正在学习使用Ruby和curses库,并尝试编写了一个简单的应用程序,可以接受两个字符并退出。

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

代码语言:javascript
复制
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还是Curses::getch获取输入?每一个都有优点和缺点吗?

EN

回答 1

Stack Overflow用户

发布于 2018-08-06 00:13:23

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

咒骂窗口通常比屏幕小。在curses中,作为一个特例,有一个预定义的窗口stdscr,它对应于整个屏幕。

在每种情况下(用户定义的窗口或特殊的窗口stdscr) ),您都可以使用getch以某种方式从窗口中读取。这意味着当从窗口读取时,curses会将光标移动到该窗口的当前位置(对于该窗口),并且根据选项的不同,可能会回显输入的字符。此外(由于移动和回显),curses使用refresh使窗口保持最新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51695280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档