简体   繁体   English

如何使用 Python 程序向终端发送键盘中断 ctrl+]?

[英]How can I send a keyboard interrupt ctrl+] to the terminal using a Python program?

I'm currently working on a ESP32 project in which there is a command named 'idf.py monitor'.我目前正在开发一个 ESP32 项目,其中有一个名为“idf.py monitor”的命令。 This command outputs strings into the bash only after the keyboard interruption 'ctrl+]'.此命令仅在键盘中断“ctrl+]”后才将字符串输出到 bash。

It's annoying to finish the program by tabbing 'ctrl+]' everytime.每次都按“ctrl+]”来完成程序很烦人。 So that I wrote a python script to execute the command automatically.所以我写了一个 python 脚本来自动执行命令。

import os
command='idf.py monitor'
r=os.popen(command) #execute the command
context=r.readlines() # get the command output
print(context)

However, the 'print' function only works after the key interrupt 'ctrl+]'.但是,“打印”function 仅在按键中断“ctrl+]”后有效。

So my question is: How can I write a python script that can send the keyinterrupt 'ctrl+]' to my shell automatically?所以我的问题是:如何编写一个 python 脚本,它可以自动将 keyinterrupt 'ctrl+]' 发送到我的 shell ?

(I tried using process.kill() or process.terminate() which send 'ctrl+c', but that doesn't work) (我尝试使用 process.kill() 或 process.terminate() 发送'ctrl+c',但这不起作用)

You can use a module named as keyboard您可以使用名为键盘的模块

  1. pip install keyboard
 import keyboard keyboard.press_and_release('ctrl+]')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM