automation - 如何通过 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?
解决方案
推荐阅读
- c++ - 使用 FFmpeg API 创建的 MP4 无法在媒体播放器中播放
- javascript - 无法通过 Postman (nodejs/express) 在请求正文中找到 json
- r - 如何通过特定的 ID 号删除数据框中的行?
- python - 如何构建一个numpy矩阵(从头开始,以前不存在)在for循环中添加计算列
- javascript - 这些 WHILE 的更好、更紧凑的代码?:) (原版 JavaScript)
- javascript - 递归函数的意外结果
- c - C中的数据(反)序列化
- vim - 找不到 vim 替代模式的良好语法
- riscv - 运行 RISC-V Rocket 芯片仿真器失败
- google-cloud-functions - 在 Google Cloud 函数上使用节点部署 gRPC 时出错