将整数转换为键

Translating a integer to a key

我在 curses 中有一个非常基本的终端 UI,如下所示:

│Option:                       Status:   Key: 
│description of option 1       [ON]       <267>
│description of option 2       [OFF]      <102>

该键是您必须按下才能更改状态的键。在 curses 模块(或一般情况下)中,我如何将关键整数转换为名称?

如果我的密钥是 ord('f'),即 102,我可以使用 chr(102) 在屏幕上打印 'f'。但是,如果我的密钥是 curses.KEY_F3,即 267,我会得到 chr(267) 的有趣符号。如何仅从整数 267 打印 'F3'?

这是使用 chr() 复制的输出,但在控制台中有一个带有 ?在 Ä

后面
│Option:                       Status:   Key:
│description of option 1       [ON]       <Ä>
│description of option 2       [OFF]      <f>

看来你需要curses.keyname

curses.keyname(k) Return the name of the key numbered k. The name of a key generating printable ASCII character is the key’s character. The name of a control-key combination is a two-character string consisting of a caret followed by the corresponding printable ASCII character. The name of an alt-key combination (128–255) is a string consisting of the prefix ‘M-‘ followed by the name of the corresponding ASCII character.