Python 2.7 诅咒键盘(True)没有特殊的键

Python 2.7 诅咒键盘(True)没有特殊的键,python-2.7,python-3.x,linux-mint,python-curses,Python 2.7,Python 3.x,Linux Mint,Python Curses,我正在尝试为Python2和Python3编写代码。以下是我用来学习诅咒的完整代码: # -*- coding: utf-8 -*- from __future__ import print_function import curses import sys import traceback class Cursor(object): def __init__(self): self.stdscr = curses.initscr() curses.

我正在尝试为Python2和Python3编写代码。以下是我用来学习诅咒的完整代码:

# -*- coding: utf-8 -*-
from __future__ import print_function

import curses
import sys
import traceback


class Cursor(object):
    def __init__(self):
        self.stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        self.stdscr.keypad(True)

    def end_window(self):
        curses.nocbreak()
        self.stdscr.keypad(False)
        curses.echo()
        curses.endwin()

    def start_win(self, begin_x=0, begin_y=1, height=24, width=71):
        return curses.newwin(height, width, begin_y, begin_x)


def applic():
    print("yo man")
    x = Cursor()
    window = x.start_win()
    try:
        # This raises ZeroDivisionError when i == 10.
        for i in range(0, 11):
            v = i - 10
            key = window.getch()
            # EDIT - Added this debug line to verify what key gets
            window.addstr('type of {} is {}\n'.format(key, type(key)))
            if key == curses.KEY_UP:
                return
            window.addstr('10 divided by {} is {}\n'.format(v, 10 // v))
            window.refresh()

    except Exception:
        x.end_window()
        errorstring = sys.exc_info()[2]
        traceback.print_tb(errorstring)

applic()
我的问题是
key
永远不会等于
curses.key\u UP
,因为getch()(或getkey())返回的是单个字符串,而不是等于key\u UP(
\x1b]A
)的整个转义键代码。因此,每次我按下例程中的向上箭头,程序都会循环执行键代码
\x1b
[
A
,并生成三行输出:

10 divided by -10 is -1
10 divided by -9 is -2
10 divided by -8 is -2
对于这个测试,我希望向上箭头键允许我在我等于10时发生的预期异常之前爆发

根据针对Curses的python文档,
stdscr.keypad(True)
应该允许返回整个键代码,但似乎没有这样做

添加了一些调试打印输出信息以显示返回的内容。无论我使用getch()还是getkey(),结果都是一样的;它返回一个字符串(文档表明getch将返回一个整数):

<代码>类型为 10除以-9等于-2 [is]的类型 10除以-8等于-2 A型是 10除以-7等于-2
问题在于,您在主窗口上设置了
键盘
属性,但正在使用
getch
从其他窗口读取。此更改将使您的程序接受

--- curses-vs-keypad.py 2017/04/02 11:07:41     1.1
+++ curses-vs-keypad.py 2017/04/03 00:57:16
@@ -28,6 +28,7 @@
     print("yo man")
     x = Cursor()
     window = x.start_win()
+    window.keypad(True)
     try:
         # This raises ZeroDivisionError when i == 10.
         for i in range(0, 11):

问题是您在主窗口上设置了
键盘
属性,但正在使用
getch
从其他窗口读取。此更改将使您的程序接受一个

--- curses-vs-keypad.py 2017/04/02 11:07:41     1.1
+++ curses-vs-keypad.py 2017/04/03 00:57:16
@@ -28,6 +28,7 @@
     print("yo man")
     x = Cursor()
     window = x.start_win()
+    window.keypad(True)
     try:
         # This raises ZeroDivisionError when i == 10.
         for i in range(0, 11):