Python-对自定义键盘中断做出反应 [英] Python - react to custom keyboard interrupt

查看:105
本文介绍了Python-对自定义键盘中断做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写通过控制台显示输出的python chatbot。每隔半秒,它就会请求服务器进行更新,并响应消息。在控制台中,我可以看到聊天记录。

I am writing python chatbot that displays output through console. Every half second it asks server for updates, and responds to message. In the console I can see chat log.

在大多数情况下,这已经足够了,但是,有时我想中断正常的工作流程并编写自定义的聊天回答。我希望能够按下将切换到自定义回复模式的按钮(或组合键)。做到这一点或达到相似结果的最佳方法是什么?

This is sufficient in most cases, however, sometimes I want to interrupt normal workflow and write custom chat answer myself. I would love to be able to press a button (or combination) that would switch to "custom reply mode". What is the best way to do that, or achieve similar result?

非常感谢!

推荐答案

sys.stdin 上使用 select.select()将允许您检查是否在终端上已经按下了一个键。

Using select.select() on sys.stdin will allow you to check if a key has been pressed at the terminal.

这篇关于Python-对自定义键盘中断做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆