为什么当我运行此代码时box.getch()
返回错误的值,而当我更改box.getch()
为screen.getch()
它时返回正确的值?我一直在网上寻找,没有人说getch()
只适用于屏幕。如果您按下其中一个箭头,它将返回 27,这是 ESC 的字符。(此代码应打印字符,直到用户按 ESC ...)
import curses
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair( 1 )
normalText = curses.A_NORMAL
screen.border( 0 )
curses.curs_set( 0 )
box = curses.newwin( 22, 64, 1, 1 )
box.box()
box.addstr( 14, 3, "YOU HAVE PRESSED: ")
screen.refresh()
box.refresh()
x = box.getch()
while x != 27:
box.erase()
box.addstr( 14, 3, "YOU HAVE PRESSED: " + str(x) )
screen.border( 0 )
box.border( 0 )
screen.refresh()
box.refresh()
x = box.getch()
curses.endwin()
exit()