首页 > 解决方案 > 如何通过 pexpect 将控制(按键)发送到 ncurses 应用程序

问题描述

我有用 ncurses 编写的 Linux 应用程序。我正在尝试使用 pexpect 使其自动化,但没有成功。

我可以生成一个应用程序并且可以使用输出,但我不能发送向下箭头键:

import pexpect
import time 
import sys, os

os.environ['LINES'] = "25"
os.environ['COLUMNS'] = "80"

child=pexpect.spawn("my_ncurses_app", maxread=4000, encoding="utf-8")
child.logfile=sys.stdout
child.setwinsize(25,80)

KEY_DOWN = '\033[B'

#close button appears on screen, After that I want to press down key twice and enter
child.expect("Close")

#ncurses_app sees KEY_DOWN as 3 different keys \033, [, B
child.send(KEY_DOWN)
child.send(KEY_DOWN)

child.sendline()
#ncurses_app sees enter as Int(10)

它适用于其他 CLI 应用程序,但不适用于我的。

调试显示应用程序看到的不是 1 个向下箭头符号,而是 3 个不同的键。

如何将 KEY_DOWN 作为一个符号发送?可能我应该使用 smth other 而不是 pexpect,与进程进行低级交互的 smth?

标签: automationcommand-line-interfacencursespexpect

解决方案


推荐阅读